Skip to content

Instantly share code, notes, and snippets.

@Paul-Reed
Last active Dec 10, 2020
Embed
What would you like to do?
Shelly 1 & 1pm example api flows

A small collection of HTTP API & MQTT flows to help you get started talking between node-RED and a Shelly 1/1pm wifi relay.

The shelly api setup guide is not the easiest to understand (at least to me!), so after several readings, I got my shelly 1pm talking to node-RED, and have added the flows here in case they provide a useful start for others.
All you need to do to use these examples are change the IP in the function nodes to that of your shelly's IP, and also edit the MQTT nodes to use the ID of your shelly (for example - shelly1pm-BA1936).
See https://shelly-api-docs.shelly.cloud/#common-http-api for the official api docs.

There are 3 sections to this gist,

  • mqtt
  • http requests
  • flow to accumulate power readings

MQTT
Examples showing how to receive data from the shelly, and also control it's relay state.

http requests
The HTTP API allows users to not only control the relay state, but also to receive it's status, change configuration settings, reboot the device, etc...

flow to accumulate power readings
A flow (which is not my own work!) to receive the the regular power readings from the shelly, and create a accumulated reading. The flow maintains continuity even if the shelly is rebooted/loses power etc.
The accumulated data can be reset, for example every midnight, by injecting the 'reset energy counter'.

