Control Shelly via Node-Red

Hi everyone,

I am a little bit confused.
I have a new Shelly Plus 2PM in my network and am able to access it via node red with "node-red-contrib-shelly"

Injecting it returns a payload like this:

{"input0":{"id":0,"state":false},"input1":{"id":1,"state":false},"switch0":{"id":0,"source":"init","output":false,"apower":0,"voltage":240.5,"current":0,"pf":0,"aenergy":{"total":0,"by_minute":[0,0,0],"minute_ts":1685984378},"temperature":{"tC":50.4,"tF":122.8}},"switch1":{"id":1,"source":"init","output":false,"apower":0,"voltage":240.5,"current":0,"pf":0,"aenergy":{"total":0,"by_minute":[0,0,0],"minute_ts":1685984378},"temperature":{"tC":50.4,"tF":122.8}}}

Thats fine while reading the values.
Now I would like to change the status of the channel0.

I assume I need to send a "true" to "input0.state".
But how do I do that? With the Shelly node givven in node-red I can't access that subobject directly. Has anybody realized that and can give me a hint?

Unfortunatelly all other methods do not work either - IOBroker doesn't find my shelly. The adapter seems to be not configured corretly. So I can't go that way either.

Any help?
Thanks!

Hi,

Unfortunately I cannot import your flow.

You may want to "Inject" the following code. (Put it in a Function node).

    msg.payload = {
        method: "Switch.Set",
        parameters: {
            id: 0,
            on: true,
            toggle_after: 1 // optional flip back time in seconds
        }
    }
return msg;

Regards

Not used a special node for my Shelly's, I always turn on the MQTT support and use that. No extra node needed and no problems with control.

3 Likes

There are several topics in the Forum regarding Shelly and their control using MQTT, some more complicated than others, but it is by far (IMHO), the best method of controlling devices. It is far easier than it first looks! I should know, I was very reluctant at first, but now you can't stop me!!

1 Like

Hi,

don't know about the shelly contrib node and would chime in with the others to use MQTT.
However, if I remember correctly you can use http directly to trigger actions, but you have to enable it in the settings of the shelly. Details are on this page:

EDIT: If I remember correectly the 2PM uses the gen2 API, hence you want to check this page:

An easy example for a flow using MQTT is below; it switches the shelly on and off periodically and I use it when putting citric acid in my kettle to remove calcium buildup :slight_smile:

Sends on or off to shellies/<name of shelly>/relay/0/command.

[{"id":"d5c5ba8d63bb2186","type":"trigger","z":"75ebbb57ee628544","name":"","op1":"on","op2":"off","op1type":"str","op2type":"str","duration":"10","extend":false,"overrideDelay":false,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":830,"y":920,"wires":[["e369faaf92911375","58f20da1d9f9170b"]]},{"id":"58f20da1d9f9170b","type":"mqtt out","z":"75ebbb57ee628544","name":"","topic":"shellies/shellyplug-s-tee/relay/0/command","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"7eafd8c136b65839","x":1170,"y":920,"wires":[]},{"id":"ed5241a08ed97470","type":"inject","z":"75ebbb57ee628544","name":"Entkalker Cycle","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":140,"y":920,"wires":[["98e23282de5934b0"]]},{"id":"e369faaf92911375","type":"debug","z":"75ebbb57ee628544","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1070,"y":880,"wires":[]},{"id":"98e23282de5934b0","type":"function","z":"75ebbb57ee628544","name":"create 15 msg","func":"const cycles = 15;\n\nfor (let i = 1; i < cycles; i++) {\n    node.send(msg);\n}\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":340,"y":920,"wires":[["68e6d6c44a467569"]]},{"id":"68e6d6c44a467569","type":"rate-limiter","z":"75ebbb57ee628544","delay_action":"ratelimit","rate":"1","nbRateUnits":"1","rateUnits":"minute","drop_select":"queue","addcurrentcount":false,"name":"","outputs":"1","buffer_size":"30","buffer_drop":"buffer_drop_new","emit_msg_2nd":false,"control_topic":"control","version":0.0018,"x":590,"y":920,"wires":[["d5c5ba8d63bb2186"]]},{"id":"73032ea55d916e29","type":"inject","z":"75ebbb57ee628544","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"control","payload":"reset","payloadType":"str","x":350,"y":960,"wires":[["68e6d6c44a467569"]]},{"id":"7eafd8c136b65839","type":"mqtt-broker","name":"docker_mosquitto","broker":"mosquitto","port":"1883","clientid":"","autoConnect":true,"usetls":false,"compatmode":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"userProps":"","sessionExpiry":""}]

Thanks for help guys. I managed to get it working with iobroker.
The iobroker adapter for shelly comes with its OWN!! mqtt server. The devices must be connected to that server. After the first device is connected to the mqtt server the adapter turns green and shows the device - and it can be controlled via iobroker, who sends mqtt commands.

Works fine for me - a native node-red solution would be fine aswell but...
Got it working.

Cheers guys!

1 Like

Excellent You can always experiment with Node-RED later...

[{"id":"98f0e0ae.d8d258","type":"mqtt in","z":"14fd8a507079e846","name":"","topic":"shellies/shellyrgbw2-A69BDF/color/0/status","qos":"2","datatype":"auto","broker":"0549f612016b7da9","nl":false,"rap":false,"inputs":0,"x":220,"y":480,"wires":[["a1ae09be3e8029db"]]},{"id":"a1ae09be3e8029db","type":"debug","z":"14fd8a507079e846","name":"debug 280","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":490,"y":480,"wires":[]},{"id":"0549f612016b7da9","type":"mqtt-broker","name":"NodeRED MQTT","broker":"XXX.XXX.XXX.XXX","port":"1883","clientid":"","autoConnect":true,"usetls":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"userProps":"","sessionExpiry":"","credentials":{}}]

In addition to my earlier post, to have a reference for later readers I would like to add a flow for Gen2 API. I came across it on the weekend and it might be handy for somebody in the future.

The relevant documentation is here: Switch | Shelly Technical Documentation

One has to activate RPC over MQTT on the Shelly device:

This is the JSON sent to Shelly to switch it off ("on": false,) and back on ("toggle_after": 15) after 15 sec.

{
    "id": 123,
    "src": "shellyrpc",
    "method": "Switch.Set",
    "params": {
        "id": 0,
        "on": false,
        "toggle_after": 15
    }
}

Image shows debug of GetStatus:

[{"id":"a9551bb676b6e818","type":"inject","z":"1ce2f0fc39d2d9e2","name":"GetStatus","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"id\":123,\"src\":\"shellyrpc\",\"method\":\"Shelly.GetStatus\"}","payloadType":"json","x":880,"y":200,"wires":[["24cab3879927967a"]]},{"id":"a1f29c8cdce87d87","type":"inject","z":"1ce2f0fc39d2d9e2","name":"Turn OFF","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"id\":123,\"src\":\"shellyrpc\",\"method\":\"Switch.Set\",\"params\":{\"id\":0,\"on\":false,\"toggle_after\":15}}","payloadType":"json","x":880,"y":240,"wires":[["24cab3879927967a"]]},{"id":"24cab3879927967a","type":"mqtt out","z":"1ce2f0fc39d2d9e2","name":"","topic":"shellyplusplugs-1234567890/rpc","qos":"0","retain":"false","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"","x":1220,"y":240,"wires":[]},{"id":"b52db48ac53948f6","type":"debug","z":"1ce2f0fc39d2d9e2","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1090,"y":320,"wires":[]},{"id":"2259fa2352464033","type":"mqtt in","z":"1ce2f0fc39d2d9e2","name":"","topic":"shellyrpc/rpc","qos":"2","datatype":"json","broker":"","nl":false,"rap":false,"inputs":0,"x":890,"y":320,"wires":[["b52db48ac53948f6"]]},{"id":"0df4d2aafc7c2ecc","type":"comment","z":"1ce2f0fc39d2d9e2","name":"https://discourse.nodered.org/t/control-shelly-via-node-red/78969/5","info":"https://discourse.nodered.org/t/control-shelly-via-node-red/78969/5","x":1080,"y":80,"wires":[]},{"id":"01a6e3e090776919","type":"comment","z":"1ce2f0fc39d2d9e2","name":"This is for Shelly with Gen2 API and RPC activated","info":"","x":1030,"y":120,"wires":[]}]
1 Like

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.