Shelly integration Nodered

Hello everybody

A question, does anyone know how I can integrate a Shelly switch in Nodered?
At the end I want to switch off the Shellys from Nodered and always know the status of the Shelly (whether closed or open)

Can someone help me there ?

Many Thanks!

The easiest way to control Shellys is using MQTT. You can also query their status.

1 Like

Alternatively take look at the node-red-contrib-shelly node.
(However I agree with @ghayne, MQTT is a better option)

Or use node-red-contrib-shelly. That gives you the option of still using cloud services (correct me if I'm wrong but enabling MQTT disables cloud service?)

1 Like

Yes, I think so too, but I've been trying for a while, but can't get to the Shelly ...

Yes, I think so too, but I've been trying for a while, but can't get to the Shelly

Have you enabled MQTT in the Shelly?

1 Like

To enable MQTT on a Shelly, you need recent firmware and you need to not be using the Shelly cloud.

You could also re-flash the Shelly with alternative firmware such as Tasmota or ESPHome.

Yes, I have, but I don't know exactly how I have to do the whole thing in Nodered to make it work. Does anyone have instructions on how this works?

You can start here: Shelly RGBW2 mit RGBW LED Strip Nodered Flow?

This will get you started if you compare the flow to the documentation on the Shelly website. Afterwards you can change the flow to use different shellies.

I'm assuming that you already have setup a server (mosquitto or Aedes??).

Firstly, enter the url of your shelly in a browser, for example, and then navigate to the 'ADVANCED - DEVELOPER SETTINGS' and then 'Internet & Security'. Scroll down to the MQTT section and ensure that your server details are correct.

Tick the 'Use custom MQTT prefix', and enter a suitable topic (less than 25 characters), such as 'shelly-switch', save & exit.

You can check if the shelly is subscribed to your server, by running the attached flow - just change the shelly's IP address in the function node.
It should return true.

[{"id":"c105348b.9c8838","type":"inject","z":"217df193.d774ce","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":true,"onceDelay":"15","topic":"","payload":"","payloadType":"str","x":115,"y":2025,"wires":[["2181481b.e73508"]]},{"id":"2181481b.e73508","type":"function","z":"217df193.d774ce","name":"subscribe check","func":"//IP address of your shelly\nvar IP = \"\";\nmsg.url= \"http://\"+ IP + \"/status\";\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":275,"y":2025,"wires":[["82f34ec5.6b90e"]]},{"id":"82f34ec5.6b90e","type":"http request","z":"217df193.d774ce","name":"","method":"POST","ret":"obj","paytoqs":"ignore","url":"","tls":"","persist":false,"proxy":"","authType":"basic","x":475,"y":2025,"wires":[["b9c2b635.a80028"]]},{"id":"b9c2b635.a80028","type":"debug","z":"217df193.d774ce","name":"debug","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload.mqtt.connected","targetType":"msg","statusVal":"","statusType":"auto","x":630,"y":2025,"wires":[]}]

If it is connected, then load the flow below, and update the MQTT node with your server details. The flow should then operate your shelly.

[{"id":"637a5b46.1cfc04","type":"mqtt out","z":"217df193.d774ce","name":"","topic":"","qos":"0","retain":"false","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"aa11e6d8.6cd088","x":300,"y":2290,"wires":[]},{"id":"3e851ca5.bfdb04","type":"inject","z":"217df193.d774ce","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"shellies/shelly-switch/relay/0/command","payload":"on","payloadType":"str","x":170,"y":2270,"wires":[["637a5b46.1cfc04"]]},{"id":"a1d185f8.bc0848","type":"inject","z":"217df193.d774ce","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"shellies/shelly-switch/relay/0/command","payload":"off","payloadType":"str","x":170,"y":2310,"wires":[["637a5b46.1cfc04"]]},{"id":"aa11e6d8.6cd088","type":"mqtt-broker","name":"Local server","broker":"","port":"1883","clientid":"Local node-RED","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"admin","birthQos":"0","birthRetain":"false","birthPayload":"Hello from Local node-RED","closeTopic":"admin","closeQos":"0","closeRetain":"false","closePayload":"Local node-RED is about to exit","willTopic":"admin","willQos":"0","willRetain":"false","willPayload":"Local node-RED has now exited"}]


You can find further help & all of the MQTT commands here - API Reference

1 Like

what kind of server should I already have set up?

I did say in my last post...

You need a MQTT broker for the Shelly to talk & listen to.
'Server' is another term for 'Broker' (I called it a Server because that's what the Shelly calls it in the config)

So with Nodered I can't control the Shellys directly?

Yes, using the node-red-contrib-shelly node, as discussed above, but if you want to use MQTT, you do need to use a MQTT server.
Unless you want a learning experience, maybe use the shelly node, and leave MQTT for another day :wink:

Correct @Steve-Mcl

1 Like

was fĂĽr ein MQTT Broker empfehlen Sie?

I would suggest reading the basics about MQTT before going further, it will answer most of your questions, and enable you to determine if MQTT is the correct approach for you.

1 Like

I have a Shelly EM and I use my Perl lib which uses the HTTP API with the device and then an HTTP API with Node-Red.

Using the included CGI script power-outlet-json.cgi - Control Power::Outlet device with JSON web service (e.g. Node-Red) -

Configure the INI file

$ cat /etc/power-outlet.ini

Then you use a standard http request node to make it turn on and off.

[{"id":"379199c2.aa7f06","type":"http request","z":"1d254cc8.5925b3","name":"power-outlet","method":"GET","ret":"obj","paytoqs":false,"url":"{{topic}};action={{payload}}","tls":"","persist":false,"proxy":"","authType":"","x":250,"y":280,"wires":[["ee0ca2cf.7c35d"]],"inputLabels":["Topic=>name, Payload=>action"]},{"id":"7543ad3a.13e6c4","type":"inject","z":"1d254cc8.5925b3","name":"","topic":"MyShelly","payload":"ON","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":80,"y":260,"wires":[["379199c2.aa7f06"]]},{"id":"9abe2d4.518afd","type":"inject","z":"1d254cc8.5925b3","name":"","topic":"MyShelly","payload":"OFF","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":80,"y":300,"wires":[["379199c2.aa7f06"]]},{"id":"ee0ca2cf.7c35d","type":"debug","z":"1d254cc8.5925b3","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":430,"y":280,"wires":[]}]

If you are on an RPM based platform use the RPMs to install

rpmbuild -ba Power-Outlet-0.42.tar.gz

If not use CPAN to install.

install Power::Outlet

Then copy the CGI file to /vaw/www/cgi-bin/ or something like that for Apache.

1 Like

The REST API is well documented I seem to remember, you should be able to use native Node-RED http-request nodes to do everything if you don't want to use MQTT.

The REST API does not require a connection to Shelly Cloud.