Shelly Gen2 MQTT Help

Hi, I am fairly new to Node Red but have managed to configure a number of Shelly Gen1 Devices to control lighting. I now have a Gen2 device and the MQTT structure is entirly different and I am a bit stuck.

I have my input MQTT subscribed to the topic shellyplus1-a8032abd78a0/events/rpc which seems to work as I can see items appearing in the debug window when I press the switch connected to the shelly. However when I run the below flow it doesnt do anything so there must be somethign wrong with my logic

Any help would be greatly appreciated

cheers

[{"id":"3b526e4b9eaed659","type":"tab","label":"Master Bedroom","disabled":false,"info":""},{"id":"8b6ebd648cdf2ca2","type":"node-lifx-in","z":"3b526e4b9eaed659","server":"396bbcdbf33a4c89","name":"Master Bedroom LifX","lightID":"d073d52d2bb8","x":680,"y":280,"wires":[]},{"id":"2e9c027016b6159e","type":"mqtt in","z":"3b526e4b9eaed659","name":"Input status","topic":"shellyplus1-a8032abd78a0/events/rpc","qos":"2","datatype":"json","broker":"2fd5bebaac766e96","nl":false,"rap":false,"x":130,"y":280,"wires":[["169b43a126bb7e7f"]]},{"id":"d1372af601a04d86","type":"comment","z":"3b526e4b9eaed659","name":"mqtt","info":"","x":110,"y":190,"wires":[]},{"id":"faf0c79fa1570f2a","type":"function","z":"3b526e4b9eaed659","name":"","func":"const Switch = msg.payload.params[\"input:0\"].State\n\nif (Switch =='true')\n{\n    msg.payload=true \n}\n if (Switch =='false')\n{\n    msg.payload=false \n}\n\nreturn msg","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":480,"y":280,"wires":[["8b6ebd648cdf2ca2","450888c621532251"]]},{"id":"775b4629a399ca07","type":"comment","z":"3b526e4b9eaed659","name":"Description","info":"uses mqtt from Shelly switch to command the LifX bulb","x":1090,"y":200,"wires":[]},{"id":"169b43a126bb7e7f","type":"rbe","z":"3b526e4b9eaed659","name":"","func":"rbe","gap":"","start":"","inout":"out","septopics":true,"property":"payload","topi":"topic","x":310,"y":280,"wires":[["faf0c79fa1570f2a"]]},{"id":"a5937025617e9404","type":"function","z":"3b526e4b9eaed659","name":"","func":"if(msg.payload.params[\"input:0\"].state==true)\n{\n    msg.payload=true\n}\n\nif(msg.payload.params[\"input:0\"].state==false)\n{\n    msg.payload=false\n}\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":800,"y":200,"wires":[[]]},{"id":"450888c621532251","type":"debug","z":"3b526e4b9eaed659","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":690,"y":400,"wires":[]},{"id":"396bbcdbf33a4c89","type":"node-lifx-server","name":"Lifx Server","address":"","broadcast":"","lights":"","interval":"10000"},{"id":"2fd5bebaac766e96","type":"mqtt-broker","name":"","broker":"localhost","port":"1883","clientid":"","usetls":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"sessionExpiry":""}]


image

I see that you have a node in your flow that isn't installed in a 'stock' node-RED install (Master BedroomLifX ??), which means that anyone loading your flow to try and help you, would need to install that node in their own system. You would be more likely to get support if you didn't include that node in your exported flow.

When you "see items appearing in the debug window", is that originating from the debug node in your flow above? and if so please show us what the message is.
Also, what did you expect the message to be?

Add a debug node to the output of the filter node so you can see what the data is you are testing.

Then ask yourself “What if the value I’m testing is a string true or false and not a Boolean? Or what if it isn’t true or false?”

If I move the debug node to the filter output

I am getting this output

{"src":"shellyplus1-a8032abd78a0","dst":"shellyplus1-a8032abd78a0/events","method":"NotifyStatus","params":{"ts":1635751127.78,"input:0":{"id":0,"state":false}}}

The variable that changes state is the "input:0" state

The input to the LifX node needs to be true or false (true for on, and false for off)

I am trying to output to the Lifx node a value of true when the "input:0" state is 1 and an output of false when the "input:0" state is 0

The code will in the function node will be complicated by the fact that the property name input:0 has a special character in it. However there’s a great page in the docs that will explain how to use the debug panel to find the right path to any data item.

Pay particular attention to the part about the buttons that appear under your mouse pointer when you over hover a debug message property in the sidebar.

BX00Cy7yHi

https://nodered.org/docs/user-guide/messages

Got to say @CTRDevelopments, that the Shelly Gen 2 api guide is not easy reading...

1 Like

this is how I receive MQTT from a shelly 4 pro pm

[{"id":"18f1c280a57f5b40","type":"mqtt in","z":"9120cee1.edcfb","name":"","topic":"shellypro4pm-94b97ec035e8/status/switch:0","qos":"0","datatype":"auto","broker":"36a1d46c.15334c","nl":false,"rap":true,"rh":0,"x":270,"y":420,"wires":[["bcfe805d6c325d7c","84528b3260458079","fb76b1a3aee80f68"]]},{"id":"15c0f7bcb90abf85","type":"mqtt in","z":"9120cee1.edcfb","name":"","topic":"shellypro4pm-94b97ec035e8/status/switch:1","qos":"0","datatype":"auto","broker":"36a1d46c.15334c","nl":false,"rap":true,"rh":0,"x":270,"y":460,"wires":[["0d73ddc899de87a2","b46cc81da1f5e519","b669dadb9fe424a2"]]},{"id":"9bc05a2f74729ffd","type":"mqtt in","z":"9120cee1.edcfb","name":"","topic":"shellypro4pm-94b97ec035e8/status/switch:2","qos":"0","datatype":"auto","broker":"36a1d46c.15334c","nl":false,"rap":true,"rh":0,"x":270,"y":500,"wires":[["d436f4f1ae97a4d1","284ea4c77de8fe10","42b53ff7a3f872b6"]]},{"id":"b66b34da6865a753","type":"mqtt in","z":"9120cee1.edcfb","name":"","topic":"shellypro4pm-94b97ec035e8/status/switch:3","qos":"0","datatype":"auto","broker":"36a1d46c.15334c","nl":false,"rap":true,"rh":0,"x":270,"y":540,"wires":[["0c86e5d8a6f509f2","54428b441d310b6a","c88f4b0e31682834","66d61fcde98a446f"]]},{"id":"36a1d46c.15334c","type":"mqtt-broker","broker":"10.0.0.43","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","willTopic":"","willQos":"0","willPayload":""}]

input:0 is already ture/false according to you debug output.

Isn't it the 'output' value that is required?

There is no "output" in the mqtt payload
Just a matter of moving input:0.state to msg.payload.

It's not shown in the posts above, but as far as I can recall reading a while ago, there should be - output: true that shows the state of the relay.

@Paul-Reed shown here from debug output.

Yes, I'm aware of that.

Thought not as you said, crossed wires.

@CTRDevelopments

[{"id":"2f3857b0.d52508","type":"mqtt in","z":"b779de97.b1b46","name":"","topic":"+/events","qos":"2","datatype":"auto","broker":"d675b749.04b9c8","x":100,"y":4840,"wires":[["15a091a2.300d5e"]]},{"id":"15a091a2.300d5e","type":"change","z":"b779de97.b1b46","name":"","rules":[{"t":"set","p":"device","pt":"msg","to":"payload.src","tot":"msg"},{"t":"move","p":"payload.params[\"input:0\"].state","pt":"msg","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":290,"y":4840,"wires":[["79ad3f9a.085908"]]},{"id":"79ad3f9a.085908","type":"switch","z":"b779de97.b1b46","name":"","property":"device","propertyType":"msg","rules":[{"t":"eq","v":"shellyplus1-a8032abd78a0","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":500,"y":4840,"wires":[["4678db9d.b6641c"]]},{"id":"e039615d.30aa9","type":"inject","z":"b779de97.b1b46","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"shellyplus1-a8032abd78a0/events","payload":"{\"src\":\"shellyplus1-a8032abd78a0\",\"dst\":\"shellyplus1-a8032abd78a0/events\",\"method\":\"NotifyStatus\",\"params\":{\"ts\":1635751127.78,\"input:0\":{\"id\":0,\"state\":false}}}","payloadType":"json","x":130,"y":4900,"wires":[["15a091a2.300d5e"]]},{"id":"4678db9d.b6641c","type":"debug","z":"b779de97.b1b46","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":690,"y":4880,"wires":[]},{"id":"d675b749.04b9c8","type":"mqtt-broker","name":"Localhost","broker":"localhost","port":"1883","clientid":"","usetls":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

@juntiedt Could you post the MQTT output message from your Shelly please, so we can see the expected format.

{"id": 3, "source": "HTTP", "output": false, "apower": 0.000, "voltage": 232.168,"aenergy": {"total":47.131,"by_minute":[0.000,0.000,0.000],"minute_ts":1635763499},"temperature":{"tC":null, "tF":null}}

EDIT by moderator (for easier reading...)

{
   "id":3,
   "source":"HTTP",
   "output":false,
   "apower":0.000,
   "voltage":232.168,
   "aenergy":{
      "total":47.131,
      "by_minute":[
         0.000,
         0.000,
         0.000
      ],
      "minute_ts":1635763499
   },
   "temperature":{
      "tC":null,
      "tF":null
   }
}
1 Like

this is the MQTT output of switch 3 of a shelly 4pro pm.
it gives you:

on off status
apower in Watt
voltage in Volt
energy used and
internal temp of the switch (in this case switch 3)

I am not too worried about the relay state, as I set that to always be on. That way the bulb is always powered and ready. It's the input switch state that I want to know. Although I assume it's a similar problem to solve? Cheers

This is probably a better way of describing my issue

Now I'm confused... why are you using a shelly to control a light if it's never switched off?