Updating a trigger-based template on startup

How can I get a trigger-based template to update shortly after Home Assistant restarts?

I am using the new Trigger-Based Template Sensors to display data based on the attributes of a command line sensor. The underlying data changes infrequently (normally only weekly), so the command line sensor has scan_interval set to poll only every few days. However, the templates do some time-based manipulation of the data, and are supposed to change daily. Using the old-style configuration, I had an automation set up to update the templates each morning, but I thought that the new-style triggered templates would be a cleaner way of achieving the same end.

Unfortunately the command line sensor is relatively slow (10-20 seconds to update), and returns Unknown when the system starts. My template sensors don’t then update when the data is ready, so end up saying Unknown too, up until their next time trigger, which could be nearly a day away. Is there any way I can get them to automatically update when the command line sensor is ready after a restart?

Here is a (thinned-down) version of my code for the templates:

- trigger:
    - platform: time
      at: '00:10:00'
    - platform: state
      entity_id: sensor.my_sensor
    - name: 'Template1'
      state: >
        {% state_attr('sensor.my_sensor', 'attr1') %}
    - name: 'Template2'
      state: >
        {% state_attr('sensor.my_sensor', 'attr2') %}

I’ve not included the full code for the state templates, as they’re rather complicated but seem to work fine: they render OK either if the time trigger is reached, or if I manually edit the state of the command line sensor using the Developer console. It’s not that something in the templates is failing to render - Home assistant isn’t trying to render them at all. I had hoped that the state trigger would cause the templates to update after a restart once the command line sensor left the Unknown state, but it doesn’t seem to (perhaps because the proper state is the same as it was before restart?)

sensor.my_sensor is the command line sensor. Once it has successfully updated, its state holds a JSON formatted string, with some of the key data from the JSON present as attributes. It is configured like this:

- platform: command_line
  command: '/home/homeassistant/scripts/fetch_data.py'
  name: 'My Sensor'
    - attr1
    - attr2
  scan_interval: 84000
  command_timeout: 20
  value_template: '{{ value_json }}'

The problem seems to be that when the sensor.my_sensor gets populated after a restart, it doesn’t generate a state change to prompt the templates to update, so they just sit there saying unknown.

The documentation mentions the issue and suggests using an is_state() test in the state template. That doesn’t help, because the template isn’t being rendered (I’ve tried using a plain string as the template to check this).

Any ideas?

I’m running Home Assistant Core v 2021.6.4 running in a venv on a RPi4.

Add another trigger:

  - platform: homeassistant
    event: start

You can also add this one to ensure it gets updated whenever Template Entities are reloaded.

  - platform: event
    event_type: event_template_reloaded

Thanks @123! The homeassistant trigger solved my original problem, but the event_template_reloaded trigger proved to be necessary too. Without it, the templates worked fine on a full restart, but got reset to unknown when I just reloaded Template Entities.