SmartThings Octoblu Integration, Redux: Processing Motion Sensor Data

by Tobias Kreidl, CTP, CUGC Steering Committee

In my previous blog on SmartThings Octoblu Integration, a basic example of creating an alert triggered by the temperature being either above or below certain thresholds was given. There was also a hint at how implementing the reaching or exceeding a specified humidity level could be used to trigger an alert or other event.

However, probably the most tricky part of this device involves dealing with the ZooZ 4-in-1’s motion sensor, which is based on a PIR (Passive InfraRed) sensor. The purpose of this follow-up article is to dive a little deeper into the ZooZ 4-in-1 output and, specifically, show how the motion sensor information can be extracted and incorporated into an Octoblu flow.

We’ve Got the Motion

The image below outlines the elements incorporated into the flow. Let’s review them sequentially, starting with the “On Start” trigger.

The “On Start” trigger is a way of assuring a specific process is run as soon as the flow is activated. In this case, it’s important to instruct the “Home SmartHub” thing to run with the message type “app-get-location” to identify the hub in question so its ID can be associated with the monitoring portion of the flow. It runs the code:

var msg = {hubzeroname: ‘{{msg.payload.hubs[0].name}}’};

return msg;

and that variable is set as:

key: hubzeroname

value: {{msg.hubzeroname}}

within the “Set Key” tool. The “set Key” tool is the closest thing Octoblu has to something like a global variable (at least that’s how I perceive it).

Note that the substring hubs[0].name is the way a specific hub get picked using an index, if multiple choices for different hubs are available.

The ZooZ 4-in-1 sensor is also triggered off of a message type, but in this case, we want to look for an event – one associated with motion detection or lack thereof – and therefore choose “app-get-events”.  It then shows up in the form (sanitized for public viewing):

{

  “msg”: {

    “hubzeroname”: “HomeHub”

  },

  “node”: “ebXXXX80-7XX1-1XX6-aXXe-0XXXXXXXXXXb”

}

That output is combined with getting the key that was used to identify the hub as “hubzeroname” using the “Get Key” tool and fed to the “Compose” tool as a required parameter:

Interestingly, it was not possible (at least in my attempts) to get the hub information to directly feed into the “Compose” tool without making use of the “Set Key” and “Get Key” functions. That ranked probably as the second trickiest part of this undertaking.

Digging Deeper into the Data

A large benefit is that at this point, you can pull data from the device at the “Compose” tool stage in debug mode and can examine the wealth of information output by this device (that normally should be set up to automatically trigger on “reasonable” parameter choices) by looking at the JSON output and sifting through it (again, sanitized here):

