Help! TypeError: Cannot read properties of undefined (reading 'map')

Hi guys,
I’m quite new in node-red, and i’m trying to adapt an irrigation system to my HA based on weather forecast.

in the function node i have this:

msg.precipitation_probability = msg.weather_entity.attributes.forecast.map(f => parseFloat(f.precipitation_probability)).reduce((p,c) => p + c) / msg.weather_entity.attributes.forecast.length;
return msg;

in the current state node have this:

I’m affraid it is related with my weather card/integration that don’t have forecast.map attribute. if i add a debug function node shows this:

but not sure.

ps: not a big issue but I hid only the name of my location

Any clue how to solve this?

I assume that you wish to apply the map() function to

msg.weather_entity.attributes.forecast

However, msg.weather_entity.attributes does not have a ‘forecast’ key/value in it, and therefore, msg.weather_entity.attributes.forecast will be undefined resulting in your error

The root issue here is that you are attempting to look at the weather_entity (entity) when this is just a target entity and does not carry the actual weather forecast anymore. You need to call the ‘get forecasts’ service against this entity in order to retrieve the current forecast from the returned service call results.

After that, you need to access the returned “weather.forecast_home” object which has a special character in it, so you need the appropriate referencing syntax for this.

After that, you appear to be taking an average (sum / count) of the precipitation_probability. I am not going to question the logic of averaging a probability like this, but there may be easier ways to do this.

I don’t have ‘precipitation_probability’ in my returned weather forecast, but based on ‘precipitation’ which is mm of rain per period, for an hourly weather forecast, the following will return an object with the count of the hours, the total rain over the next 24 hours, the average rain per hour, and the hour periods that it will be raining.

[{"id":"2ade2067a423465b","type":"api-call-service","z":"7021f7d89ccb8403","name":"","server":"ae8c890fd1f09885","version":5,"debugenabled":false,"domain":"weather","service":"get_forecasts","areaId":[],"deviceId":[],"entityId":["weather.forecast_home"],"data":"{\"type\": \"hourly\"}","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"results"}],"queue":"none","x":500,"y":4540,"wires":[["f7c775403e8fd895"]]},{"id":"f7c775403e8fd895","type":"change","z":"7021f7d89ccb8403","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"(\t    $rain:=payload.*.forecast.precipitation;\t    {\"periods\": $count($rain),\t    \"total\": $sum($rain),\t    \"average\": $average($rain)~>$round(3),\t    \"wet_periods\": $count($rain[$>0])}\t)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":740,"y":4540,"wires":[["d1e60d5ca2e90174"]]}]

Please change the service call target weather entity for your own!

Hope that helps.

1 Like

Many thanks.

I hesitated to ask how can I make this part, because it seems quite basic, but I’m also trying to understand the basics of nodered. Apologies.

There is absolutely no need to apologise - Node-RED is challenging to understand, especially at first, and the complexity here comes partly from how Home Assistant works, particularly with the new arrangement and needing to call a service to get the weather forecasts. This confuses a great many of us, even stuff that seems very basic.

I hope that you can get your Node-RED flow to work, and I am sure you will understand it all much better in time.

Good luck with your project.