Build the Logic Behind the Action
For this simple example, you will update the value of the Property state, once the Action of turning the lamp on/off is executed.
- Before coding your Function, get the Thing’s Client ID and Client Secret under the Interfaces tab and store the values under .
- In + New Function. , choose to create
- Enter a Function name (for example, toggle-logic).
-
Choose to code using the template for Python3 and
include the code below.
-
Complete the following variables with your own values:
SPACE
andTHING_UID
-
Enter the name of your variables to get them from the variables storage
and store their value in the variables
my_client_id
andmy_client_secret
.
import json from iots import API from function import variables SPACE = "enter-your-space-ID" THING_UID = "enter-thing-UID" my_scopes = ["space", "category", "thing"] def handle(req): action = json.loads(req.body.decode('utf-8')) if variables.exists("my_client_id"): my_client_id = variables.get("my_client_id") if variables.exists("my_client_secret"): my_client_secret = variables.get("my_client_secret") with API().set_credentials(my_client_id, my_client_secret, my_scopes) as api: href = action["toggle"]["href"] action_id = href.split('/')[-1] input_action = action["toggle"]["input"]["value"] response = api.spaces(SPACE).things(THING_UID).properties().get() response_json = response.json() property_value = response_json["state"] if input_action != property_value: payload_property = { "state": input_action } update_property = api.spaces(SPACE).things( THING_UID).properties("state").update(payload_property) payload_action = { "toggle": { "status": "Completed - Lamp State has been successfully updated." } } update_action = api.spaces(SPACE).things(THING_UID).actions( "toggle", action_id).update(payload_action) else: payload_action = { "toggle": { "status": "Completed - Lamp State Unchanged" } } update_action = api.spaces(SPACE).things(THING_UID).actions( "toggle", action_id).update(payload_action) return { "status_code": 200, "body": req.body.decode('utf-8') }
-
Complete the following variables with your own values:
-
Under event Trigger, click + Add Topic. Complete
YourSpaceID
andYourThingUID
in the topic below with your own values and click Save:spaces/YourSpaceID/things/YourThingUID/actions/toggle
The logic behind the Function will update the state of the lamp if an Action is triggered. It will check that the input value of the Action is different from the state of the lamp and if so, the state of the lamp will be updated. If not, it will remain unchanged.Note that in a real implementation, you would add to the code the logic needed to actually execute the Action (typically, a request to a specific endpoint)
-
In + New Trigger.
, choose to add A Trigger is a component that is capable of invoking serverless Functions from an event source. They work as a listener to a particular endpoint to convert incoming events to HTTP requests to asynchronously invoke all the Functions that match certain criteria.
-
In this example, you need to define a Trigger to listen to the Event topic that
will trigger the invocation of our Function:
- Choose type MQTT.
- Set the host as mqtt.swx.altairone.com
- Username and Password: MQTT credentials from our Thing.
-
Topic:
spaces/YourSpaceID/things/YourThingUID/actions/toggle
- Check the Function status is running, and proceed to test if it works as expected.