Switch Node | Beginner Question

Hey guys,

starting to learn Node-RED and hence running into some 'beginner-friendly' questions :smiley:

2022-11-14 13_15_48-Window
screenshot 1

2022-11-14 13_17_43-Window
screenshot 2

The screenshots are taken from a switch node that I have attached to an 'mqtt in' node representing the output from a thermometer. The object that gets passed can be see in the second screenshot.
Now in my mind, it should be possible to parse the number from temperature and apply a simple logic: "If temperature below 18°C do this", else "do that".
I set this up in screenshot one. However this does not work. Even if the temp is below 18°C the node will always choose "otherwise".

Interestingly enough, if I ask it to proceed if regex matches \d*\.\d* it will correctly understand that there is a number and continue the flow. I must be missing something fundamental here, anyone willing to help?

PS. I already have the automation running by using a change node and moving msg.payload.temperature to msg.payload. Everything is running fine. I just want to understand the switch node better.


You are using JSONata which you do not require
The mqtt message comes in on msg.payload.temperature most likely, but your test screenshot, you have enter the data in msg, not msg.payload.

I would set the switch property to msg. payload.temperature
and the rule to as you have it.
<= 18

As to regex it the payload a string or an object, show us in a debug node the msg entering the switch node.

1 Like

Awesome. Yes I see it now. Explains why the regex matches aswell. Thank you!

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