Changing of msg.payload

Hello,

I am bloody new to Node-RED and would ask you for your help to solve my "problem".

I have a homebridge with NR running and connected a Shelly motion sensor and a Shelly PM to then switch its output on motion.

The motion sensor is giving me a payload which the PM is not "understanding" so I think I have to convert it somehow. I tried and googled around but did not come to a solution, so maybe some Pro's can help me!

In the first line is the message from the motion sensor and in the second what i need to send to the shelly output

Thx a lot
cheerio

This is a common need and can be achieved in a change node or a function node or multiple nodes switch and change node combination.
here is a change node example using JSONata expression.
And using a switch node.

[{"id":"12cf7965.5ab35f","type":"inject","z":"c74669a0.6a34f8","name":"","props":[{"p":"payload.MotionDetected","v":"0","vt":"num"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":110.33332824707031,"y":4766.33349609375,"wires":[["6264d36.70275ac"]]},{"id":"6264d36.70275ac","type":"switch","z":"c74669a0.6a34f8","name":"","property":"payload.MotionDetected","propertyType":"msg","rules":[{"t":"eq","v":"0","vt":"num"},{"t":"eq","v":"1","vt":"num"}],"checkall":"false","repair":false,"outputs":2,"x":260,"y":4740,"wires":[["70c210d.dba867"],["6294e50b.cd001c"]]},{"id":"70c210d.dba867","type":"change","z":"c74669a0.6a34f8","name":"","rules":[{"t":"set","p":"payload.On","pt":"msg","to":"false","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":500,"y":4720,"wires":[["df2cf978.75a28"]]},{"id":"6294e50b.cd001c","type":"change","z":"c74669a0.6a34f8","name":"","rules":[{"t":"set","p":"payload.On","pt":"msg","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":470,"y":4760,"wires":[["df2cf978.75a28"]]},{"id":"df2cf978.75a28","type":"debug","z":"c74669a0.6a34f8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":580,"y":4840,"wires":[]},{"id":"dc48ccf2.11107","type":"change","z":"c74669a0.6a34f8","name":"","rules":[{"t":"set","p":"payload.On","pt":"msg","to":"payload.MotionDetected = 0 ? true : false","tot":"jsonata"},{"t":"delete","p":"payload.MotionDetected","pt":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":340,"y":4840,"wires":[["df2cf978.75a28"]]},{"id":"73b548ec.6ff45","type":"inject","z":"c74669a0.6a34f8","name":"","props":[{"p":"payload.MotionDetected","v":"0","vt":"num"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":150,"y":4840,"wires":[["dc48ccf2.11107"]]}]

similar can be done in a function node

1 Like

Thx, this worked perfect!

Is there a classical AND in NR?

cheers

2 switch nodes in series

Or

A function node if(X && y)

Or

A JSONata expression

Hmmmm...if I want to combine two inputs then I have to use JSON?

Hi, I slight mixup there (probably a typo) but for clarification...

  • JSON is a syntax for storing and exchanging data
  • JSONata is a query and transformation language

And no, you dont have to use JSONata (as i said before, 2 switch nodes in series or a function node are viable solutions) & there are add-in contrib nodes for Boolean logic (I dont use any personally)

In JSONata use and / or Boolean Operators · JSONata
in Javasript use && / || JavaScript Comparison and Logical Operators

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