home-assistant / core

:house_with_garden: Open source home automation that puts local control and privacy first.
https://www.home-assistant.io
Apache License 2.0
74.02k stars 31.05k forks source link

Template entities do not restore an unavailable state #125548

Open wbyoung opened 2 months ago

wbyoung commented 2 months ago

The problem

Trigger based sensor entities restore their state, but when restored from an unavailable state (or when newly created), they result in a state of unknown. The unknown state makes sense for when they're newly created, but I'm not sure it makes sense when they're retored.

Here's an example that can be debugged by triggering events:

- trigger:
  - platform: event
    event_type: _debug_template_state_issues
  sensor:
    - unique_id: debug_template_state_issues
      name: Debug Template State Issues
      unit_of_measurement: km/h
      state_class: measurement
      state: >
        {{ trigger.event.data.state }}
      availability: >
        {{ trigger.event.data.get('availability', true) }}

You can set the state by firing the event _debug_template_state_issues with data:

state: 1

When you reload template entities, the state is restored as expected.

You can set this to unavailable by firing the event _debug_template_state_issues with data:

state:
availability: false

When you reload template entities, the state becomes unknown which is not what it was before.

Expected Behavior

I believe they be restored to an unavailable state (and not render the state or availability templates).

If you try to fix this to fix by adding a trigger for event_template_reloaded, you still get a state change when template entities are reloaded from unknown to unavailable which isn't the most desirable either, so this may be a workaround for some, but also just creates other complications.

- trigger:
  - platform: event
    event_type: _debug_template_state_issues
  - platform: event
    event_type: event_template_reloaded
    variables:
      available: false
  sensor:
    - unique_id: _debug_template_state_issues
      name: Debug Template State Issues
      unit_of_measurement: km/h
      state_class: measurement
      state: >
        {{ trigger.event.data.state if available|default(true) else -1 }}
      availability: >
        {{ trigger.event.data.get('availability', true) and available|default(true) }}

Tangentially Related

This is only slightly related to https://github.com/home-assistant/core/issues/117397 for me because I was actually trying to add the availability option in order to work around the fact that returning none has been giving me errors.

What version of Home Assistant Core has the issue?

core-2024.9.1

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

Template

Link to integration documentation on our website

https://www.home-assistant.io/integrations/template/#availability

Diagnostics information

No response

Example YAML snippet

No response

Anything in the logs that might be useful for us?

No response

Additional information

No response

home-assistant[bot] commented 2 months ago

Hey there @phracturedblue, @tetienne, @home-assistant/core, mind taking a look at this issue as it has been labeled with an integration (template) you are listed as a code owner for? Thanks!

Code owner commands Code owners of `template` can trigger bot actions by commenting: - `@home-assistant close` Closes the issue. - `@home-assistant rename Awesome new title` Renames the issue. - `@home-assistant reopen` Reopen the issue. - `@home-assistant unassign template` Removes the current integration label and assignees on the issue, add the integration domain after the command. - `@home-assistant add-label needs-more-information` Add a label (needs-more-information, problem in dependency, problem in custom component) to the issue. - `@home-assistant remove-label needs-more-information` Remove a label (needs-more-information, problem in dependency, problem in custom component) on the issue.

(message by CodeOwnersMention)


template documentation template source (message by IssueLinks)