Add 'initial' and 'restore' variables to (event) trigger-based template sensors

EDIT: it has actually been delivered in 2022.5, see Restore state of trigger-based template sensor by emontnemery · Pull Request #69344 · home-assistant/core · GitHub. Still no initial value, but it is actually less of a concern (to me). It even actually started to be addressed already in 2022.4.

=======================================================================

The trigger-based template sensors are a great addition, improving a lot e.g. event-based sensors (no need for helpers and automations any more).
However, when HA reboots, sensors may remain unavailable, and their attributes absent, for a long time before a first event will populate these info.
An improvement would be to add variables such as initial and restore, the same way other core integrations have (e.g. counter).
We could then control whether we want a trigger-based template sensor to restore the last known values (state and attributes) when Home Assistant starts or its initial values when reloaded, for instance.

This is already easily resolved by adding these two triggers:

  - platform: homeassistant
    event: start
  - platform: event
    event_type: event_template_reloaded

The sensor’s template will be evaluated on startup and whenever Template Entities are reloaded.

Your solution does not apply to templates triggered by events, of which data are taken to feed the state and attributes.

My suggestion applies to your original complaint :

As for “templates triggered by events”, that’s a very specific use-case and, until Trigger-based Template Sensors were recently introduced, completely unsupported by traditional Template Sensors.

Effectively, your Feature Request is to support a specific kind of trigger, namely an Event Trigger because the template cannot be evaluated at any time (like startup or reload) other than when the event actually occurs.

However, the Feature Request introduces concepts contrary to a Template Sensor’s fundamental behavior. The purpose of a Template Sensor is to evaluate its template to compute a value. The value is always calculated, never stored or initialized.

Anyway, good luck convincing the development team to modify the architecture of the Trigger-based Template Sensor.

An example I have in use (idea came from this thread, from @123 - thanks a lot):

template:
  - trigger:
    - platform: homeassistant
      event: start
    - platform: state
      entity_id:
        - sensor.a_sensor
    sensor:
      - name: another_sensor
        state: >
          {% if trigger.platform == 'homeassistant' %}
            0.0
          {% else %}
            {{ trigger.to_state.state | float(0) - trigger.from_state.state | float(0) }}
          {% endif %}
        unit_of_measurement: mm
        icon: mdi:weather-rainy 
        state_class: measurement

So I registered two events, one is the restart and the other one is the data I am really interested in. I then query the platform of the event to distinguish between them. I use the restart event for initializing and the state event for my logic.

See here which data is available for the trigger: Automation Trigger Variables - Home Assistant

I suggest you change the subtraction to this:

{{ trigger.to_state.state | float(0) - trigger.from_state.state | float(0) }}
1 Like