Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Icon for template sensor rendered from old state #60382

Closed
akloeckner opened this issue Nov 25, 2021 · 2 comments
Closed

Icon for template sensor rendered from old state #60382

akloeckner opened this issue Nov 25, 2021 · 2 comments

Comments

@akloeckner
Copy link
Contributor

akloeckner commented Nov 25, 2021

The problem

In my template sensor, I want to select the icon based on the sensors state. But the icon template only knows the previous state, forcing me to repeat all state logic in the icon template.

What version of Home Assistant Core has the issue?

2021.8.3

What was the last working version of Home Assistant Core?

No response

What type of installation are you running?

Home Assistant Container

Integration causing the issue

Template

Link to integration documentation on our website

No response

Example YAML snippet

template:
- trigger:
  - platform: time_pattern
    seconds: '*'
  sensor:
  - name: Test icon
    state: >
      {{ 'mdi:flash' if as_timestamp(now())|int %2 == 0 else 'mdi:clock' }}
    icon: >
      {{ states('sensor.test_icon') }}

This is most simplified, obviously. My real use case has a more complex state template.

Anything in the logs that might be useful for us?

No response

Additional information

This might be circumventable with variables being allowed in the template integration, like in scripts.

It can also be circumvented with an additional trigger:

- trigger:
  - platform: time_pattern
    seconds: '*'
  - platform: state
    entity_id: sensor.test_icon
@probot-home-assistant
Copy link

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!
(message by CodeOwnersMention)


template documentation
template source
(message by IssueLinks)

@emontnemery
Copy link
Contributor

This is clearly explained in the documentation https://www.home-assistant.io/integrations/template/#trigger-based-template-sensors

Trigger-based entities do not automatically update when states referenced in the templates change. This functionality can be added back by defining a state trigger for each entity that you want to trigger updates.

In your case, you should add a state trigger triggering on sensor.test_icon

There's a new feature in Home Assistant Core 2012.12 which prevents the trigger from firing when an attribute updates: #59713

@github-actions github-actions bot locked and limited conversation to collaborators Dec 30, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants