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
73.23k stars 30.58k forks source link

Cannot refer to own sensor in icon template for a trigger based template sensor #58056

Closed parautenbach closed 2 years ago

parautenbach commented 3 years ago

The problem

When defining a new style template sensor, it isn't possible to self reference the sensor in the icon template. A related issue referring to the old style has been logged and resolved before. By not allowing this, the only alternative is to create an intermediate helper sensor, which seems unnecessary.

What is version of Home Assistant Core has the issue?

core-2021.9.7

What was the last working version of Home Assistant Core?

No response

What type of installation are you running?

Home Assistant Core

Integration causing the issue

Template

Link to integration documentation on our website

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

Example YAML snippet

template:
  - trigger:
    - platform: event
      event_type: amcrest
      event_data:
        event: CallNoAnswered
        payload:
          action: Start
    binary_sensor:
      name: Doorbell Rang
      icon: "{{ 'mdi:bell-ring-outline' if is_state('binary_sensor.doorbell_rang', 'on') else 'mdi:bell-outline' }}"
      state: true
      auto_off: 5  # seconds


### Anything in the logs that might be useful for us?

_No response_

### Additional information

_No response_
probot-home-assistant[bot] commented 3 years ago

template documentation template source (message by IssueLinks)

probot-home-assistant[bot] commented 3 years 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! (message by CodeOwnersMention)

parautenbach commented 2 years ago

I believe this is still an issue. Any response (comments) on this issue would be appreciated.

emontnemery commented 2 years ago

The sensor in your example is not a "new style template sensor", it's a trigger-based template sensor: https://www.home-assistant.io/integrations/template/#trigger-based-template-sensors Such sensors will only update when the trigger fires, as explained in the documentation.

You need to make the sensor trigger on its own state.

parautenbach commented 2 years ago

You need to make the sensor trigger on its own state.

Do you mean like this?

template:
  - trigger:
    - platform: event
      event_type: amcrest
      event_data:
        event: CallNoAnswered
        payload:
          action: Start
    - platform: state
      entity_id: binary_sensor.doorbell_rang
      to: "on"
    binary_sensor:
      name: Doorbell Rang
      icon: "{{ 'mdi:bell-ring-outline' if is_state('binary_sensor.doorbell_rang', 'on') else 'mdi:bell-outline' }}"
      state: true
      auto_off: 5  # seconds
emontnemery commented 2 years ago

@parautenbach yes, something like that.

You probably need a trigger also when changing to the off state. In the next release of Home Assistant (2022.2) you can instead do like this to trigger on any state change (but not attribute change)

    - platform: state
      entity_id: binary_sensor.doorbell_rang
      to: ~
parautenbach commented 2 years ago

You probably need a trigger also when changing to the off state.

Indeed! Thanks for catching that.

I'm thinking of submitting a change/example for the docs too to make this way of making the icon dynamic a bit more apparent.