Trigger based template sensor

Home Assistant should not update trigger based sensors on restart. Currently the sensors are updated to “unknown” on restart.

A workaround is to use a helper and an automation to update the value based on the wanted trigger but it should not be neccessary.

I have not asked the sensor to be updated on restart and I expect that it keeps the value regadless of restarts until the next trigger.

(I know that there are several threads about this issue.)

Not sure what you are talking about. The docs say this:

The state, including attributes, of trigger-based sensors and binary sensors is restored when Home Assistant is restarted. The state of other trigger-based template entities is not restored.

And that is exactly what happens for all my triggered template sensors.

The state was unknown because HA was off and not monitoring it. Then the state is changed to the last known value.

This is how home assistant works at a very deep level. It is unlikely to be changed.

What issue is it causing?

There’s probably a way to solve it.

I have a template sensor that computes a value once a day. If I restart Home Assistant, that value is lost. I don’t want to recompute on restart as the value will change during the day due to source sensor changes.

It does not re-evaluate triggered template sensors, it restores the state from before the restart.

Show your sensor configuration.

This is used to calculate remaining km/day on my car insurance.
Inputs:

  • Date for insurance renewal
  • Max km allowed on insuriance
  • Current odometer km

This sensor is unknown after a restart.

- trigger:
      - platform: time_pattern
        hours: "23"
        minutes: "55"
    sensor:
      - name: daily_km_remaining
        unique_id: car.daily_km_remaining
        state_class: measurement
        unit_of_measurement: "km"
        state: >
          {% set days = (((states.input_datetime.bilforsikring_dato.attributes.timestamp) - as_timestamp(now())) / 3600 / 24) | int %}
          {% set km = states('input_number.bilforsikring_km') | float - states('sensor.id_4_pro_performance_odometer') | float %}
          {{(km/days)|round(1) }}