Hi,
I’m just new in HA and discovering it step by step for 3 weeks now.
I have installed HAOS on an Intel NUC and I’m using a “SONOFF ZigBee 3.0 USB Dongle Plus” as a bridge for my Zigbee devices.
So I’ve installed MQTT and ZigeeToMqtt in HA.
From there, as I work in IT, I chose to use Node-Red for my automations so I’ve installed Node-red add-on in HA.
In Node-red, I can use my HA entities with different nodes (current_state, events…), at least when I’m using basic fields of the nodes.
But I would like to use JSONata in the “If statement” to be able to make advanced conditionnals.
The problem is that I don’t see (and cannot use) the entity() function to be able to use my HA entities in Node-red JSONata.
Here is the ‘If State’ selection, looking at ‘in’ and therefore an array.
I am using J: option (note - this is the only way you get to run JSONata in any Node-RED node, by selecting this option when it is available).
The JSONata I have is
(
$time:=$entities('sensor.time').state;
$append(["on"], $time>"08:30" and $time<"17:30" ? ["off"]);
)
and you can see $entities(‘sensor.time’).state works perfectly.
Note:
In standard JSONata, the documented JSONata functions are included. When using JSONata in Node-RED, NR prepares each statement and adds in a few extra functions (for reading the context variables).
In, and ONLY in the WebSocket nodes, the JSONata statement is further prepared by adding in the special WebSocket functions.
This means that you cannot run / test them in the standard node editor box. You can only use them within the WebSocket Nodes.
Also, I am wondering if there is a way to handle automations with code (like javascript or python) instead of using HA automations or Node-red wich looks like a no-code environnment (like Zapier or Make) ?
Your JSONata does not look right. I am not sure what you are trying to do here, but the node is already returning the light state as a string “on or” off".
Your JSONata is getting the entity state again, the hard way (use $entity().state) then doing a conditional test against a field called ‘off’ not a string “off”.
This won’t work, and would return a Boolean, which is not an array.
Interesting error message. One reason why JSONata can be hard to write correctly
It worked ! Thank you so much for unblocking my situation.
However, I just found on another topic that it would be possible to use NodeJS and the HA API to do anything I want.
As I already have worked with NodeJS, I think I will waste less time going in that direction instead of trying to learn how node-red and jsonata work