Switch node and Object message

#1

I forgot to introduce myself Hi all , i'm new with Node Red and JavaScript. i'm on a project with multi sensor in my home automation.
I would like to make averages for sensor values. I saw this topic which corresponds to my request: DS18B20 smoothing and PID tuning
The Switch node separates the sensors on the different outputs thanks to the topic and payload of each sensor.

For my part, the message I receive is an object:
{" MeteoTemperature ": 32.5," meteoHumidity ": 75}
My question: in the Switch node how do I have to fill the contitions to direct my sensors?

[{"id":"4c6736d0.2f4a38","type":"switch","z":"fe2c7bc6.bd49c8","name":"","property":"payload","propertyType":"msg","rules":[{"t":"cont","v":"payload.meteoTemperature","vt":"jsonata"},{"t":"cont","v":"meteoHumidity","vt":"str"},{"t":"cont","v":"meteoPressure","vt":"str"},{"t":"cont","v":"notUsed","vt":"str"},{"t":"cont","v":"voltage","vt":"str"}],"checkall":"true","repair":false,"outputs":5,"x":358.57144927978516,"y":1865.714412689209,"wires":[["188a6136.b0778f"],,,,]},{"id":"188a6136.b0778f","type":"debug","z":"fe2c7bc6.bd49c8","name":"debug","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":513.5714149475098,"y":1842.7143421173096,"wires":},{"id":"852bed77.21c8f","type":"inject","z":"fe2c7bc6.bd49c8","name":"","topic":"","payload":"{"meteoTemperature":32.5,"meteoHumidity":75}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":212.85714721679688,"y":1867.1429471969604,"wires":[["4c6736d0.2f4a38"]]}]

0 Likes

#2

Hi @SuperNinja

The Switch node can be used to route the entire message to different outputs based on the different rules you create. It does not modify the structure of the message.

You need to use a Split node first. When it receives msg.payload as an Object, it will emit a new message for each key/value pair in that object. You can configure the node to copy the key value to msg.topic.

You can then use a Switch node to check msg.topic and add a rule for each of the topics you are getting.

By the way, please read this post on sharing code in the forum: How to share code or flow json

0 Likes

#3

thanks @knolleary , i test it.

0 Likes