Template sensor stops working when updating HA Core

Hi,

I have the following sensor which has been working for months (probably not the greatest way to do it but Im amateur). Currently on version 2024.3.1 but when updating to latest version the sensor no longer returns any values. How can I find out whats causing the problem? Ive restored a backup so working again for now.

Thanks

  - platform: template
    sensors:
        wiser_forecast:
          friendly_name: "Wiser Forecast"
          value_template: >
            {% set time = state_attr('weather.openweathermap', 'forecast')[0]['datetime'] %}
           
            {% if (( time | as_datetime |as_local - now()).seconds / 60) |int >=120 %}
    
            {% set temp = (state_attr('weather.openweathermap', 'temperature') | float + state_attr('weather.openweathermap', 'apparent_temperature') | float) /2 %}
            {% else %}
            
            {% set temp = (state_attr('weather.openweathermap', 'forecast')[0]['temperature'] | float + state_attr('weather.openweathermap', 'apparent_temperature') | float) /2 %}

            {% endif %}
   
            
            {% if temp <= states('input_number.winter_temp') | float %}
            Winter
            {% elif  temp  > states('input_number.winter_temp') | float and  temp  <= states('input_number.autumn_spring_temp') | float %}
            {% set month = now().month %}
            
            {% if month >1 and month <=6 %}
            {% set temp2 = states('input_number.winter_temp') | float  + (states('input_number.autumn_spring_temp') | float - states('input_number.winter_temp') | float )/2  %}

            {% if temp <= temp2 %}
            Early Spring
            {% else %}
            Spring
            {% endif %}
            
  
            {% else %}
            {% set temp2 = states('input_number.winter_temp') | float  + (states('input_number.autumn_spring_temp') | float - states('input_number.winter_temp') | float )/2  %}

            {% if temp <= temp2 %}
            Autumn
            {% else %}
            Early Autumn
            {% endif %}
            
            {% endif %} 
            {% else %}
            Summer
            {% endif %}
          attribute_templates:
            Actual_Temp:  "{{ state_attr('weather.openweathermap', 'temperature') }}"
            Feels_Like_Temp: "{{state_attr('weather.openweathermap', 'apparent_temperature')}}"
            Forecast_Temp: " {{ state_attr('weather.openweathermap', 'forecast')[0]['temperature'] }}"
            Wiser_Forecast_Temp: >
                {% set diff = state_attr('weather.openweathermap', 'temperature') | float - state_attr('weather.openweathermap', 'apparent_temperature') | float %}
                {% set temp = (state_attr('weather.openweathermap', 'forecast')[0]['temperature'] | float +  state_attr('weather.openweathermap', 'apparent_temperature')) /2 %}
                {{ temp | round(1) }}
            Temp_From: >
                {% set time = state_attr('weather.openweathermap', 'forecast')[0]['datetime'] %}
                {% if (( time | as_datetime |as_local - now()).seconds / 60) |int >=120 %}
                Feels Like Temp
                {% else %}
                Wiser Forecast
                {% endif %}

There was a long standing change for the weather providers. The weather entity doesn’t contain a forecast attribute anymore, you’d need to use “weather.get_forecasts”.

Please do a search for “weather forecasts”, as this has been discussed already in multiple threads.

Like this one here:

or this one

Thanks!

Looks like that might be too much for me to figure out. Like I said Im relatively new to HA so dont think Id be able to get that working. Guess I just wont upgrade for as long as possible.

Thanks for the reply

Looking at those or a little bit of searching and you might find some that has shared what you want exactly. I see hundreds of examples here. A search in your search engine like:

site:home-assistant.io weather forcast

Will find a lot to start, tweak the search from there.

Stopping your update cycle cannot end well. You can see it has already broke your system.

Not updating isn’t really an option, as you’ll likely run into problems, rather sooner than later, and you’re missing out on a lot of cool new functions! HA is making big steps in the last months, and will do so in the future. :slight_smile:

So, no, that’s not an option! :slight_smile:

However, where did you even get that code for a template sensor? It seems a little “strange” on a first glance. Strange in the best possible way! :slight_smile:

Maybe you give us an idea, what you really do with this sensor, so we can find an alternative for you. :slight_smile:

From what I see, this is a season sensor, depending on some temperatures. And at the end there are some sensors as well. These are strange as well, as in one sensor something gets calculated, but isn’t used…

Anyway, please give us a good idea, for what you need this, and especially where you use this! Maybe some things in that template sensor aren’t even necessary.

Ok so I have managed to get it working but now I have another problem.

This is part of the output from the weather.get_forecasts action:

weather.pirateweather:
forecast:
- datetime: “2024-08-21T16:00:00+00:00”
condition: cloudy
wind_bearing: 243
precipitation_probability: 0
cloud_coverage: 90
uv_index: 1.64
temperature: 19.4
apparent_temperature: 14.4
dew_point: 10
pressure: 1014.43
wind_gust_speed: 36.5
wind_speed: 25.78
precipitation: 0
humidity: 55
- datetime: “2024-08-21T17:00:00+00:00”
condition: cloudy
wind_bearing: 233
precipitation_probability: 0
cloud_coverage: 100
uv_index: 1.87
temperature: 19.1
apparent_temperature: 14.1
dew_point: 10.9
pressure: 1014.02
wind_gust_speed: 37.44
wind_speed: 27
precipitation: 0
humidity: 59
- datetime: "2024-08-21T18:00:00+00:

The problem I have is the forecast is showing data from earlier (i.e the time now is 6pm but the forecast is showing data from 4pm).

How would I be able to grab parts of the forecast for future times? So now for example, I would want it to grab the data for 1 hour from now (what ever that may be) so now that would be the 7pm data.

Hope that makes sense