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 https://github.com/node-red/node-red/issues/108, 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

No comments:

Post a Comment