#Trigger-based template_sensor isn't restored on restart

1 messages · Page 1 of 1 (latest)

civic zenith
#

Hi,
I have this template sensor that is populated by a script. It is meant to store a specific amount of seconds that the script calculates. Anyway, here's my template.yaml:

- trigger: 
    - platform: event
      event_type: thisshouldnevertrigger
  sensor:
    - name: "Orange Duration"
      unique_id: 1723585496000
      icon: "mdi:sort-clock-descending-outline"
      state: None
      unit_of_measurement: s
      state_class: measurement
      attributes:
        caller: None

The attribute caller restores without fail, but the state (which is numeric) is set to unknown upon both restart and template entities reload.
What's the issue here?

The HA docs say: «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.»

ivory sun
#

you are setting the state to None

#

but HA expects a numeric state, as you have a unit of measurement and state_class: measurement

#

the state none on a sensor for which a numeric state is expected is translated to unknown

civic zenith
#

No, my issue isn't that None converts to unknown. The issue is that the template entity should not even re-evaluate at this point because the trigger didn't fire. This is about restoring the entity's state from before the restart.

ivory sun
#

but how are you setting the state?

#

there is no state template

civic zenith
#

Hi,
I have this template sensor that is populated by a script.

civic zenith
ivory sun
#

what kind of script, not a standard Home Assistant script as setting the state of a sensor is not something which is supported by default

civic zenith
#

okay, but that is not what this thread is concerned about

ivory sun
#

Look, I'm trying to help you here, but I really don't like how you're acting on my help

#

so I guess I'm out 🙂

civic zenith
#

Okay, sorry if I reacted in a bad way

#

I feel like my issue is not what you were concerned about

#

instead you questioned my setup

ivory sun
#

I think you are doing something which might not be supported by the trigger based template sensor setup

#

But instead of using some kind of pyscript, why not actually send the values you want in an event, and then let the template sensor use the values sent in the event.

#

Then it will actually restore on restart

civic zenith
#

thank you for your concern. I can assure you the way I set up the script to populate the sensor works fine and the sensor values show up in my dashboard. What I'm trying to find out here is why the sensor doesn't restore on restart though.

ivory sun
#

because the way you are writing the state is probably not stored in the recorder, and therefor not restored

#

are you actually storing this sensor in recorder?

civic zenith
civic zenith
ivory sun
#

I understand the values are calculated, but you can send a manual event in a Home Assistant script.
This is how I would do it

# in home assitant script
sequence:
  - event: thisshouldnevertrigger
    event_data:
      state: "{{ some complicated calculation which results in a number }}"
      caller: "{{ another complicated calculation which results in a number }}"

# template sensor:
- trigger: 
    - platform: event
      event_type: thisshouldnevertrigger
  sensor:
    - name: "Orange Duration"
      unique_id: 1723585496000
      icon: "mdi:sort-clock-descending-outline"
      state: "{{ trigger.event.data.state }}"
      unit_of_measurement: s
      state_class: measurement
      attributes:
        caller: "{{ trigger.event.data.caller }}"
civic zenith
#

Ah, I didn't know it was possible to send events manually. I will look into this, thank you