Skip to content

Instantly share code, notes, and snippets.

@jbardi

jbardi/README.md Secret

Last active February 20, 2018 19:50
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jbardi/d3439d33e85b76b5832ac2a0212ef567 to your computer and use it in GitHub Desktop.
Save jbardi/d3439d33e85b76b5832ac2a0212ef567 to your computer and use it in GitHub Desktop.
Home Assistant Websocket

This is a subflow that uses the Websocket node to retrieve events from a Home Assistant installation.

You will need to set your Home Assistant API Password in the flow as well as the IP address of your Home Assistant Installation.

They are marked in the flow as YOUR_API_PASSWORD and YOUR_HASS_IP so that you can easily replace that text with your info before importing the flow into Node-RED

This has been updated from the original version that only had 2 outputs. This subflow now has 5 outputs, as follows:

  1. state_changed events
  2. homeassistant_start event
  3. zwave.network_ready event
  4. zwave.scene_activated events
  5. errors

The first output is for all state_changed events. Here are the payload items with examples:

  • msg.payload.time_fired: "2017-02-19T03:10:26.088271+00:00"
  • msg.payload.domain: "switch"
  • msg.payload.entity: "aeotec_dsc06106_smart_energy_switch_switch_4_0"
  • msg.payload.entity_id: "switch.aeotec_dsc06106_smart_energy_switch_switch_4_0"
  • msg.payload.new_state: "on"
  • msg.payload.old_state: "off"
  • msg.payload.name: "Aeotec DSC06106 Smart Energy Switch Switch"

The second output is for the homeassistant_start event, in case you want to trigger any actions to occur when Home Assistant starts up, otherwise you can skip this second output.

The third output is for the zwave.network_ready event, in case you want to trigger any action to occur when the Z-Wave Network is online and ready, again, if you do not wish to trigger anything or do not use Z-Wave, you can ignore this third output.

The fourth output is for zwave.scene_activated events, this would include devices such as the Aeotec Minimote, or any other devices that trigger Z-Wave Scenes. Once more, if you do not use Z-Wave, you can ignore this fourth output.

The fifth output and final output is for any errors, so you can attach a debug node to this fifth output to help with any errors connecting with your Home Assistant.

I have also included a switch node attached to the first output (changed_events) of the subflow to give you an example of how you can break apart the events by device domain (sensor, switch, light, lock, scene, script, input_boolean, input_slider, input_select, climate, alarm_control_panel and device_tracker)

And finally, I have also included one input along with the 5 outputs, just in case you wish to communicate with Home Assistant's Websocket API

