Wednesday, April 27, 2016

Node Red: Forcing numeric comparisons in a Switch node

I've been learning and using node-red to control a relay that turns on my car charger (technically an EVSE, the charger being in the car itself) and one thing I discovered as that the Switch node doesn't enforce any specific type for the comparisons. A bug was reported and a change made in a previous version, for details see, but this was then removed when new features were added to the Switch node.

How this causes problems is if your numbers have different lengths and the default msg.payload type is a string; then 850 is greater than 2000.

The solution I found was a very simple intermediate function that just changes the type of the msg.payload to a number. Put this in front of your Switch node and all works as expected:

save image

Long delayed update

It's been sometime since I posted anything here. Social media and forums means that much of the stuff I'm do get mentioned there in the first instance. I am going to try to make the effort to post here too and in more detail if for no other reason than to have a long lived and locatable instance of all this stuff that I think may be interesting.