Binary Sensor - Icon Template Opposite Issues!

I’m hoping I’m just being an idiot but please can someone point me in the right direction…

I’ve set up a binary_sensor to show if anyone is connected to my guest SSID. This will then allow me to determine automation actions based on whether this binary_sensor is on or off.

I’ve set up the sensor and it works perfectly (see code below), however the icon template does the opposite of what I’ve configured it to do! The idea is that when a guest is in the house the icon is mdi:account and when there are no guests the icon should be mdi:account-off. I’m getting the opposite experience though…

- platform: template
  sensors:
    guest_presence:
      friendly_name: "Guests"
      entity_id:
        - sensor.unifi
      value_template: >-
        {{ state_attr('sensor.unifi', 'SSID_Guest')|int > 0 }}
      icon_template: >-
        {% if is_state("binary_sensor.guest_presence", "on") %}
          mdi:account
        {% else %}
          mdi:account-off
        {% endif %}

…I’ve run the icon_template through Developer Tools -> Template and when the binary sensor is on the template shows me the text mdi:account and when the binary sensor is off it shows me the text mdi:account-off. But in the UI it does exactly the opposite and shows the “account” icon when there are no guests and the “account-off” icon when there are guests!

What have I done wrong? :man_facepalming:

Looks like this is a delay issue. The icon changes around 10 minutes after the state of the binary_sensor changes. My “opposite” issue I think must’ve been that I waited a while for the change, it didn’t happen so disconnected my test device from the guest SSID but within that time the icon had actually changed to reflect the above config, so it then took a while to change back, resulting in what seemed to be the icon displaying the opposite of what I was expecting! (I hope that makes sense!)

Is it normal behaviour for the change to take so long? To be fair I won’t be relying on the icon whatsoever, it’s just a nice enhancement to the sensor!

Try using the same template you use for the value template.

{% if state_attr('sensor.unifi', 'SSID_Guest')|int > 0 %}

The template you have should update as soon as the binary sensor changes state. I have no idea why you are not seeing that happen.

Ah so something like this perhaps?

icon_template: >-
        {% if state_attr('sensor.unifi', 'SSID_Guest')|int > 0 %}
          mdi:account
        {% else %}
          mdi:account-off
        {% endif %}

If that’s the idea then I’ll give it a shot! Although, your post has given me an idea…If I include binary_sensor.guest_presence within the entity_id: section, would that fix the original issue maybe? I was assuming that both actions would happen at once (ie. number of connected users is more than 0 so the binary sensor is turned on then also change the icon. Just to clarify I’m thinking maybe this might work…?

 - platform: template
  sensors:
    guest_presence:
      friendly_name: "Guests"
      entity_id:
        - sensor.unifi, binary_sensor.guest_presence
      value_template: >-
        {{ state_attr('sensor.unifi', 'SSID_Guest')|int > 0 }}
      icon_template: >-
        {% if is_state("binary_sensor.guest_presence", "on") %}
          mdi:account
        {% else %}
          mdi:account-off
        {% endif %}

No. There is no entity id option for the icon template and use of that elsewhere is being depreciated soon. Try the first option.

I didn’t mean that I’d include the entity_id: within the icon_template: but instead it’d be like I’d written above and include it within the overall entity_id:

Either way I don’t think it works unfortunately as I tried it. It still takes ~10 minutes to change :disappointed:

As above, it’s not the end of the world so I’ll leave it for now!