Routing payload depending on state of another entity. Is this really not possible?

I absolutely love NodeRed and use it for all automation-tasks in HA.There is however one functionality / node that I dearly miss:

Something similar to the switch node, where I can specify multiple conditions that each create an output. But instead of routing the message depending on the content of the message itself, It should be routed depending on the state of another entity.

I’m unsure wether I’m too stupid to see the obvious solution to this or if NR really has no possibility to do this - in my eyes - basic and absolutely crutial operation!?

Example:

Message arrives at one single input.
Condition-entity is a sensor for outdoor temperature in °C

IF state of condition-entity smaller or equal 0 THEN route message to output 1
IF state of condition-entity between 0 and 10 THEN route message to output 2
IF state of condition-entity betwenn 10 and 20 THEN route message to output 3
IF state of condition-entity bigger or equal 20 THEN route message to output 4

Seems simple enought, but I have not fund any way to do this!

I have thus far tried these approaches:

  • One could join the original message to be routed with the message of the state of the condition-entity to route them through a switch node, but that would mean externally querying the state of the condition each time and then joining and later un-joining it. Depending of the structures of the two messages this can get really complicated and each flow would be a one-off design.

  • It is kind of possible to do this with gate nodes that open and close whenever the state of the condition-entity changes. But this approach always depends on the last reported value, it does not ask for the current one. So when e.g. NR restarts or when redeploying, this breaks the flow. And if you want to have more that two conditions this increases the complexity exponentially since you need to build a cascade of gates and controls. More than 3 conditions is basically undoable.

  • One can operate a state machine that switches to a specific output depending on the state-change of the condition-entity. This works, but again depends on the latest reportetd value instead of the current one. One could mitigate this by querying the state of the condition-entity e.g. every second, but it creates uneeded system load and is just ugly.

Any input is very welcome. Thank you all in advance!

You can add the state of the other entity to the message and use that path in a switch node for routing. Say I am looking at an input boolean to turn on. I’d set that in an event state and then change the output options.

Using jsonata to add the state of another entity and creating msg.route

$number($entities('sensor.2b_air_sensor_01_temperature').state)

$entities('sensor.2b_air_sensor_01_temperature').state)' gets the state of the entity. $number changes a string to a number. A string will be in quotes. you may or may not need it.

This is the output, notice msg.route

use that in the switch node

1 Like

Your problem description sound exactly like a current state node with the if state field set.

1 Like

Oh, right. I see that now!

However as far as I understand the current state node can only test for one condition and route depending on wheter its is met or not.

…or is there an option for multiple conditions / outputs?

EDIT: This works like a charm.I didnt realize this is how current state could be used. For simple true/false tests this is perfect, for anything with multiple conditions the slightly more complex solution offered by @Biscuit works great.

How difficult do you want to make it?

Well, preferably as simple as possible.

I tried all the ways I myself could think of and searched this & other forums for answers but did not find a solution.

I’m glad there seems to be a simple solution.

Its just that I personally have never seen / used something like the “global” property definition you are showing. I am not a coder so this is anything but self-explanatory to me.

Thank you for providing this input, I will try to understand & replicate this.

Node-RED is a separate program to Home Assistant, and NR knows nothing about the HA entities.

If you add the HA WebSocket nodes, with the homeassistant server correctly set, and with the “Enable Global Context store”, then the server will copy over the entire HA state to a Global Context variable “homeassistant”.

This variable is updated (regularly) and can be used to obtain any of the HA entity values, both states and attributes.

Global Context variables, like flow context, are available in many of the Node-RED basic nodes. Global variables are available across all flow tabs and can be used anywhere. And there is even auto-complete, so it is now even easier to find the variable you want, and to drill down to the sub-field.

Note - I have renamed my HA server, so you will probably have homeassistant.homeAssistant or similar…

The basic Node-RED nodes typically have options to use global and flow context as well as the usual msg.payload option, hence you can change the Switch property to almost anything you like. And with JSONata, you could even read several HA entities and combine them.

Since the HA state value is stored as a string, I use string values to compare, and the ‘Stopping at the first match’ option is used to simplify the tests. Each test happens in order, so the first will fire if the value is <= “0”, only going to the second if not, and so on. Otherwise is a great ‘end stop’.

If you want something more complicated, then you can use JSONata code, either in the test property, or in the individual tests. JSONata can read the global context variables also, so it is possible to route depending on almost any combination of entities and values.

There are, of course, many other ways to do this.

Good luck with your coding.

1 Like

Oh wow - I realize now that “Enable Global Context store” opens a whole word of possibilities for NR, I did not know about this before. This is crazy.

Thank you for your detailed explanations, I have now already made great progress and found the correct value with the auto-complete. It does not work just yet, but I’m confident I’ll figure it out!

EDIT: It works. Amazing. Thank you so much for directing me towards the solution!

Interesting. Thank you.

Slightly more complex than the solution offered below, but I see how this opens up posibilities for more complex conditions.

I will try it out & learn!

1 Like