[Node-red] no $entity() function | Cannot use entites with JSONata

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.

Did I miss something ?

I just happen to be working on an example that I am trying to put together for documentation on just this subject.

Here is an events: state node.

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.

Hi Biscuit !

Thanks for your answer.

I tried to follow your example (using “in” statement and an array in JSONata) with a current_state node and a light entity but still not working :sob:

Here is the configuration of my node and the debug result

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) ?

Thanks a lot for your help !

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

I’m just trying to look if I am able to get entities with $entity() or $entities(), that’s why I use a simple case wich is to get an entity state.

At the beginning, I tried JSONata because I would like to use “or” conditionnal that is nor available in the basic node-red fields in an If statement.

So what can I try to know if $entity()/$entities() works or not ?

Go to the current state node you are using.

You will have to set an entity, and clear the If State selection otherwise you will still get an error

In the output properties at the bottom of the edit box.

Where it usually defaults set msg.payload to the fixed option “entity state”
Change the option to J: JSONata
Enter $entities() in the expression box

Use the node, and you should find that msg.payload has an output being the complete array of all your HA entities

Use $entities(‘sun.sun’) to get just that entity

1 Like

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 :sweat_smile: