Setting input_number from attribute using node red

Hello everyone,

I want to set an inpunt_number helper to a value that I get from an attribute of an entity. Unfortunately, I always get an error message (“HomeAssistantError: required key not provided @ data[‘value’]”) from the call service node.

What I do: I call a service for the weather forecast and then set an input_number to a value from the weather information. This is the flow:
flow

This is the massage I get from the weather service:
msg

Now I want to set the input_number to the value of the precipitation for today. To do this, I use a call service node:


I copied the path of the precipitation from the debug node.

As mentioned, this gives me an error message: “HomeAssistantError: required key not provided @ data[‘value’]”
If I set the input_number to a fixed value like {“value”:1.5}, it works fine. It’s probably just a small bug in my JSONata, but I can’t figure it out…

Thank you very much for your help!

Yes, JSONata is a very different language, and the referencing syntax for object fields with special characters uses quotes and not [ ].

You want

payload.'weather.forecast_home'.forecast[0].precipitation

where the key with the problematic . in it is “weather.forecast_home” .

For anyone who likes the ‘documentation - approach’ to learning new things, the official JSONata documentation says to use backticks ` to enclose key fields with special characters, but I am finding that ordinary single quotes are working just as well.

The ‘path reference’ in the Node-RED debug window uses the JavaScript referencing approach, but in JSONata the [ ] is a special filter operator, so in using this you are actually asking for msg.payload to be filtered by the expression “weather.forecast_home” being true which it is not, hence no output.

Since this question does come up from time to time, particularly with the HA use of “.” in the middle of key fields, here is how to debug JSONata and test this for yourself.

First capture the full message output from the previous node, so that you can copy the entire message object in the NR debug window.

Then go to https://try.jsonata.org/

In the left hand window, select the entire JSON input object (example) and paste over this with your debug message output.

You can now use the top right hand window to write a JSONata expression, and the evaluation result appears in the window below. This is dynamic, so typing in

payload

gets the msg.payload object, then you can continue the expression.

Note that the left hand side can be edited too, which makes debugging easier.

As a bonus, since the problem is the key with the “.” in it, you can also use the JSONata wildcard operator. Since there is only one forecast return from the service call

payload.*.forecast[0].precipitation

will return just the forecast object we want. Even better

payload.**.precipitation

will return an array of all precipitation values, using an ‘any-depth’ wildcard. This means that you can also use

(payload.**.precipitation)[0]

although the required use of ( ) is another oddity of JSONata since the [ ] filter operator binds more tightly to the precipitation field than it does to the output of payload.**.precipitation

1 Like

Wow, thank you very much for your post. That helped a lot!
Probably the most awesome reply I’ve ever gotten on a forum.