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.