Switch node with expression as compare value

Hi I try to use a switch node that compare the payload with an entity value from home assistant.
I try to use the expression option for this but I don’t succeed

no error but also nothing comes out of the node

Also tried with double quotes but same issue

Oh yes there is an error - red line around the input box!

You really should expect to see an error in the debug window

"Invalid JSONata expression: Attempted to invoke a non-function"

However, you are also making your JSONata expression result an object by using “{ }” - in JSONata this builds an object, so is expecting “key”: “value” pair and this is why you have a nice red line around your JSONata edit box which says ERROR ! Invalid JSON expression.

The $entities() JSONata function is a special one, added by the Home Assistant websocket nodes, and it only works in these nodes.

Whilst the switch node can accept a valid JSONata expression, you will need to acquire the entity state value from an HA node first.

Option 1:
You can use the current state node to read the state of your entity, and if you set the State Type to ‘number’ it will turn the string into a number for you. You can get the state value into msg.payload and then perform a test in a change node, or in the switch node.

However, as you are looking to just perform a switch based on a comparision, it is very easy to do the comparison directly in the current state node itself. Here is an example (using one of my entities).

What I am doing here is passing the ‘compensation’ value in msg.payload (as input) into the current state node. The node obtains the state information of my sensor.

In the output properties, I am setting msg.payload (as output) as the result of the JSONata expression, which uses the input payload compared to $entity().state, where $entity() is the entity that the node is currently working with.

The outcome of this is that msg.payload is either true or false, which is then very easy to use in a switch node (there is a ‘is true’ and ‘is false’ option in the ‘value option’ list.

I may have my comparison the wrong way around for you, but I am sure you can make this work quite easily, and it saves having to keep two variables, one in msg.payload, and the other somewhere else. Just remember to select ‘State Type’ as number.

Option 2.
You can get the state value you want (as a string) from global context. If you have ‘Enable global context store’ option set in your Home Assistant Server (configuration node) for the WebSocket nodes, then the entire HA state is recorded in a homeassistant global variable.

Using the switch node, we can get at this variable for the sensor, the sensor state, turn this into a number, and do the comparison all in one go. The switch node can use $globalContext() to get a global variable. This is a special Node-RED function added to JSONata but it works wherever you can use JSONata in Node-RED

Here is my first example done this way.

My server is called HA1, but you will probably have homeAssistant, so just change as necessary.

The only thing to watch out for here, is that in the global variable HA state object, all the fields have ‘.’ in the name - “sensor.cb_battery_soc” is the entire key, and thus it requires the sensor.name to be in quotes as shown.

This way will again require your ‘compensation’ to be a number in the input msg.payload, and will switch accordingly, with the least amount of code and just one node.

I hope this helps !

And, if anyone ever reads this stuff later for reference, of if Kermit wants any material for documentation…

The switch node has a JSONata expression option - which expects a JSONata expression that returns true to enable a switch option path.

Your example can be done entirely therefore in just one line

As long as the expression evaluates as ‘true’ then that switch path is taken.

Thanks a lot, got it working

for me it is not working, please help.
payload<=$number($globalContext(“homeassistant”).homeassistant.states.“input_number.lr_temp_in”.state)
Server name Home Assistant
In server node Home Assistant Enable Global Context store is checked.

I immediately thought that I had messed up with the code, but I think the issue is copy-paste from the text introducing smart-quotes.

I have tested this (honest) with my own input_number sensor, and edited it to match your value, so fingers-crossed this works this time.

payload <= $number($globalContext("homeassistant").homeAssistant.states."input_number.lr_temp_in".state)

If you copy from the above code-block, that should solve any issues from ‘smart quotes’.

The global Context value should be called ‘homeassistant’ and have an object with key ‘homeAssistant’, but if you have renamed your server-instance then this has to match.

The referencing of keys with special characters such as “input_number.lr_temp_in” in JSONata is documented as requiring back-ticks, but the standard quotes seem to work fine here so I am leaving this as is.

Hope that now works for you!

Edit:
As @Mikefila has pointed out (see below) I got the ‘homeAssistant’ wrong!

I’m using the addon with the default name. My second home assistant is camel case.

payload <= $number($globalContext("homeassistant").homeAssistant.states."input_number.lr_temp_in".state)
1 Like

Thank you for spotting that. Another good reason for not changing the default values…

1 Like

Thank You! It is working now as expected!