{

  “msg”: {

    “payload”: {

      “events”: [

        {

          “date”: “2017-01-03T15:22:39+0000”,

          “id”: “7XXXXXX0-dXX8-1XX6-aXXa-06XXXXXXXX02”,

          “data”: “{\”microDeviceTile\”:{\”type\”:\”value\”,\”label\”:\”RH 29.43%\”,\”backgroundColor\”:\”#ffffff\”}}”,

          “description”: “zw device: 02, command: 9881, payload: 00 31 05 05 42 0B 7F”,

          “descriptionText”: “zooZ 4-in-1 sensor – RV 1.2 humidity is 29.43%”,

          “displayName”: “zooZ 4-in-1 sensor – RV 1.2”,

          “deviceId”: “bXXXXXX3-dXXc-4XX4-8XX2-d4XXXXXXXXd6”,

          “hubId”: “2XXXXXXa-8XX0-4XXb-8bXXXXXXXXXXXXXf1”,

          “installedSmartAppId”: null,

          “isoDate”: “2017-01-03T15:22:39.965Z”,

          “isDigital”: false,

          “isPhysical”: false,

          “isStateChange”: true,

          “locationId”: “dXXXXXX4-6XXe-4XX4-aXXa-b3XXXXXXXX85”,

          “name”: “humidity”,

          “source”: “DEVICE”,

          “unit”: “%”,

          “value”: “29.43”,

          “category”: “event”,

          “type”: “device:smart-thing”

        },

        {

          “date”: “2017-01-03T15:22:40+0000”,

          “id”: “7XXXXXX0-dXX8-1XX6-bXX9-0aXXXXXXXX16”,

          “data”: “{\”microDeviceTile\”:{\”type\”:\”value\”,\”label\”:\”LIGHT 0.17%\”,\”backgroundColor\”:\”#ffffff\”}}”,

          “description”: “zw device: 02, command: 9881, payload: 00 31 05 03 42 00 11”,

          “descriptionText”: “zooZ 4-in-1 sensor – RV 1.2 illuminance is 0.17%”,

          “displayName”: “zooZ 4-in-1 sensor – RV 1.2”,

          “deviceId”: “bXXXXXX3-dXXc-4XX4-8XX2-d4XXXXXXXXd6”,

          “hubId”: “2XXXXXXa-8XX0-4XXb-8bXXXXXXXXXXXXXf1”,

          “installedSmartAppId”: null,

          “isoDate”: “2017-01-03T15:22:40.033Z”,

          “isDigital”: false,

          “isPhysical”: false,

          “isStateChange”: true,

          “locationId”: “dXXXXXX4-6XXe-4XX4-aXXa-b3XXXXXXXX85”,

          “name”: “illuminance”,

          “source”: “DEVICE”,

          “unit”: “%”,

          “value”: “0.17”,

          “category”: “event”,

          “type”: “device:smart-thing”

        },

        {

          “date”: “2017-01-03T15:22:39+0000”,

          “id”: “7XXXXXX0-dXX8-1XX6-aXX6-0aXXXXXXXXc4”,

          “data”: “{\”microDeviceTile\”:{\”type\”:\”value\”,\”label\”:\”64.09°\”,\”backgroundColor\”:\”#76c879\”}}”,

          “description”: “zw device: 02, command: 9881, payload: 00 31 05 01 4A 19 09”,

          “descriptionText”: “zooZ 4-in-1 sensor – RV 1.2 temperature is 64.09°F”,

          “displayName”: “zooZ 4-in-1 sensor – RV 1.2”,

          “deviceId”: “bXXXXXX3-dXXc-4XX4-8XX2-d4XXXXXXXXd6”,

          “hubId”: “2XXXXXXa-8XX0-4XXb-8bXXXXXXXXXXXXXf1”,

          “installedSmartAppId”: null,

          “isoDate”: “2017-01-03T15:22:39.892Z”,

          “isDigital”: false,

          “isPhysical”: false,

          “isStateChange”: true,

          “locationId”: “dXXXXXX4-6XXe-4XX4-aXXa-b3XXXXXXXX85,

          “name”: “temperature”,

          “source”: “DEVICE”,

          “unit”: “F”,

          “value”: “64.09”,

          “category”: “event”,

          “type”: “device:smart-thing”

        },

        {

          “date”: “2017-01-03T01:22:32+0000”,

          “id”: “1XXXXXX0-dXX3-1XX6-8XX4-0aXXXXXXXXd8”,

          “data”: “{\”microDeviceTile\”:{\”type\”:\”value\”,\”label\”:\”67.72°\”,\”backgroundColor\”:\”#63c159\”}}”,

          “description”: “zw device: 02, command: 9881, payload: 00 31 05 01 4A 1A 74”,

          “descriptionText”: “zooZ 4-in-1 sensor – RV 1.2 temperature is 67.72°F”,

          “displayName”: “zooZ 4-in-1 sensor – RV 1.2”,

          “deviceId”: “bXXXXXX3-dXXc-4XX4-8XX2-d4XXXXXXXXd6”,

          “hubId”: “2XXXXXXa-8XX0-4XXb-8XXd-a1XXXXXXXXf1”,

          “installedSmartAppId”: null,

          “isoDate”: “2017-01-03T01:22:32.068Z”,

          “isDigital”: false,

          “isPhysical”: false,

          “isStateChange”: true,

          “locationId”: “dXXXXXX4-6XXe-4XX4-aXXa-b3XXXXXXXX85”,

          “name”: “temperature”,

          “source”: “DEVICE”,

          “unit”: “F”,

          “value”: “67.72”,

          “category”: “event”,

          “type”: “device:smart-thing”

        },

        {

          “date”: “2017-01-03T01:22:32+0000”,

          “id”: “1XXXXXX0-dXX3-1XX6-8XX1-0aXXXXXXXXb0”,

          “data”: “{\”microDeviceTile\”:{\”type\”:\”value\”,\”label\”:\”RH 31.29%\”,\”backgroundColor\”:\”#ffffff\”}}”,

          “description”: “zw device: 02, command: 9881, payload: 00 31 05 05 42 0C 39”,

          “descriptionText”: “zooZ 4-in-1 sensor – RV 1.2 humidity is 31.29%”,

          “displayName”: “zooZ 4-in-1 sensor – RV 1.2”,

          “deviceId”: “bXXXXXX3-dXXc-4XX4-8XX2-d4XXXXXXXXd6”,

          “hubId”: “2XXXXXXa-8XX0-4XXb-8bXXXXXXXXXXXXXf1”,

          “installedSmartAppId”: null,

          “isoDate”: “2017-01-03T01:22:32.133Z”,

          “isDigital”: false,

          “isPhysical”: false,

          “isStateChange”: true,

          “locationId”: “dXXXXXX4-6XXe-4XX4-aXXa-b3XXXXXXXX85”,

          “name”: “humidity”,

          “source”: “DEVICE”,

          “unit”: “%”,

          “value”: “31.29”,

          “category”: “event”,

          “type”: “device:smart-thing”

        },

        {

          “date”: “2017-01-03T01:22:32+0000”,

          “id”: “1XXXXXX1-dXX3-1XX6-bXX0-06XXXXXXXX42”,

          “data”: “{\”microDeviceTile\”:{\”type\”:\”value\”,\”label\”:\”LIGHT 0.34%\”,\”backgroundColor\”:\”#ffffff\”}}”,

          “description”: “zw device: 02, command: 9881, payload: 00 31 05 03 42 00 22”,

          “descriptionText”: “zooZ 4-in-1 sensor – RV 1.2 illuminance is 0.34%”,

          “displayName”: “zooZ 4-in-1 sensor – RV 1.2”,

          “deviceId”: “bXXXXXX3-dXXc-4XX4-8XX2-d4XXXXXXXXd6”,

          “hubId”: “2XXXXXXa-8XX0-4XXb-8XXd-a1XXXXXXXXf1”,

          “installedSmartAppId”: null,

          “isoDate”: “2017-01-03T01:22:32.205Z”,

          “isDigital”: false,

          “isPhysical”: false,

          “isStateChange”: true,

          “locationId”: “dXXXXXX4-6XXe-4XX4-aXXa-b3XXXXXXXX85”,

          “name”: “illuminance”,

          “source”: “DEVICE”,

          “unit”: “%”,

          “value”: “0.34”,

          “category”: “event”,

          “type”: “device:smart-thing”

        },

        {

          “date”: “2017-01-02T14:50:19+0000”,

          “id”: “cXXXXXX0-dXXa-1XX6-aXX6-0aXXXXXXXXc4”,

          “data”: “{\”microDeviceTile\”:{\”type\”:\”value\”,\”label\”:\”LIGHT 0.17%\”,\”backgroundColor\”:\”#ffffff\”}}”,

          “description”: “zw device: 02, command: 9881, payload: 00 31 05 03 42 00 11”,

          “descriptionText”: “zooZ 4-in-1 sensor – RV 1.2 illuminance is 0.17%”,

          “displayName”: “zooZ 4-in-1 sensor – RV 1.2”,

          “deviceId”: “bXXXXXX3-dXXc-4XX4-8XX2-d4XXXXXXXXd6”,

          “hubId”: “2XXXXXXa-8XX0-4XXb-8bXXXXXXXXXXXXXf1”,

          “installedSmartAppId”: null,

          “isoDate”: “2017-01-02T14:50:19.233Z”,

          “isDigital”: false,

          “isPhysical”: false,

          “isStateChange”: true,

          “locationId”: “dXXXXXX4-6XXe-4XX4-aXXa-b3XXXXXXXX85”,

          “name”: “illuminance”,

          “source”: “DEVICE”,

          “unit”: “%”,

          “value”: “0.17”,

          “category”: “event”,

          “type”: “device:smart-thing”

        },

        {

          “date”: “2017-01-02T14:50:19+0000”,

          “id”: “cXXXXXX0-dXXa-1XX6-bXX0-06XXXXXXXX42”,

          “data”: “{\”microDeviceTile\”:{\”type\”:\”value\”,\”label\”:\”RH 31.49%\”,\”backgroundColor\”:\”#ffffff\”}}”,

          “description”: “zw device: 02, command: 9881, payload: 00 31 05 05 42 0C 4D”,

          “descriptionText”: “zooZ 4-in-1 sensor – RV 1.2 humidity is 31.49%”,

          “displayName”: “zooZ 4-in-1 sensor – RV 1.2”,

          “deviceId”: “bXXXXXX3-dXXc-4XX4-8XX2-d4XXXXXXXXd6”,

          “hubId”: “2XXXXXXa-8XX0-4XXb-8XXd-a1XXXXXXXXf1”,

          “installedSmartAppId”: null,

          “isoDate”: “2017-01-02T14:50:19.150Z”,

          “isDigital”: false,

          “isPhysical”: false,

          “isStateChange”: true,

          “locationId”: “dXXXXXX4-6XXe-4XX4-aXXa-b3XXXXXXXX85”,

          “name”: “humidity”,

          “source”: “DEVICE”,

          “unit”: “%”,

          “value”: “31.49”,

          “category”: “event”,

          “type”: “device:smart-thing”

        },

        {

          “date”: “2017-01-02T14:50:19+0000”,

          “id”: “cXXXXXX0-dXXa-1XX6-9XX0-06XXXXXXXX72”,

          “data”: “{\”microDeviceTile\”:{\”type\”:\”value\”,\”label\”:\”64.11°\”,\”backgroundColor\”:\”#76c879\”}}”,

          “description”: “zw device: 02, command: 9881, payload: 00 31 05 01 4A 19 0B”,

          “descriptionText”: “zooZ 4-in-1 sensor – RV 1.2 temperature is 64.11°F”,

          “displayName”: “zooZ 4-in-1 sensor – RV 1.2”,

          “deviceId”: “bXXXXXX3-dXXc-4XX4-8XX2-d4XXXXXXXXd6”,

          “hubId”: “2XXXXXXa-8XX0-4XXb-8bXXXXXXXXXXXXXf1”,

          “installedSmartAppId”: null,

          “isoDate”: “2017-01-02T14:50:19.088Z”,

          “isDigital”: false,

          “isPhysical”: false,

          “isStateChange”: true,

          “locationId”: “dXXXXXX4-6XXe-4XX4-aXXa-b3XXXXXXXX85”,

          “name”: “temperature”,

          “source”: “DEVICE”,

          “unit”: “F”,

          “value”: “64.11”,

          “category”: “event”,

          “type”: “device:smart-thing”

        },

        {

          “date”: “2017-01-01T03:28:58+0000”,

          “id”: “6XXXXXX0-cXX2-1XX6-8XX7-06XXXXXXXX3c”,

          “data”: “{\”microDeviceTile\”:{\”type\”:\”value\”,\”label\”:\”LIGHT 0.34%\”,\”backgroundColor\”:\”#ffffff\”}}”,

          “description”: “zw device: 02, command: 9881, payload: 00 31 05 03 42 00 22”,

          “descriptionText”: “zooZ 4-in-1 sensor – RV 1.2 illuminance is 0.34%”,

          “displayName”: “zooZ 4-in-1 sensor – RV 1.2”,

          “deviceId”: “bXXXXXX3-dXXc-4XX4-8XX2-d4XXXXXXXXd6”,

          “hubId”: “2XXXXXXa-8XX0-4XXb-8XXd-a1XXXXXXXXf1”,

          “installedSmartAppId”: null,

          “isoDate”: “2017-01-01T03:28:58.283Z”,

          “isDigital”: false,

          “isPhysical”: false,

          “isStateChange”: true,

          “locationId”:

          “name”: “illuminance”,”dXXXXXX4-6XXe-4XX4-aXXa-b3XXXXXXXX85″,

          “source”: “DEVICE”,

          “unit”: “%”,

          “value”: “0.34”,

          “category”: “event”,

          “type”: “device:smart-thing”

        }

      ],

      “command”: “app-get-events”

    },

    “fromUuid”: “0XXXXXX8-4XXc-4XXf-9XXa-c2XXXXXXXX4b”

  },

  “node”: “eXXXXXX0-7XX1-1XX6-aXXe-09XXXXXXXX9b”

}

At this point, we have a much better insight into the format of the output and how different sensor elements and their associated parameters are labeled. The “name” parameter is heavily overloaded as is the parameter “value,” those being the same for each sensor. So, how do we differentiate one sensor from another? It’s by examining the “Equal” tool labeled “Equal motion” and requiring the condition for the “motion” sensor:

{{msg.payload.name}} = motion

to be fulfilled, otherwise, the flow stops at this point, e.g. if instead, the value is “humidity” or “temperature.” Assuming that is the case, we next look to see what the payload.value key contains.

If motion is detected by the PIR, it will trigger an independent event that otherwise doesn’t show up in the output. When it does, it looks something like this:

{

  “msg”: {

    “payload”: {

      “value”: “active”,

      “name”: “motion”

    },

    “hubzeroname”: “HomeHub”

  },

  “node”: “aXXXXXX0-7XXd-1XX6-aXXe-09XXXXXXXX9b”

}

In other words, {{msg.payload.value}} contains the entry “active”. Note that we need to process these sequentially since Octoblu performs just individual comparisons; it would also be senseless to perform extra evaluations on any detections that are unrelated to motion, of which there are many. Hence, we first look if the payload.name key points to “motion” before checking the payload.value token. If would be much easier if the construct were instead something like {{msg.payload.motion.value}} in which case one could do a single query, but this isn’t how the handler was written, and why this is a bit tricky. Thankfully, the debug options in Octoblu give you the ability to examine parameters at every step of the process.

I have my motion sensor set to only update on detection of motion or subsequent lack thereof only if the state changes within 5 minutes so as to not over-burden the device with too many triggered events. In other words, a subsequent detection of motion within five minutes of the initial detection will not trigger an additional event; after five minutes of no additional motion detection, a subsequent “lack of motion detection” event will be generated. A positive motion detection triggers the next event.

Given the information above, it is clear that we need to look for a payload where:

{{msg.payload.value}} = active | inactive

to isolate that particular case. The logic now divides, because it has to now look at this additional parameter indicating whether motion detection is “active” or “inactive”.

Depending then on whether the state is active or inactive, we could have skipped splitting the flow and even just generated a single message that covered both cases, but here, I chose instead to customize the messages further, hence the split to two different conditions and associated email “things”. For the sake of completeness, here is what the debug output looks like for when the inactive state is sensed:

{

  “msg”: {

    “payload”: {

      “value”: “inactive”,

      “name”: “motion”

    },

    “hubzeroname”: “HomeHub”

  },

  “node”: “cXXXXXX0-7XXd-1XX6-aXXe-09XXXXXXXX9b”

}

The Results

The output can then be sent using email, SMS, text, etc. to the desired recipients or of course, could be processed further within the flow. In this case we simply generate an email message with a recipient plus a subject line and email body for the case of motion detected:

hub: {{msg.hubzeroname}} – Motion detected status: {{msg.payload.value}}

Detected parameter: {{msg.payload.name}}, status: {{msg.payload.value}} on {{msg.hubzeroname}}. Motion was detected by this device.

And these for the case there was no further motion detected:

hub: {{msg.hubzeroname}} – Stopped detection: {{msg.payload.name}}, status: {{msg.payload.value}}

Detected parameter: {{msg.payload.name}}, status: {{msg.payload.value}} on {{msg.hubzeroname}}. Motion has ceased to be detected on this device.

As mentioned above, these could of course be combined into a single message with more generic descriptions or pre-processed further before creating an even more customized single email message. Additional messages could be also generated as text, sent to a Slack channel, etc. as desired. One could get even more creative and decide whom to send reports to, depending on the time the detection took place.

Conclusion

This demonstrates the extraction of the motion components of the ZooZ 4-in-1 device. The PIR settings I used are a motion timeout of 5 minutes and a sensitivity of 4. That seems to work quite reasonably without being over- or under-sensitive. A similar procedure could be of course applied to any motion sensor that can be integrated into the Octoblu environment. Though this example is handled with a lot of detail, the process itself will be similar for other configurations. It will also depend a lot on the nature of how data are generated, hence having good debugging options at your disposal is invaluable.

Pro Bono Bonus Section

If you look carefully at the output from the “Compose” debug section above, you may notice certain repetitions. Indeed, the motion detected in this case also triggered a significant enough of a change in the light level to trigger that sensor to provide readings multiple times. This may or may not be something desirable, and is left as an exercise to the reader to ponder. For example, would a cat trigger motion but not cause significant enough of a light level change for that also to trigger, or would it need to be a much larger object like a human or an elephant? Would the overall reflectivity of the object be significant, as well? If using a sensor such as this for security purposes, factors like this, as well as the time of the day, whether lights are switched on and off in that area, if doors and windows are being opened and closed, and many others that affect readings all need to be taken into consideration.

Leave a Reply