Weather from the met.no integration has not attribute 'forecast'

I’ve been trying to create a sensor to sum the rainfall for the next 24 hours. For this I’m trying to use the Met.no integration to loop through the “forecast” attribute and sum the rainfall hour by hour, but I’m not having any success. Every code I try gives me an error when implementing the “forecast” attribute that contains the list of the different hours of the forecast. I’ll appreciate any help you can give me. I’ve already looked at a lot of examples and I can’t figure it out.

Here is my code:

- trigger:
    - platform: time_pattern
#      hours: /1
      minutes: /1
    - platform: homeassistant
      event: start
  action:
    - service: weather.get_forecasts
      data:
        type: hourly
      target:
        entity_id: weather.forecast_casa
      response_variable: Pronostico
  sensor:
    - name: Lluvia proximas 24 horas
      unique_id: clima_lluvia_proximas_24_horas
      unit_of_measurement: mm
      icon: mdi:weather-rainy
      state: >
        {% set ns = namespace() %}
        {% set ns.TotalPrecipitacion = 0 %}
        {% for PronosticoPorHora in Pronostico.forecast %}
          {% set ns.TotalPrecipitacion = 50 %}
        {% endfor %}
        {{ ns.TotalPrecipitacion | float(0) | round(2) }}

For this example, if the for loop could read the “forecast” attribute it should return 50 in the variable ns.TotalPrecipitacion, but it always returns 0 since it doesn’t enter the for loop. The error I get is:
Template variable warning: 'dict object' has no attribute 'forecast' when rendering '{% set ns = namespace() %} {% set ns.TotalPrecipitacion = 0 %} {% for PronosticoPorHora in Pronostico.forecast %} {% set ns.TotalPrecipitacion = 50 %} {% endfor %} {{ ns.TotalPrecipitacion | float(0) | round(2) }}'

In another example I made, when I implement the direct reading of the “forecast” attribute I get the error:
Error rendering state template for sensor.lluvia_proximas_24_horas: UndefinedError: 'dict object' has no attribute 'forecast'

Thank you very much for any comments you can give me.

As shown in the Example template sensor using get_forecasts from the Home Assistant docs, you are missing the entity ID of the weather entity:

...
{% for PronosticoPorHora in Pronostico['weather.forecast_casa'].forecast %}
....

The forecast attribute was removed from the state object about 6 month ago, there is no way to access it directly with a state template.

1 Like

Thank you very much for the prompt response. I made the adjustment and now the sensor works perfectly.
I read the example “Example template sensor using get_forecasts” several times, but I didn’t see that point, I was probably so wrapped up in the code that I lost the context. Thanks again.