Template sensor does not update on HA start

After one of recent updates (I did not noticed when exactly this happened) one of my template sensor no longer update on HA restart. It is sensor that reads Weatherbit alerts and presents it in more readible form. Here is the code for sensor:

sensor:
  - platform: template
    sensors:
      weather_alert:
        friendly_name_template: "Weatherbit alert {{state_attr('sensor.weatherbit_weather_alerts','alerts')[0].city_name}}"
        value_template: "{{state_attr('sensor.weatherbit_weather_alerts','alerts')[0].title}}"
        attribute_templates:
          alert: >
            {% if state_attr('sensor.weatherbit_weather_alerts','alerts')[0].title == 'No Weather Alerts'%}
              No Weather Alerts
            {% else %}
              {{state_attr('sensor.weatherbit_weather_alerts','alerts')[0].description.split('english:')[1].split('polski')[0]}}
            {% endif %}
          severity: >
            {{state_attr('sensor.weatherbit_weather_alerts','alerts')[0].severity}}
          color: >
            {% set severity = state_attr('sensor.weatherbit_weather_alerts','alerts')[0].severity %}
            {% if severity == 'Advisory' %}
              "#ffffff"            
            {% elif severity == 'Watch' %}
              "#ffaa2e"
            {% else %}
              "#ff0000"
            {% endif %}

Upon HA restart state of this sensor is unknown (and it also have no attributes):

All template values resolve properly in Template Editor. Probably since weather alert is not updating frequently, it also causes my sensor not to update after resart and stay in unknown mode. In principle sensor works fine, as I can force update by reloading Template Entities (from Configuration->Server Controls) and this causes sensor to update its value:

As there are not too many alerts recently, I can’t verify if sensor would update properly once source data is changing. Is there any solution to get such template sensor to properly update its state after HA restart?

You could automate it:

- id: startup
  alias: 'Startup'
  trigger:
    platform: homeassistant
    event: start
  action:
    service: homeassistant.update_entity
    entity_id: sensor.weather_alert

Thanks Tom! I was actually thinking about similar solution, though it looks a bit strange that only one of my template sensors behavies this way…
Anyhow I implemented this and no success… Difference is that now I see error in log file:

2021-08-07 17:55:28 ERROR (MainThread) [homeassistant.helpers.entity] Update for sensor.weather_alert fails
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 446, in async_update_ha_state
    await self.async_device_update()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 654, in async_device_update
    raise exc
  File "/usr/src/homeassistant/homeassistant/components/template/template_entity.py", line 277, in async_update
    self._async_update()
TypeError: 'NoneType' object is not callable

I thought it might be linked with Weatherbit integration not being ready so I added some delay in automation and even tried to call the service manually… same error :frowning: At the same time reloading template entities afterwards work like charm… I feel lost.

OK, sorted it out… had to rewrite this sensor to new template platform to get it working properly. So I ended with following code:

template:
  - sensor:
      - name: "Weather Alert"
        state: "{{state_attr('sensor.weatherbit_weather_alerts','alerts')[0].title}}"
        attributes:
          alert: >
            {% if state_attr('sensor.weatherbit_weather_alerts','alerts')[0].title == 'No Weather Alerts'%}
              No Weather Alerts
            {% else %}
              {{state_attr('sensor.weatherbit_weather_alerts','alerts')[0].description.split('english:')[1].split('polski')[0]}}
            {% endif %}
          severity: >
            {{state_attr('sensor.weatherbit_weather_alerts','alerts')[0].severity}}
          color: >
            {% set severity = state_attr('sensor.weatherbit_weather_alerts','alerts')[0].severity %}
            {% if severity == 'Advisory' %}
              "#ffffff"            
            {% elif severity == 'Watch' %}
              "#ffaa2e"
            {% else %}
              "#ff0000"
            {% endif %}

The only drawback is that new template platform does not support friendly name to be defined as attribute and seems to take it from sensor name directly, but this is manageable.

how do you define multiple sensors that way?

Do you use ‘-’ before each sensor? or you just right it down as it is?

Yes, entire section starting with ‘- name:’ needs to be repeated.