Skip to content

Instantly share code, notes, and snippets.

@LosantGists
Last active June 15, 2016 16:46
Show Gist options
  • Select an option

  • Save LosantGists/5a6ebcb3fbe60b35aa75754773a15aa2 to your computer and use it in GitHub Desktop.

Select an option

Save LosantGists/5a6ebcb3fbe60b35aa75754773a15aa2 to your computer and use it in GitHub Desktop.
A workflow for receiving state from and sending commands to Losant's Python-powered, Internet-connected two-man switches.
{
"globals":[
{
"key":"phone",
"type":"string"
},
{
"key":"slackUrl",
"type":"string"
}
],
"triggers":[
{
"config":{
},
"outputIds":[
[
"HJKSAHi34"
]
],
"key":"type/twoManSwitch",
"type":"deviceTag",
"meta":{
"category":"trigger",
"name":"deviceIdsTags",
"label":"State Received",
"x":580,
"y":40,
"uiId":"HyQlSCrj3E",
"description":"When a switch / button sends its state ..."
}
},
{
"config":{
},
"outputIds":[
[
"ByZxBRHjh4"
]
],
"key":"type/twoManSwitch",
"type":"deviceTagDisconnect",
"meta":{
"category":"trigger",
"name":"deviceIdsTagsDisconnect",
"label":"Switch Offline",
"x":40,
"y":40,
"uiId":"BkExHRBshV",
"description":"When a switch goes offline ..."
}
},
{
"config":{
},
"outputIds":[
[
"ByZxBRHjh4"
]
],
"key":"type/twoManSwitch",
"type":"deviceTagConnect",
"meta":{
"category":"trigger",
"name":"deviceIdsTagsConnect",
"label":"Switch Online",
"x":240,
"y":40,
"uiId":"H1BxBAHihV",
"description":"When a switch comes online ..."
}
}
],
"nodes":[
{
"type":"GaugeNode",
"meta":{
"category":"data",
"name":"gauge",
"label":"Get All Switches",
"x":660,
"y":380,
"deviceSelectionType":"direct",
"description":"Let's get the last reported state of each switch"
},
"config":{
"resultPath":"data.allKeysTurned",
"aggregation":"MIN",
"attribute":"isKeyTurned",
"deviceIds":[
],
"deviceTags":[
{
"key":"type",
"value":"twoManSwitch"
}
],
"deviceIdsPath":""
},
"id":"SyrRHj3N",
"outputIds":[
[
"BkeHCHinV"
]
]
},
{
"type":"ConditionalNode",
"meta":{
"category":"logic",
"name":"conditional",
"label":"All are Turned?",
"x":660,
"y":480,
"description":"If any key is not turned, fail the button press"
},
"config":{
"expression":"{{data.allKeysTurned.value}} === 1"
},
"id":"BkeHCHinV",
"outputIds":[
[
"BJWrRronN"
],
[
"SyMS0So2V"
]
]
},
{
"type":"ConditionalNode",
"meta":{
"category":"logic",
"name":"conditional",
"label":"Button Pressed?",
"x":580,
"y":260,
"description":"Was this state change the result of a key turn or a button press?"
},
"config":{
"expression":"{{data.isButtonPressed}} === true"
},
"id":"SJXSAro24",
"outputIds":[
[
"rJ4BABjnV"
],
[
"SyrRHj3N"
]
]
},
{
"type":"DebugNode",
"meta":{
"category":"output",
"name":"debug",
"label":"GO!",
"x":840,
"y":800,
"description":"Debug output"
},
"config":{
"message":"ALL TURNED! GO!"
},
"id":"B1BHASsnN",
"outputIds":[
[
"By8SAHs3N"
]
]
},
{
"type":"DebugNode",
"meta":{
"category":"output",
"name":"debug",
"label":"STOP!",
"x":620,
"y":800,
"description":"Debug output"
},
"config":{
"message":"STOP!"
},
"id":"BJWrRronN",
"outputIds":[
[
"ryPrAHj2N"
]
]
},
{
"type":"DeviceSendCommandNode",
"meta":{
"category":"output",
"name":"device-command",
"label":"Failure animation",
"x":620,
"y":900,
"deviceSelectionType":"direct",
"description":"Blink the LEDs red on all devices to indicate a failed press"
},
"config":{
"nameTemplate":"btnPressedAnim",
"payloadTemplate":"{\n\"status\": \"failed\"\n}",
"sendToDeviceIds":[
],
"sendToDeviceTags":[
{
"key":"type",
"value":"twoManSwitch"
}
],
"deviceIdsPath":""
},
"id":"ryPrAHj2N",
"outputIds":[
]
},
{
"type":"DeviceSendCommandNode",
"meta":{
"category":"output",
"name":"device-command",
"label":"Success animation",
"x":840,
"y":900,
"deviceSelectionType":"direct",
"description":"Blink the LEDs green on all devices to indicate a successful press"
},
"config":{
"nameTemplate":"btnPressedAnim",
"payloadTemplate":"{\n\"status\": \"succeeded\"\n}",
"sendToDeviceIds":[
],
"sendToDeviceTags":[
{
"key":"type",
"value":"twoManSwitch"
}
],
"deviceIdsPath":""
},
"id":"By8SAHs3N",
"outputIds":[
[
"ByOS0Bjh4",
"ByFX8s2N"
]
]
},
{
"type":"RawFunctionNode",
"meta":{
"category":"logic",
"name":"function",
"label":"Add Dev. ID to data",
"x":580,
"y":160,
"description":"We need to know the ID of the device that just reported its state"
},
"config":{
"script":"if(!payload.data) payload.data = {};\npayload.data.deviceId = payload.triggerId;\nreturn payload;"
},
"id":"HJKSAHi34",
"outputIds":[
[
"SJXSAro24"
]
]
},
{
"type":"RawFunctionNode",
"meta":{
"category":"logic",
"name":"function",
"label":"Define Key Status",
"x":400,
"y":700,
"description":"Determine what happened and define the status that should be sent to the devices"
},
"config":{
"script":"if(!payload.data) payload.data = {};\nif(payload.data.disconnectReason) {\npayload.data.keyStatus = 'disconnected';\n}\nelse if(payload.data.isKeyTurned === 1) {\npayload.data.keyStatus = 'engaged';\n} else {\npayload.data.keyStatus = 'disengaged';\n}\nreturn payload;"
},
"id":"rJ4BABjnV",
"outputIds":[
[
"By5rRri24"
]
]
},
{
"type":"DebugNode",
"meta":{
"category":"output",
"name":"debug",
"label":"Key / Con / Disc",
"x":400,
"y":800,
"description":"Debug output"
},
"config":{
"message":""
},
"id":"By5rRri24",
"outputIds":[
[
"HkjH0BjnN"
]
]
},
{
"type":"DeviceSendCommandNode",
"meta":{
"category":"output",
"name":"device-command",
"label":"setKeyStatus",
"x":400,
"y":900,
"deviceSelectionType":"direct",
"description":"Send a command to all devices updating the current key-turned status"
},
"config":{
"nameTemplate":"setKeyStatus",
"payloadTemplate":"{\n\"deviceId\": \"{{ data.deviceId }}\", \n\"keyStatus\": \"{{ data.keyStatus }}\"\n}",
"sendToDeviceIds":[
],
"sendToDeviceTags":[
{
"key":"type",
"value":"twoManSwitch"
}
],
"deviceIdsPath":""
},
"id":"HkjH0BjnN",
"outputIds":[
]
},
{
"type":"StructureSmsNode",
"meta":{
"category":"output",
"name":"structure-sms",
"label":"SMS",
"x":740,
"y":1000,
"description":"Placeholder action for now. Add anything you want here!"
},
"config":{
"bodyTemplate":"Both keys turned and button pressed!",
"phoneNumberTemplate":"{{globals.phone}}"
},
"id":"ByOS0Bjh4",
"outputIds":[
]
},
{
"type":"GetValueNode",
"meta":{
"category":"data",
"name":"get-value",
"label":"Get Connected Number",
"x":140,
"y":260,
"description":"How many devices are currently connected?"
},
"config":{
"keyName":"connectedSwitches",
"valuePath":"data.connectedSwitches",
"defaultValueType":"json",
"defaultValue":"0"
},
"id":"rJ3BCrs3N",
"outputIds":[
[
"By6BAHohE"
]
]
},
{
"type":"ConditionalNode",
"meta":{
"category":"logic",
"name":"conditional",
"label":"Disconnect Event?",
"x":140,
"y":360,
"description":"Which happened: a device connection or a device disconnection?"
},
"config":{
"expression":"{{data.disconnectReason}} !== undefined"
},
"id":"By6BAHohE",
"outputIds":[
[
"HJCHASo3V"
],
[
"SkylBCHj3N"
]
]
},
{
"type":"MathNode",
"meta":{
"category":"logic",
"name":"math",
"label":"Devices - 1",
"x":240,
"y":480,
"description":"A device went offline. Decrease the connected number"
},
"config":{
"statements":[
{
"expression":"{{data.connectedSwitches}} - 1",
"resultPath":"data.connectedSwitches"
}
]
},
"id":"SkylBCHj3N",
"outputIds":[
[
"HyxxrRri2V"
]
]
},
{
"type":"MathNode",
"meta":{
"category":"logic",
"name":"math",
"label":"Devices + 1",
"x":40,
"y":480,
"description":"A device came online. Increase the connected number"
},
"config":{
"statements":[
{
"expression":"{{data.connectedSwitches}} + 1",
"resultPath":"data.connectedSwitches"
}
]
},
"id":"HJCHASo3V",
"outputIds":[
[
"HyxxrRri2V"
]
]
},
{
"type":"RawFunctionNode",
"meta":{
"category":"logic",
"name":"function",
"label":"Add Dev. ID to data",
"x":140,
"y":160,
"description":"We need to know the ID of the device that just reported its connection status"
},
"config":{
"script":"if(!payload.data) payload.data = {};\npayload.data.deviceId = payload.triggerId;\nreturn payload;"
},
"id":"ByZxBRHjh4",
"outputIds":[
[
"rJ3BCrs3N"
]
]
},
{
"type":"StoreValueNode",
"meta":{
"category":"data",
"name":"store-value",
"label":"Store Connected Number",
"x":140,
"y":600,
"description":"Maintain the number of connected devices in our workflow storage."
},
"config":{
"keyName":"connectedSwitches",
"valueType":"path",
"value":"data.connectedSwitches"
},
"id":"HyxxrRri2V",
"outputIds":[
[
"rJ4BABjnV"
]
]
},
{
"type":"ConditionalNode",
"meta":{
"category":"logic",
"name":"conditional",
"label":"All are On?",
"x":760,
"y":680,
"description":"If any key is offline, fail the button press"
},
"config":{
"expression":"{{data.connectedSwitches}} >= 2"
},
"id":"SkflBASo3N",
"outputIds":[
[
"BJWrRronN"
],
[
"B1BHASsnN"
]
]
},
{
"type":"GetValueNode",
"meta":{
"category":"data",
"name":"get-value",
"label":"Get Connected Number",
"x":760,
"y":580,
"description":"Retrieve the current number of connected devices from workflow storage"
},
"config":{
"keyName":"connectedSwitches",
"valuePath":"data.connectedSwitches",
"defaultValueType":"json",
"defaultValue":"0"
},
"id":"SyMS0So2V",
"outputIds":[
[
"SkflBASo3N"
]
]
},
{
"type":"SlackNode",
"meta":{
"category":"output",
"name":"slack",
"label":"Slack",
"x":940,
"y":1000,
"description":"Let's also send an animation to one of our Slack channel"
},
"config":{
"urlPathTemplate":"{{globals.slackUrl}}",
"channelTemplate":"",
"textTemplate":"http://bestanimations.com/Military/Explosions/atomic-mushroom-cloud-explosion-2-2.gif"
},
"id":"ByFX8s2N",
"outputIds":[
]
}
],
"name":"Two-Man Switch",
"enabled":true,
"description":"",
"applicationId":"57578a7aba8f4701000d23cd",
"_type":"flow",
"_exportDate":"2016-06-15T16:45:47.360Z"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment