Calculations in "current state"

I have a current state node that is supposed to read the state of a home assistant counter. But I also want it to multiply this value with a factor 0.01 with a JsonATA expression in the same node so I don’t need to put a function node after it. Is this possible and what syntax would I need to accomplish this?

A constant pattern I notice is that I am always struggling with understanding of the current scope where these expressions are evaluated, what fields are visible, and unfortunately there is no auto complete.

You could use just a function node instead…

msg.counterval =  global.get('homeassistant.homeAssistant.states')["counter.your_counter_entity"].state * 0.01;
return msg;

A constant pattern I notice is that I am always struggling with understanding of the current scope where these expressions are evaluated

On the bottom of most HA nodes there is an output option. Here you can tell it what information to send when the node is triggered.


It can be the properties of the defined entity or any other entity or a combination of both. The above gives you the state (on/off/number, etc) of the defined entity to msg.payload. The entity, state and all attributes are sent to

The JSONata equivalent of the above is.


That will give me the same output as the first example. In JSONata now we have more control and can tailor the output. If I just wanted a specific attribute to be sent I can specify that like this:


Something to note, if I copied that path from the debug it would add “data”, i.e. data.attributes.source. In most cases data needs to be removed.

With that same format, using $entities instead of $entity we can choose any entity in Home Assistant:


Like above, the same format applies, we can also choose specific attributes :


Another note most states in nodered are saved as strings, even numbers. The first example will automatically change a number saved as a string to a number, we need to do that manually in JSONata. That is done by adding a number wrapper.


Now that we have a number as a state we can apply mathematical operators.

$number($entities("").state) * 1000
$number($entities("").state) / 1000

Even between entities

($number($entities("").state) + $number($entities("input_number.two").state)) / 2

@Mikefila mind if I through this in the docs somewhere?

Go right ahead, use whatever you need.

This was super helpful. I remember having tried to just get the JSONata equvalent of “entity state” I did not know I needed to refer to the entity as $entity(). For some reason I was also unable to locate the documentation where this is explained and where all available $xxxx() functions for each node are documented.

Does there exist some trick in JSONata to produce a list of all available identifiers in the current scope so I can iteratively explore it even in the absence of code completion?

Like for example in Python I could just use globals() or someobject.__dict__ and have it outputted to the console (or anywhere) to get a list of all identifiers (properties, functions), Is there some equivalent for this in JSONata to explore the scope if I cannot remember which functions or properties are there?

$entity and $entities is specific to the home assistant and are not available in standard nodered nodes like a change node.

In a cell service data field, set to J;expression, click the ... at the bottom there is a drop down list of available functions with examples. These should work in all nodered nodes.

Also on the right, where the debug panel is, there is a context tab. Select globals and hit the little refresh icon. You will find a list of all the states from home assistant.

1 Like