Enhance Trigger-based Template Sensors

UPDATE

My Feature Request is unnecessary; the requested event already exists. It’s called:

event_template_reloaded

If you want your Trigger-based Template Sensors to compute their state value whenever Template Entities are reloaded, simply add the following Event Trigger:

  - platform: event
    event_type: event_template_reloaded

Without that, they are likely to report unknown until they are triggered.


Request

Create a new event that reports when Template Entities have finished loading. Then it could be used by Trigger-based Template Sensors to compute a value on loading (which is something non-Trigger-based Template Sensors already do).

    - platform: event
      event_type: template_entities_reloaded

Justification

I believe there is a small deficiency in the current behavior of Trigger-based Template Sensors: when first defined and loaded, their state value is unknown.

By design, a Trigger-based Template Sensor computes its state value only when one of its triggers fires. However, until that happens, its state is unknown.

I know of no way to mitigate this using currently available triggers. For example, this Trigger-based Template Sensor will report unknown until either the Time Pattern Trigger fires or Home Assistant is restarted.

template:
  - trigger:
    - platform: time_pattern
      hours: '/1'
    - platform: homeassistant
      event: start
    sensor:
    - name: Contrived Example
      state: '{{ now() }}'

A seemingly obvious addition would be an Event Trigger that triggers when Template Entities are reloaded via call_service.

template:
  - trigger:
    - platform: event
      event_type: call_service
      event_data:
        domain: template
        service: reload
    - platform: time_pattern
      hours: '/1'
    - platform: homeassistant
      event: start
    sensor:
    - name: Contrived Example
      state: '{{ now() }}'

That should now trigger whenever Template Sensors are reloaded, right? Well no, because the Template Sensor can’t “hear” anything while it’s in the process of being reloaded.

It seems to me an event is needed to indicate when Template Entities have finished loading. Perhaps something that looked like this:

    - platform: event
      event_type: template_entities_reloaded

Then this configuration would ensure the Trigger-based Template Sensor reports a computed state value at the moment it is loaded.

template:
  - trigger:
    - platform: event
      event_type: template_entities_reloaded
    - platform: time_pattern
      hours: '/1'
    sensor:
    - name: Contrived Example
      state: '{{ now() }}'