[{"id":"4ad6479.c8cc3b8","type":"mqtt in","z":"ceaa4a2a.b2cf88","name":"power readings","topic":"shellies/shelly1pm-BA1936/relay/0/power","qos":"2","datatype":"auto","broker":"3ecd7f11.4d3dd","x":230,"y":150,"wires":[["13fb1305.e290fd"]]},{"id":"13fb1305.e290fd","type":"debug","z":"ceaa4a2a.b2cf88","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":440,"y":150,"wires":[]},{"id":"5c2ed097.9529b","type":"debug","z":"ceaa4a2a.b2cf88","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":630,"y":720,"wires":[]},{"id":"abe24fc8.c374f","type":"comment","z":"ceaa4a2a.b2cf88","name":"flow to accumulate power readings","info":"","x":280,"y":680,"wires":[]},{"id":"f1974e0c.c5bde","type":"inject","z":"ceaa4a2a.b2cf88","name":"Clear","topic":"","payload":"","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":319,"y":770,"wires":[["70dee9e2.463c68"]]},{"id":"244f4b1.616beb4","type":"mqtt in","z":"ceaa4a2a.b2cf88","name":"Shelly - Energy","topic":"shellies/shelly1pm-BA1936/relay/0/energy","qos":"2","datatype":"auto","broker":"3ecd7f11.4d3dd","x":220,"y":720,"wires":[["d0fc7a46.8e75a8"]]},{"id":"d0fc7a46.8e75a8","type":"function","z":"ceaa4a2a.b2cf88","name":"Total Energy","func":"// Given current total energy from Shelly in msg.payload as a string\npayload = parseInt(msg.payload)\n// check that it has converted ok (so it is not not-a-number)\nif (!isNaN(payload)) {\n let runningTotalEnergy = flow.get(\"runningTotalEnergy\") || 0\n let previousShellyValue = flow.get(\"previousShellyValue\") || 0\n // test whether the Shelly has reset since last input\n if (payload < previousShellyValue) {\n // yes it has, just add new value to the total\n runningTotalEnergy += payload\n } else {\n // no so add the difference between this and last to the running total\n runningTotalEnergy += payload - previousShellyValue\n }\n flow.set(\"previousShellyValue\", payload)\n flow.set(\"runningTotalEnergy\",runningTotalEnergy)\n // Convert watt/minutes to watt/hours\n msg.payload = (runningTotalEnergy/60)\n} else {\n // an illegal string passed in so ignore it, don't pass on anything\n msg = null\n}\nreturn msg;","outputs":1,"noerr":0,"x":410,"y":720,"wires":[["5c2ed097.9529b"]]},{"id":"c73eee2b.44d47","type":"mqtt in","z":"ceaa4a2a.b2cf88","name":"Shelly announcements","topic":"shellies/#","qos":"2","datatype":"auto","broker":"3ecd7f11.4d3dd","x":250,"y":100,"wires":[["13fb1305.e290fd"]]},{"id":"c2b8ddab.31cb4","type":"mqtt out","z":"ceaa4a2a.b2cf88","name":"","topic":"","qos":"2","retain":"false","broker":"3ecd7f11.4d3dd","x":420,"y":290,"wires":[]},{"id":"fee0bdf6.ce384","type":"inject","z":"ceaa4a2a.b2cf88","name":"","topic":"shellies/shelly1pm-BA1936/relay/0/command","payload":"on","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":220,"y":250,"wires":[["c2b8ddab.31cb4"]]},{"id":"7aeb348d.ebaa4c","type":"inject","z":"ceaa4a2a.b2cf88","name":"","topic":"shellies/shelly1pm-BA1936/relay/0/command","payload":"off","payloadType":"str","repeat":"","crontab":"00 23 * * *","once":false,"onceDelay":0.1,"x":220,"y":290,"wires":[["c2b8ddab.31cb4"]]},{"id":"4114738a.fe5d5c","type":"inject","z":"ceaa4a2a.b2cf88","name":"Report status","topic":"shellies/command","payload":"update","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":240,"y":330,"wires":[["c2b8ddab.31cb4"]]},{"id":"753c1c9e.1150c4","type":"inject","z":"ceaa4a2a.b2cf88","name":"ON","topic":"","payload":"on","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":230,"y":530,"wires":[["e4e6cfa1.b88ea"]]},{"id":"e4e6cfa1.b88ea","type":"function","z":"ceaa4a2a.b2cf88","name":"turn relay on/off","func":"var x = msg.payload;\nvar IP = \"192.168.1.22\";\nmsg.url= \"http://\"+ IP + \"/relay/0\";\n\nmsg.payload = \"turn=\" + x;\nreturn msg;","outputs":1,"noerr":0,"x":470,"y":550,"wires":[["cf4b0b9c.2e3788"]]},{"id":"f73044cd.de8df8","type":"inject","z":"ceaa4a2a.b2cf88","name":"OFF","topic":"","payload":"off","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":230,"y":570,"wires":[["e4e6cfa1.b88ea"]]},{"id":"97ac4109.5ddb2","type":"function","z":"ceaa4a2a.b2cf88","name":"get device status","func":"\nvar IP = \"192.168.1.22\";\nmsg.url= \"http://\"+ IP + \"/status\";\n\n\nreturn msg;","outputs":1,"noerr":0,"x":480,"y":490,"wires":[["cf4b0b9c.2e3788"]]},{"id":"36814aad.047cb6","type":"inject","z":"ceaa4a2a.b2cf88","name":"","topic":"","payload":"","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":230,"y":490,"wires":[["97ac4109.5ddb2"]]},{"id":"3434812a.ff7f9e","type":"function","z":"ceaa4a2a.b2cf88","name":"change mqtt update period","func":"var x = msg.payload;\nvar IP = \"192.168.1.22\";\nmsg.url= \"http://\"+ IP + \"/settings\";\n\nmsg.payload = \"mqtt_update_period=\" + x;\nreturn msg;","outputs":1,"noerr":0,"x":510,"y":450,"wires":[["cf4b0b9c.2e3788"]]},{"id":"cf4b0b9c.2e3788","type":"http request","z":"ceaa4a2a.b2cf88","name":"","method":"POST","ret":"obj","paytoqs":false,"url":"","tls":"","persist":false,"proxy":"","authType":"","x":730,"y":540,"wires":[["cf280268.e0c2a"]]},{"id":"cf280268.e0c2a","type":"debug","z":"ceaa4a2a.b2cf88","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":920,"y":540,"wires":[]},{"id":"f25f81e4.86b0a","type":"inject","z":"ceaa4a2a.b2cf88","name":"MQTT update period","topic":"","payload":"300","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":270,"y":450,"wires":[["3434812a.ff7f9e"]]},{"id":"77129295.530c9c","type":"comment","z":"ceaa4a2a.b2cf88","name":"http requests","info":"","x":220,"y":410,"wires":[]},{"id":"f717b6e4.beb698","type":"mqtt in","z":"ceaa4a2a.b2cf88","name":"Relay status","topic":"shellies/shelly1pm-BA1936/relay/0","qos":"2","datatype":"auto","broker":"3ecd7f11.4d3dd","x":220,"y":200,"wires":[["13fb1305.e290fd"]]},{"id":"70dee9e2.463c68","type":"change","z":"ceaa4a2a.b2cf88","name":"Reset energy counter","rules":[{"t":"set","p":"runningTotalEnergy","pt":"flow","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":510,"y":770,"wires":[[]]},{"id":"9087142f.0b8a88","type":"function","z":"ceaa4a2a.b2cf88","name":"reboot device","func":"var IP = \"192.168.1.22\";\nmsg.url= \"http://\"+ IP + \"/reboot\";\nreturn msg;","outputs":1,"noerr":0,"x":470,"y":610,"wires":[["cf4b0b9c.2e3788"]]},{"id":"61fdbcd.868c344","type":"inject","z":"ceaa4a2a.b2cf88","name":"","topic":"","payload":"","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":230,"y":610,"wires":[["9087142f.0b8a88"]]},{"id":"eb2a8afb.78f218","type":"comment","z":"ceaa4a2a.b2cf88","name":"mqtt","info":"","x":200,"y":60,"wires":[]},{"id":"3ecd7f11.4d3dd","type":"mqtt-broker","z":"","name":"paul_broker","broker":"192.168.1.8","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment