Have Objects in msg, need to take actions based on one of the payload codes

Hello, I am using rtl_433 in my node red project and have the need to take actions within node red based upon the data within payload.code of my (msg.payload : Object). Here is an example of the message from the rtl_433 node:

rtl1

I will be keeping rtl_433 running and listening for 433mhz signals that I wish to use within my flow. Based upon the data in the "code" payload and intend to take action on ~20 different codes. I have attempted a few functions and other NOOB pokes at it, but to no avail. I was thinking some type of case statement perhaps then to the output I need. All ideas very welcome.

That is what a switch node does.

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.

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

You may be able to simply use a switch node. Though if you need to switch on different payload properties, a function node might be easier.

If, for example you needed to decided based on the protocol: msg.payload.protocol is what you would choose in the switch node.

If, howeverm you need to decide based on >1 property then almost certainly a function node will be easier.

First of all thank you both! Making some progress with the Switch node as I can have many outputs and action on those appropriately. For now this looks like the correct path unless I need to look at more than one payload.xxx. I can now segregate the actions based upon the codes.

Off-topic, but related to the solution I am attempting:

Is there any way to read the status of the node-red UI switch node? As I am using the same rtl_433 code to both switch on and off a relay I need to check the status of that UI node prior to sending the correct payload to it (On or Off) as my main goal here is to update the UI nodes properly based upon two methods of activating the relay. For instance, I have 433mhz wall switches that directly control the relays which are equipped with 433mhz receivers, I also have the UI node which when pressed sends the 433mhz code via a Python script on my Pi. So now my rtl_433 is listening for the wall switch presses (basis of this thread) and I need to update the UI switch status appropriately based upon its current status.

switch

Looks like I have to store the state of the switch using a function node, but attempting to now read that state prior to sending the proper message to it after reading the rtl_433 code.

Yes, you can use the status node. However, I trust prefer to store the state in flow or global context so that I can access it in a function/switch/change node at any appropriate time.

e.g.

[switch node] :arrow_right: [change node] (set flow.switch1 to msg.payload)

then

[some event] :arrow_right: [change node] (set msg.payload to flow.switch1) :arrow_right: [switch node] (on or off)

Thank you Steve. I have been experimenting with sending the state of the UI switch node to an MQTT topic (retain=true), then attempting to read that same MQTT switch state and use switch and change nodes from there. I have read through your explination several times but cannot seem to wrap my head around what you are suggesting which BTW sounds like a much better solution. Would you be able to expound on your proposed solution or provide a flow that gives me some traction?

It all depends on the end goal.

This is what I meant...

image

[{"id":"38d8e069.8596d","type":"ui_switch","z":"dabbee39.9c292","name":"","label":"switch","tooltip":"","group":"dce9e7a2.d20c78","order":4,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","x":390,"y":180,"wires":[["306abda4.e31ad2"]]},{"id":"306abda4.e31ad2","type":"change","z":"dabbee39.9c292","name":"copy msg.payload --> flow.switch1","rules":[{"t":"set","p":"switch1","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":620,"y":180,"wires":[[]]},{"id":"d4a3ba82.f67da8","type":"inject","z":"dabbee39.9c292","name":"some event","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":400,"y":280,"wires":[["18aba6d4.3147c9"]]},{"id":"18aba6d4.3147c9","type":"change","z":"dabbee39.9c292","name":"copy flow.switch1 --> msg.payload","rules":[{"t":"set","p":"payload","pt":"msg","to":"switch1","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":660,"y":280,"wires":[["1b228ad.6e3ce75"]]},{"id":"1b228ad.6e3ce75","type":"switch","z":"dabbee39.9c292","name":"is payload true or false?","property":"payload","propertyType":"msg","rules":[{"t":"true"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":490,"y":360,"wires":[["1342f993.77b066"],["ba2b469a.e26418"]]},{"id":"1342f993.77b066","type":"debug","z":"dabbee39.9c292","name":"switch1 is true :)","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":760,"y":340,"wires":[]},{"id":"ba2b469a.e26418","type":"debug","z":"dabbee39.9c292","name":"switch1 is false :(","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":770,"y":380,"wires":[]},{"id":"44b9eec.395dc1","type":"comment","z":"dabbee39.9c292","name":"When sw changes, store payload in flow.switch1","info":"","x":520,"y":140,"wires":[]},{"id":"5ad2ff93.dcf1b","type":"comment","z":"dabbee39.9c292","name":"When something unrelated happens, get flow.switch1 & do something based on its value","info":"","x":600,"y":240,"wires":[]},{"id":"dce9e7a2.d20c78","type":"ui_group","name":"Object detection","tab":"5132060d.4cde48","order":1,"disp":true,"width":"7","collapse":false},{"id":"5132060d.4cde48","type":"ui_tab","name":"Home","icon":"dashboard","disabled":false,"hidden":false}]

note: this is a fairly verbose demo purely to demonstrate the use of flow context

3 Likes

Steve, so very interesting. I have never seen the change node used to do the Set, I can see this being very useful! I will spend some time with your example which does look very promising. I will share my solution when I get it properly functioning. Again, I wish to thank you for sharing your skills and time!

That is pretty much its primary purpose

You might do well to spend an hour or so watching this playlist: Node-RED Essentials. The videos are done by the developers of node-red. They're nice & short and to the point. You will understand a whole lot more in about 1 hour. A small investment for a lot of gain.

Will do, Thank you!

1 Like

Making progress, but since the UI switch needs to activate the relay via my python script sending the 433mhz signal the flow goes into a loop of on/off as the rtl_433 sees that 433mhz send when the UI switch is changed. I tried some delays, but that negates the seamless UI update and/or delays the relay on/off. Any ideas?

[{"id":"18e8e789.778a08","type":"tab","label":"Translate 433mhz","disabled":false,"info":""},{"id":"68134d2b.4fc204","type":"debug","z":"18e8e789.778a08","name":"From rtl_433","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":110,"y":80,"wires":[]},{"id":"31e8fc24.42c854","type":"rtl_433","z":"18e8e789.778a08","name":"","device":"","frequency":"","protocols":"","x":90,"y":20,"wires":[["68134d2b.4fc204","a153d738.c85908"]]},{"id":"a153d738.c85908","type":"switch","z":"18e8e789.778a08","name":"","property":"payload.code","propertyType":"msg","rules":[{"t":"eq","v":"450513","vt":"str"},{"t":"eq","v":"8529fb","vt":"str"},{"t":"eq","v":"2529fb","vt":"str"}],"checkall":"true","repair":false,"outputs":3,"x":290,"y":35.5,"wires":[["98768616.8d5188"],["51f1aacb.c56824"],["6c42780f.3f0e48"]]},{"id":"2f917e27.809da2","type":"mqtt out","z":"18e8e789.778a08","name":"","topic":"Relay_1","qos":"0","retain":"true","broker":"98e43eb6.346e2","x":580,"y":40,"wires":[]},{"id":"38d8e069.8596d","type":"ui_switch","z":"18e8e789.778a08","name":"","label":"Relay #1","tooltip":"","group":"dce9e7a2.d20c78","order":4,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","x":864,"y":160,"wires":[["306abda4.e31ad2","cd668987.36ce98"]]},{"id":"306abda4.e31ad2","type":"change","z":"18e8e789.778a08","name":"copy msg.payload --> flow.relay1","rules":[{"t":"set","p":"relay1","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":804,"y":240,"wires":[[]]},{"id":"cd668987.36ce98","type":"exec","z":"18e8e789.778a08","command":"python3 send.py -p 367 -t 1 13148322","addpay":false,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"Relay #1 Send Code","x":1080,"y":160,"wires":[[],[],[]]},{"id":"98768616.8d5188","type":"change","z":"18e8e789.778a08","name":"copy flow.relay1 --> msg.payload","rules":[{"t":"set","p":"payload","pt":"msg","to":"relay1","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":184,"y":160,"wires":[["d81f173e.5ee2b8","afa62b39.2c6098"]]},{"id":"d81f173e.5ee2b8","type":"switch","z":"18e8e789.778a08","name":"is payload true or false?","property":"payload","propertyType":"msg","rules":[{"t":"true"},{"t":"false"}],"checkall":"true","repair":false,"outputs":2,"x":457,"y":160,"wires":[["f5f80e84.274ab"],["b9f164e3.15b8b8"]]},{"id":"b9f164e3.15b8b8","type":"change","z":"18e8e789.778a08","name":"if false, set to true","rules":[{"t":"set","p":"payload","pt":"msg","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":684,"y":180,"wires":[["38d8e069.8596d"]]},{"id":"f5f80e84.274ab","type":"change","z":"18e8e789.778a08","name":"if true, set to false","rules":[{"t":"set","p":"payload","pt":"msg","to":"false","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":684,"y":140,"wires":[["38d8e069.8596d"]]},{"id":"e424bb0.13bf448","type":"ui_switch","z":"18e8e789.778a08","name":"","label":"Relay #2","tooltip":"","group":"dce9e7a2.d20c78","order":4,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","x":864,"y":320,"wires":[["3a3857ba.4f26d8"]]},{"id":"3a3857ba.4f26d8","type":"change","z":"18e8e789.778a08","name":"copy msg.payload --> flow.relay2","rules":[{"t":"set","p":"relay2","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":804,"y":400,"wires":[[]]},{"id":"51f1aacb.c56824","type":"change","z":"18e8e789.778a08","name":"copy flow.relay2 --> msg.payload","rules":[{"t":"set","p":"payload","pt":"msg","to":"relay2","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":184,"y":320,"wires":[["7fa2de29.568a7"]]},{"id":"7fa2de29.568a7","type":"switch","z":"18e8e789.778a08","name":"is payload true or false?","property":"payload","propertyType":"msg","rules":[{"t":"true"},{"t":"false"}],"checkall":"true","repair":false,"outputs":2,"x":457,"y":320,"wires":[["8b9b6c87.31283"],["b767b732.b24208"]]},{"id":"b767b732.b24208","type":"change","z":"18e8e789.778a08","name":"if false, set to true","rules":[{"t":"set","p":"payload","pt":"msg","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":684,"y":340,"wires":[["e424bb0.13bf448"]]},{"id":"8b9b6c87.31283","type":"change","z":"18e8e789.778a08","name":"if true, set to false","rules":[{"t":"set","p":"payload","pt":"msg","to":"false","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":684,"y":300,"wires":[["e424bb0.13bf448"]]},{"id":"91ed0081.468b1","type":"ui_switch","z":"18e8e789.778a08","name":"","label":"Relay #3","tooltip":"","group":"dce9e7a2.d20c78","order":4,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","x":864,"y":480,"wires":[["fafae55a.39ab68"]]},{"id":"fafae55a.39ab68","type":"change","z":"18e8e789.778a08","name":"copy msg.payload --> flow.relay3","rules":[{"t":"set","p":"relay3","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":804,"y":560,"wires":[[]]},{"id":"6c42780f.3f0e48","type":"change","z":"18e8e789.778a08","name":"copy flow.relay3 --> msg.payload","rules":[{"t":"set","p":"payload","pt":"msg","to":"relay3","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":184,"y":480,"wires":[["c748a6a4.1998b8"]]},{"id":"c748a6a4.1998b8","type":"switch","z":"18e8e789.778a08","name":"is payload true or false?","property":"payload","propertyType":"msg","rules":[{"t":"true"},{"t":"false"}],"checkall":"true","repair":false,"outputs":2,"x":457,"y":480,"wires":[["734d2956.3af778"],["1ed2c16c.91c21f"]]},{"id":"1ed2c16c.91c21f","type":"change","z":"18e8e789.778a08","name":"if false, set to true","rules":[{"t":"set","p":"payload","pt":"msg","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":684,"y":500,"wires":[["91ed0081.468b1"]]},{"id":"734d2956.3af778","type":"change","z":"18e8e789.778a08","name":"if true, set to false","rules":[{"t":"set","p":"payload","pt":"msg","to":"false","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":684,"y":460,"wires":[["91ed0081.468b1"]]},{"id":"afa62b39.2c6098","type":"debug","z":"18e8e789.778a08","name":"relay 1 state","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":350,"y":240,"wires":[]},{"id":"98e43eb6.346e2","type":"mqtt-broker","name":"P-Pi-Gibson","broker":"192.168.1.101","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"dce9e7a2.d20c78","type":"ui_group","name":"Object detection","tab":"5132060d.4cde48","order":1,"disp":true,"width":"7","collapse":false},{"id":"5132060d.4cde48","type":"ui_tab","name":"Home","icon":"dashboard","disabled":false,"hidden":false}]

Could be pass though causing a loop (switch it off)

image

Steve, I failed to mention that I also tried that...however in doing so the set UI switch payload fails to update and thus the incoming new state does not alter the UI switch status.

What is the logic you wish to achieve (in steps please)

e.g.

  1. Opertaing UI Switch from OFF-->ON sends XXX??? to Python Script

    1. Python script triggers relay
    2. Relay sends signal to RF
  2. RF input from Relay is recieved in node-red (value is XXXX)

    1. This should do XXX and YYY.

etc.

Set the ui switch to show the state of the input, not the output.

Here is the logic, hopefully it is clear:

  1. 433mhz single button wall switch sends signal to the relay which is equipped with a 433 receiver.

    • One of the primary challenges her is that the same code is used for both on and off as the relay is set to latching mode...as the wall switch is single button, touch on/touch off
  2. I also need to control the relay via the node red UI via the switch node

    • When pressing the UI switch a python script on my pi sends out the same 433mhz code as the wall switch. Again...same code for both on and off, essentially trigger to opposite state at the relay.
  3. rtl_433 is running on the pi and listening for ALL 433mhz transmissions, the node red rtl node is followed by the switch node to separate out each 433mhz captured code into actions to be taken against the UI switch node

    • The overarching goal is that when I activate the load via the wall switch, node red would be unaware of the changed state, thus the UI would not reflect the relays actual state. Thus I wish to "inform" the UI switch node of the changed state which was activated via the wall switch and update accordingly

    • The issue is that when the rtl logic does update the UI switch node, since that node is wired to send out the 433 signal...the process loops

Colin, I am not certain what you are recommending here. I don't see any such option in the UI switch

Set the ui switch to show the state of the input, not the output.

image

It appears when you tell it not to pass through (there is no difference whether it shows the in or the out if the message is being passed through). The way the switch then works is that when you click it the switch icon does not change immediately, the instruction goes off to the real world to change the hardware state, and when confirmation comes back in to the input that it has changed then the icon changes. That way the icon always shows what the real world is doing.

Interesting...my UI switch node does not have the option you show??

@Tomadoggy, Clear the Pass Through checkbox.