[{"id":"f4e624ef.0af088","type":"subflow","name":"HASS Websocket","info":"","in":[{"x":580,"y":160,"wires":[{"id":"b41a532.7d337b"}]}],"out":[{"x":1260,"y":20,"wires":[{"id":"f8fdb5bd.b1dbb8","port":0}]},{"x":1120,"y":60,"wires":[{"id":"a5ceae16.4203d","port":1}]},{"x":1120,"y":100,"wires":[{"id":"a5ceae16.4203d","port":2}]},{"x":1260,"y":140,"wires":[{"id":"726dbf35.60eeb","port":0}]},{"x":780,"y":160,"wires":[{"id":"1ba5da61.3b4056","port":0}]}]},{"id":"bc0b9e27.a2e1f","type":"websocket in","z":"f4e624ef.0af088","name":"HASS (websocket receive)","server":"","client":"4d562fa0.d65b9","x":190,"y":100,"wires":[["c9ce9b30.348688"]]},{"id":"b41a532.7d337b","type":"websocket out","z":"f4e624ef.0af088","name":"HASS (websocket send)","server":"","client":"4d562fa0.d65b9","x":750,"y":120,"wires":[]},{"id":"c9ce9b30.348688","type":"json","z":"f4e624ef.0af088","name":"","x":370,"y":100,"wires":[["d7fb3de2.34dfc"]]},{"id":"d7fb3de2.34dfc","type":"function","z":"f4e624ef.0af088","name":"Verify Request","func":"if(msg.payload.type == \"auth_required\") {\n var msg_auth = {payload:{\"type\": \"auth\",\"api_password\": \"YOUR_API_PASSWORD\"}};\n return [null, msg_auth, null];\n}else if(msg.payload.type == \"auth_ok\") {\n var msg_sub = {payload:{\"id\": 18,\"type\": \"subscribe_events\"}};\n return [null, msg_sub, null];\n}else if(msg.payload.type == \"result\") {\n if(msg.payload.success === false) {\n return [null, null, msg];\n }else{\n return [null, null, null];\n }\n}else{\n return [msg, null, null];\n}","outputs":"3","noerr":0,"x":520,"y":100,"wires":[["206dfd9b.2c9012"],["b41a532.7d337b"],["1ba5da61.3b4056"]]},{"id":"206dfd9b.2c9012","type":"change","z":"f4e624ef.0af088","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.event","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":720,"y":80,"wires":[["a5ceae16.4203d"]]},{"id":"1ba5da61.3b4056","type":"function","z":"f4e624ef.0af088","name":"Error","func":"msg.payload = \"Websocket Error #\"+msg.payload.error.code+\": \"+msg.payload.error.message;\nreturn msg;","outputs":1,"noerr":0,"x":690,"y":160,"wires":[[]]},{"id":"a5ceae16.4203d","type":"switch","z":"f4e624ef.0af088","name":"Event Type","property":"payload.event_type","propertyType":"msg","rules":[{"t":"eq","v":"state_changed","vt":"str"},{"t":"eq","v":"homeassistant_start","vt":"str"},{"t":"eq","v":"zwave.network_ready","vt":"str"},{"t":"eq","v":"zwave.scene_activated","vt":"str"}],"checkall":"false","outputs":4,"x":950,"y":80,"wires":[["f8fdb5bd.b1dbb8"],[],[],["726dbf35.60eeb"]]},{"id":"f8fdb5bd.b1dbb8","type":"function","z":"f4e624ef.0af088","name":"","func":"var entity_parts = msg.payload.data.entity_id.split(\".\");\nvar time_fired = msg.payload.time_fired;\nvar domain = entity_parts[0];\nvar entity = entity_parts[1];\nvar entity_id = msg.payload.data.entity_id;\nif(msg.payload.data.new_state) {\n var new_state = msg.payload.data.new_state.state;\n var name = msg.payload.data.new_state.attributes.friendly_name;\n}\nif(msg.payload.data.old_state) {\n var old_state = msg.payload.data.old_state.state;\n var name = msg.payload.data.old_state.attributes.friendly_name;\n}\nmsg.payload = \"\";\nmsg.payload = {\n \"time_fired\":time_fired,\n \"domain\":domain,\n \"entity\":entity,\n \"entity_id\":entity_id,\n \"new_state\":new_state,\n \"old_state\":old_state,\n \"name\":name\n};\nreturn msg;","outputs":1,"noerr":0,"x":1150,"y":20,"wires":[[]]},{"id":"726dbf35.60eeb","type":"function","z":"f4e624ef.0af088","name":"","func":"var time_fired = msg.payload.time_fired;\nvar scene_id = msg.payload.data.scene_id;\nvar object_id = msg.payload.data.object_id;\nvar entity_id = msg.payload.data.entity_id;\nmsg.payload = \"\";\nmsg.payload = {\n \"time_fired\":time_fired,\n \"scene_id\":scene_id,\n \"object_id\":object_id,\n \"entity_id\":entity_id\n}\nreturn msg;","outputs":1,"noerr":0,"x":1150,"y":140,"wires":[[]]},{"id":"4d562fa0.d65b9","type":"websocket-client","z":"","path":"ws://YOUR_HASS_IP:8123/api/websocket","wholemsg":"false"},{"id":"c068e90a.f2fc18","type":"subflow:f4e624ef.0af088","z":"42cbfe60.08b14","x":170,"y":120,"wires":[["9c574c2f.11a1d"],[],[],[],[]]},{"id":"9c574c2f.11a1d","type":"switch","z":"42cbfe60.08b14","name":"Device Domains","property":"payload.domain","propertyType":"msg","rules":[{"t":"eq","v":"sensor","vt":"str"},{"t":"eq","v":"switch","vt":"str"},{"t":"eq","v":"light","vt":"str"},{"t":"eq","v":"lock","vt":"str"},{"t":"eq","v":"scene","vt":"str"},{"t":"eq","v":"script","vt":"str"},{"t":"eq","v":"input_boolean","vt":"str"},{"t":"eq","v":"input_slider","vt":"str"},{"t":"eq","v":"input_select","vt":"str"},{"t":"eq","v":"climate","vt":"str"},{"t":"eq","v":"alarm_control_panel","vt":"str"},{"t":"eq","v":"device_tracker","vt":"str"}],"checkall":"false","outputs":12,"x":420,"y":120,"wires":[[],[],[],[],[],[],[],[],[],[],[],[]]}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment