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 msg.data
.
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:
$entities("input_number.one").state
Like above, the same format applies, we can also choose specific attributes :
$entities("light.entrance").attributes.brightness
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.
$number($entities("input_number.one").state)
Now that we have a number as a state we can apply mathematical operators.
$number($entities("input_number.one").state) * 1000
$number($entities("input_number.one").state) / 1000
Even between entities
($number($entities("input_number.one").state) + $number($entities("input_number.two").state)) / 2