How to sum values from a list in a attribute

I get these values from another service/sensor

temperature: 16.5
temperature_unit: °C
humidity: 57
pressure_unit: hPa
wind_bearing: 55
wind_speed: 3.24
wind_speed_unit: km/h
visibility_unit: km
precipitation_unit: mm
radar_coverage: ok
has_precipitation: false
radar_online: true
forecast_json: [{"temperature": 16.5, "precipitation": 0.0, "relative_humidity": 57.4, "wind_bearing": 55.0, "wind_speed": 0.9, "wind_speed_of_gust": 3.9, "datetime": "2025-06-08T12:25:00Z", "condition": "cloudy"}, {"temperature": null, "precipitation": 0.0, "datetime": "2025-06-08T12:30:00Z"}, {"temperature": null, "precipitation": 0.0, "datetime": "2025-06-08T12:35:00Z"}, {"temperature": null, "precipitation": 0.0, "datetime": "2025-06-08T12:40:00Z"}, {"temperature": null, "precipitation": 0.0, "datetime": "2025-06-08T12:45:00Z"}, {"temperature": null, "precipitation": 0.0, "datetime": "2025-06-08T12:50:00Z"}, {"temperature": null, "precipitation": 0.0, "datetime": "2025-06-08T12:55:00Z"}, {"temperature": null, "precipitation": 0.0, "datetime": "2025-06-08T13:00:00Z"}, {"temperature": null, "precipitation": 0.0, "datetime": "2025-06-08T13:05:00Z"}, {"temperature": null, "precipitation": 0.0, "datetime": "2025-06-08T13:10:00Z"}, {"temperature": null, "precipitation": 0.0, "datetime": "2025-06-08T13:15:00Z"}, {"temperature": null, "precipitation": 0.0, "datetime": "2025-06-08T13:20:00Z"}, {"temperature": null, "precipitation": 0.0, "datetime": "2025-06-08T13:25:00Z"}, {"temperature": null, "precipitation": 0.0, "datetime": "2025-06-08T13:30:00Z"}, {"temperature": null, "precipitation": 0.0, "datetime": "2025-06-08T13:35:00Z"}, {"temperature": null, "precipitation": 0.0, "datetime": "2025-06-08T13:40:00Z"}, {"temperature": null, "precipitation": 0.0, "datetime": "2025-06-08T13:45:00Z"}, {"temperature": null, "precipitation": 0.0, "datetime": "2025-06-08T13:50:00Z"}, {"temperature": null, "precipitation": 0.0, "datetime": "2025-06-08T13:55:00Z"}, {"temperature": null, "precipitation": 0.0, "datetime": "2025-06-08T14:00:00Z"}, {"temperature": null, "precipitation": 0.0, "datetime": "2025-06-08T14:05:00Z"}, {"temperature": null, "precipitation": 0.0, "datetime": "2025-06-08T14:10:00Z"}, {"temperature": null, "precipitation": 0.0, "datetime": "2025-06-08T14:15:00Z"}]
longitude: 13.0774
latitude: 55.5921
attribution: Weather forecast from met.no, delivered by the Norwegian Meteorological Institute.
friendly_name: Met.no Nowcast: RegnPrognos_Mö
supported_features: 2

I want to build a sensor that sums the precipitation in the attribute(‘forecast_json’)

I can’t get it to work

Sorry, I misread your request. :man_facepalming:

You want a sum of the precipitation. Ignore what I said previously about temperature.


Test the following template in the Template Editor (Developer tools → Template). Be sure to replace sensor.your_sensor with the entity_id of your actual

{{ state_attr('sensor.your_sensor', 'forecast_json')
   | map(attribute='precipitation') | sum }}

If it produces the correct result, you can use in a Template Sensor helper.

Thanks,
but it didnt work.

I get the error: ‘UndefinedError: ‘str object’ has no attribute ‘precipitation’’

If it is a string you’ll have to convert it from json:

{{ state_attr('sensor.your_sensor', 'forecast_json')
   | from_json | map(attribute='precipitation') | sum }}

Thanks,
that fixed it!

The value of the forecast_json attribute was a string, even though, based on what you had posted, it had the appearance of a list of dictionaries. The error message made it clear that it was actually a string.

The string was in JSON format so, as mekaneck suggested, the template needed to use the from_json filter to convert it from a JSON string.

Keep that in mind, should you ever need to extract a value in a similar situation.