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.

  1. Before coding your Function, get the Thing’s Client ID and Client Secret under the Interfaces tab and store the values under Space Settings > Variables Storage.
  2. In User Functions > Function, choose to create + New Function.
  3. Enter a Function name (for example, toggle-logic).
  4. Choose to code using the template for Python3 and include the code below.
    1. Complete the following variables with your own values: SPACE and THING_UID
    2. Enter the name of your variables to get them from the variables storage and store their value in the variables my_client_id and my_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')
        }
    
  5. Under event Trigger, click + Add Topic. Complete YourSpaceID and YourThingUID 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)

  6. In User Functions > Triggers, choose to add + New Trigger.
    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.
  7. In this example, you need to define a Trigger to listen to the Event topic that will trigger the invocation of our Function:
    1. Choose type MQTT.
    2. Set the host as mqtt.swx.altairone.com
    3. Username and Password: MQTT credentials from our Thing.
    4. Topic: spaces/YourSpaceID/things/YourThingUID/actions/toggle
  8. Check the Function status is running, and proceed to test if it works as expected.