Change of icons via YAML code

I have written this script to count the open doors and windows. Counting does work, but the selection of the appropriate icon does’nt work. What is the mistake I made?

Many thanks for your help.

Number open doors and windows

  • platform: template
    sensors:
    window_count:
    value_template: >-
    {{ states.binary_sensor | selectattr(‘state’, ‘eq’, ‘on’)
    | selectattr(‘attributes.device_class’, ‘eq’, ‘window’)
    | list | count }}
    icon_template: ‘{% if window_count | int > 0 %}mdi:window-open-variant{% else %}mdi:window-closed-variant{% endif %}’
    door_count:
    value_template: >-
    {{ states.binary_sensor | selectattr(‘state’, ‘eq’, ‘on’)
    | selectattr(‘attributes.device_class’, ‘eq’, ‘door’)
    | list | count }}
    icon_template: ‘{% if door_count | int > 0 %}mdi:door-open-variant{% else %}mdi:door-closed-variant{% endif %}’

Welcome! Couple of things:

  1. Its easier if you format your code, have a look at point 11 here:
  1. You’re using the legacy format to define your sensor (see Template - Home Assistant) - I’d consider switching it to the lastest standard (see Template - Home Assistant)

  2. to your question; in the icon_template you need reference the full name of the sensor, so instead of
    ... door_count | int > 0 ...
    you need
    ... states('sensor.door_count') | int > 0 ...

We must note that the legacy notation is not deprecated and still works.
“Switching” to the new notation is just a matter of a user’s desire.
There are some new things came up with a new notation (“trigger”), there are some things which are not supported anymore (“friendly_names”).

1 Like

Your solution also doesn’t work.

Who can help me?

Can you post your latest configuration?

Here is the current configuration.

#Loads default set of integrations. Do not remove.
default_config:

Text to speech

tts:

  • platform: google_translate

Yaml File Orte

automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml
sensor: !include sensors.yaml
template: !include templates.yaml

http:
base_url: https://myhomeassistantgm.duckdns.org:8123
ssl_certificate: /ssl/fullchain.pem
ssl_key: /ssl/privkey.pem
cors_allowed_origins:
- https://google.com
- https://www.home-assistant.io
ip_ban_enabled: true
login_attempts_threshold: 3

lovelace:

########### Alarm Control Panel ###########
alarm_control_panel:

  • platform: manual
    name: Haus Alarm
    code: 9257
    code_arm_required: false
    arming_time: 0 # The time of the ‘arming’ state before affecting a state change
    delay_time: 0 # The time of the ‘pending’ state before triggering the alarm
    trigger_time: 00 # The time of the ‘triggered’ state in which the alarm is firing
    disarm_after_trigger: false
    disarmed:
    trigger_time: 0
    armed_night:
    arming_time: 0
    delay_time: 0
    armed_away:
    arming_time: 0
    delay_time: 0

########## Custom Panels ############
panel_custom:

  • name: Supervisor
    url_path: hassio/system
    sidebar_title: Supervisor
    sidebar_icon: mdi:home-assistant
    module_url: /local/panel-redirect.js

Sorry, I meant the configuration for the sensor where you say the icon’s aren’t updating :slight_smile:

#### Number open doors and windows ####
- platform: template 
  sensors:
    window_count:
        value_template: >-
          {{ states.binary_sensor | selectattr('state', 'eq', 'on') 
          | selectattr('attributes.device_class', 'eq', 'window') 
          | list | count }}
        icon_template: "mdi:window-{{ 'open' if this.state is defined and this.state > 0 else 'closed' }}-variant"
    door_count:
        value_template: >-
          {{ states.binary_sensor | selectattr('state', 'eq', 'on') 
          | selectattr('attributes.device_class', 'eq', 'door')
          | list | count }} 
        icon_template: "mdi:door-{{ 'open' if this.state is defined and this.state > 0 else 'closed' }}-variant"

Hi petr,
thank you for your help. Unfortunately it doesn’t work. At the moment I don’t know how to get it work.

Sorry, but I don’t know what you mean. I tried petro’s solution, but it doesn’t work.

That will definitely work, so you need to explain what’s not working.

It delivers still the same result as my YAML did. The number of open doors and windows are shown in the dashboard card correctly, but the icons are not shown and not changing. Normally, when no door or window is open the mdi:window/door-closed should been shown. That does not work and also the icon for open windows/doors is not shown.

I think you need to check again as this is the proper way to handle this and works for many people. Otherwise, you need to recreate the code in the icon_template.

Keep in mind at startup or restart, the icon will not be in sync as the state machine does not have a state yet. The icon will always show off in that case until the first state change occurs that causes the value_template to update.

If you don’t like that, you have to add the code in value_template to your icon_template.

It certainly should work - you need to post your non-working code - where you’ve put it etc. to enable us to help you.

Maybe you should check if an icon was set for that entity in the UI that overrules the one from the template.

He can’t do that as he doesn’t have a unique_id

—> These icons don’t exist, check the auto completion in HA for reference.

And you need a


this.state|int(0)

3 Likes