Help with Service call and node red please

Hello All Sorry to ask what I hope with be a simple question but is been driving me nuts.
I have been using Node Red for a while now and have figured most things out. but this one it stumping me.
I am using a call service node to get weather forecasts and it returns data like this:
msg.payload : Object
object
weather.openweathermap: object
forecast: array[8]
0: object
datetime: “2024-07-16T12:00:00+00:00”
condition: “rainy”
temperature: 20.4

templow: 14.1
pressure: 1010
cloud_coverage: 87
wind_speed: 24.62
wind_bearing: 294
uv_index: 5.86
precipitation_probability: 100
precipitation: 3.76
dew_point: 14.9
wind_gust_speed: 42.26
humidity: 85
1: object
datetime: “2024-07-17T12:00:00+00:00”
condition: “partlycloudy”
temperature: 20.5
templow: 12.8
pressure: 1021
cloud_coverage: 29
wind_speed: 16.42

I am interested is only the following data
payload[“weather.openweathermap”].forecast[0].precipitation
payload[“weather.openweathermap”].forecast[1].precipitation_probability

So how do I take this data and use it to set a helper of the same names so I can use it a flow to control garden watering?

Sorry if I am asking this in the wrong area or it a dumb question but just cant work it out.

Thanks in advance

Please add code tags to your data so that it formats correctly on the forum.

You trust the forecast for lawn watering? We have had forecasts of rain and thunderstorms for the past week, and other than a light sprinkle a few days ago, it’s been a desert here.

It helps everyone when you use the </> code tags (for YAML, Node-RED code, and data structures).

{"weather.forecast_home":{"forecast":[{"condition":"rainy","datetime":"2024-07-16T11:00:00+00:00","wind_bearing":307.9,"temperature":21,"templow":14.7,"wind_speed":19.1,"precipitation":1.8,"humidity":65},{"condition":"cloudy","datetime":"2024-07-17T11:00:00+00:00","wind_bearing":236.4,"temperature":21.1,"templow":12.1,"wind_speed":14.4,"precipitation":0.1,"humidity":68},{"condition":"cloudy","datetime":"2024-07-18T11:00:00+00:00","wind_bearing":215.1,"temperature":23.7,"templow":14,"wind_speed":14.4,"precipitation":0,"humidity":62},{"condition":"cloudy","datetime":"2024-07-19T11:00:00+00:00","wind_bearing":246.3,"temperature":21.8,"templow":13.2,"wind_speed":12.6,"precipitation":0.1,"humidity":70},{"condition":"cloudy","datetime":"2024-07-20T11:00:00+00:00","wind_bearing":197.3,"temperature":21.7,"templow":16.6,"wind_speed":18.4,"precipitation":3.7,"humidity":74},{"condition":"rainy","datetime":"2024-07-21T11:00:00+00:00","wind_bearing":319,"temperature":20.8,"templow":16.8,"wind_speed":20.9,"precipitation":4.5,"humidity":70}]}}

Note: I use a different weather provider, so my data object will not match yours, and does not have precipitation_probability.

This is a wonderful JSON data structure, and if we look at just the first part, an array of objects nested in a two-layer object:

{
  "weather.forecast_home": {
    "forecast": [
      {
        "condition": "rainy",
        "datetime": "2024-07-16T11:00:00+00:00",
        "wind_bearing": 307.9,
        "temperature": 21,
        "templow": 14.7,
        "wind_speed": 19.1,
        "precipitation": 1.8,
        "humidity": 65
      },

Your simple question has multiple facets.

First:
Since the top object key contains a special character, reference using JavaScript is certainly payload["weather.openweathermap"].forecast[0].precipitation, however if you are using JSONata (J: expression) the reference would be

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

or (payload.**.precipitation)[0] works just as well.

Second:
Weather comes now from a service call, and we can use this service in both Node-RED and in Home Assistant. If you are looking to get the data in Node-RED and then put it back into Home Assistant (in an input_helper) why not just do this in Home Assistant, using a trigger based automation? No need to involve Node-RED at all!

Third:
Why use an input_helper to store your value? Input based sensors are special in that we (humans) can input stuff by changing their values in the dashboard, but if you are working in Node-RED you can use any of the WebSocket entity (sensor) nodes to create and maintain both the state value and attributes of a sensor of your choice.

If you are going to use the data in HA, then it would be better to do everything in HA.
If you are going to use the data in another Node-RED flow, then why not call the service, get the data, and use it immediately? Is there really a need to save the data anywhere?
If you are going to use the data in Node-RED only, then you can save this in Context, and then read it back again when you want it. Context store is a powerful way to save data without having to save to file or Home Assistant.

A solution:
Assuming that you want to read the weather forecast in Node-RED, and then save it to Home Assistant (so you can access it in HA and also in Node-RED) the following flow might provide you with some ideas.

You will have to change the service call entity_id to your weather forecast, and you can play around with the names I have used in the sensor. The flow gets the weather, uses a bit of JSONata to build a new object with just the bits we want, and then a Sensor entity node to write the precipitation to the state value, and the precipitation probability to an attribute. You can, of course, have two sensor nodes, one for the rain and another for the probability if you wish.

I have also added a save of the ‘rain object’ to flow context. If you plan to use this in Node-RED only (and don’t need the values back in Home Assistant) then you can drop the Sensor node (and the configuration node behind it) completely, and just read back the flow context variable ‘Rain’.

Here is the basic flow

[{"id":"46067599524f8de5","type":"api-call-service","z":"7021f7d89ccb8403","name":"","server":"","version":5,"debugenabled":false,"domain":"weather","service":"get_forecasts","areaId":[],"deviceId":[],"entityId":["weather.forecast_home"],"data":"{\"type\": \"daily\"}","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"results"}],"queue":"none","x":440,"y":2700,"wires":[["c14743f79f67a3de"]]},{"id":"c14743f79f67a3de","type":"change","z":"7021f7d89ccb8403","name":"Extract precipitation","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\"rain_now\": (payload.**.precipitation)[0], \"rain_chance\": (payload.**.precipitation_probability)[1]}","tot":"jsonata"},{"t":"set","p":"Rain","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":670,"y":2700,"wires":[["1f834b10f5498869"]]},{"id":"1f834b10f5498869","type":"ha-sensor","z":"7021f7d89ccb8403","name":"Rain","entityConfig":"1c295f2ff52050b6","version":0,"state":"payload.rain_now","stateType":"msg","attributes":[{"property":"chance","value":"payload.rain_chance","valueType":"msg"}],"inputOverride":"allow","outputProperties":[],"x":850,"y":2700,"wires":[["f2672103f97e8d6f"]],"server":""},{"id":"1c295f2ff52050b6","type":"ha-entity-config","server":"","deviceConfig":"","name":"SC Weather Rain","version":"6","entityType":"sensor","haConfig":[{"property":"name","value":"Weather Rain"},{"property":"icon","value":""},{"property":"entity_picture","value":""},{"property":"entity_category","value":""},{"property":"device_class","value":""},{"property":"unit_of_measurement","value":""},{"property":"state_class","value":""}],"resend":false,"debugEnabled":false}]

I hope this resolves your impasse and gives you some ideas ! Good luck with your watering project.

(I also hope it works too as I can’t fully test it against your data)

Hello Geoff
Thanks for you detailed reply to what was a poorly asked question!
While I did not fully understand your reply’s and go not get to work what you sent.
They did show me the way ahead and I have now fixed my problem.
It would seem I just needed to understand the “Change node” better which you have helped with.

As you have saved me many hours of messing around I am very grateful.

Please allow me you buy you a drink or a coffee.

If you in the Uk I can send a little gift.

Alistair

The Change Node is probably my favourite and most used. There is just so much you can do just with the UI settings, and when using JSONata (J: expression) almost anything can be achieved. Node-RED flows are really just trigger node - change node - action node !

When I first started with Home Assistant and Node-RED I relied very heavily on this forum, reading many posts to find answers to my growing questions, to just learn and try things, or to search for help and augment what can be poor documentation.

I post to (try to) give back to this community a little of the benefit I have already received, and I hope that longer posts to try and explain more will be helpful to others later, not just the original poster. If I have helped you, even a little, then I have succeeded, which is good enough for me. :smiley:

My old ‘maths’ teacher at school always said the ones who asked the “stupid” questions were always the ones who learnt the most and came top in the exams. :wink:

Hello Geoff
Nice of you to support me thanks. It would seem your a little older than me I was 5 when man landed on the moon but am also semi-retired.
I live near Southampton so not a million miles away from you.
I now have had a glass of wine to celebrate my/our breakthrough. Which I have been messing around with for nearly two week between what my wife wants me to do that is.

Once again thanks I really would like to buy you a drink in there anyway I can do that?

Ali

Yes you are on the nail. I predate Sputnik, but only just, and am now fully retired (although I seem to spend as much time messing about with Node-RED stuff as I ever did working).

Kind of you, but really no need. When you have more experience behind you, perhaps you too will begin to post replies to questions, and thus the community will move on. To be honest, the best people to write answers are those who have just found solutions themselves. Best wishes with your project!

1 Like