core: persistent trigger based sensors

The problem

How to make trigger based sensors value persistent and survive restart? Trigger based sensors always have state unknown until next trigger.

This sensor defined below was not directly possible without trigger based sensors because it needs access to previous and current state to calculate the difference.

It’s always an option to push event to mqtt and make sensor out of that but there should be other way, right?

What is version of Home Assistant Core has the issue?

core-2021.4.6

What was the last working version of Home Assistant Core?

No response

What type of installation are you running?

Home Assistant OS

Integration causing the issue

No response

Link to integration documentation on our website

https://www.home-assistant.io/integrations/template/#configuration-for-trigger-based-template-sensors

Example YAML snippet

template:
  - trigger:
      - platform: state
        entity_id: cover.living_room_shutters
    sensor:
      - name: "Living room shutters tilt"
        unique_id: living_room_shutters_tilt
        icon: mdi:math-integral
        state: >-
            {%set diff=(trigger.to_state.attributes.current_position - trigger.from_state.attributes.current_position) %}
            {%set curr=states("sensor.living_room_shutters_tilt")|int%}
            {%if curr > 0 %}
            {%else%}{%set curr=0%}
            {%endif%}
            {%if diff <= -3 %}{%set tgt=0%}
            {%elif diff >= -2 and diff <= 2%}{%set tgt=(curr + diff*30)%}
            {%elif diff >= 3 %}{%set tgt=90%}
            {%else%}{%set tgt=0%}
            {%endif%}
            {{ tgt|int }}
        unit_of_measurement: "°"

Anything in the logs that might be useful for us?

No response

Additional information

No response

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 19 (2 by maintainers)

Most upvoted comments

Following, since I have same kind of issue here, I want to trigger an sensor update once a day. This is working well except:

  • upon restart. The start event could do the trick, but I’m sure there are some use cases where people don’t want their template entity state to be updated on start (maybe for daily consumption sensor ?). I think there could be an option where you indicate if you want your entity state persistent (upon restart). – If yes, the state will have the last known value before restart and will update when trigger matches – if no, then your entity will have “unknown” value, but you can still add start even to the trigger 😃

  • upon template entity reload (through the UI), my entities are losing their states until the next trigger happens. (I’m currently migrating from old template to new template style, so I’m reloading a lot right now). I have no idea here 😞, you might want to update it after the reload, but there won’t be any trigger data then…

Honestly, it sounds quite hard to have something that would fit every use cases

EDIT: for template entity reload, there is an event event_type event_template_reloaded