Template switch icon do not change

Hi, I have a template switch based on a door lock.

- platform: template
    switches:
      front_door:
        friendly_name: Entre Dörren
        value_template: "{{ is_state('switch.doorlock', 'on') }}"
        turn_on:
          service: switch.turn_on
          data:
            entity_id: switch.doorlock
        turn_off:
          service: switch.turn_off
          data:
            entity_id: switch.doorlock
        icon_template: >-
          {%- if is_state('switch.doorlock', 'on') or is_state_attr('light.door_status', 'brightness', 20) -%}
            mdi:shield-lock
          {%- elif is_state_attr('light.door_status', 'brightness', 127) -%}
            mdi:door-closed
          {%- elif is_state_attr('light.door_status', 'brightness', 255) -%}
            mdi:door-open
          {%- else -%}
            mdi: lock-question
          {%- endif -%}

What I am trying to do is that the icon should be updated based on the door’s status. The status comes from a dummy unit in the form of a dimable light (light.door_status). The icon changes when I lock / unlock the door, but if the door is unlocked and I open the door, the icon does not change.
Does anyone have a tip on how to do this if it is even possible?

Regards Andreas

there’s too many unknowns here. Where are you getting this light.door_status entity? Are you certain the light brightness is changing when you open the door or close the door?

I am 100% sure that those dim levels are correct, they come from z-wave Id-Lock and Telldus Z-net v2. The only time icon changes is when i lock/unlock the door. I would like the icon to change even when i open/close the door without locking it.

It seems that the icon only updates when state changes.

I understand that but the problem is clearly with the light.door_status portion of all your code. None of the statements using is_state_attr('light.door_status', 'brightness', x) are resolving to true. Otherwise you’d get a door open / door closed.

So with that being said you need to figure out what the true value for that attribute is.

You can place

{{ is_state_attr('light.door_status', 'brightness', 255) }}

into the template editor to verify that they return true/false when the door is open or closed.

Also, remember that strings are not equal to integers, but floats can be equal to integers.

It state true when i set value to current “brightness” and false if i set it othervise. An if i paste the entire template it tells me correct mdi:icon.

What i try to find out is if there are any setting och function to force update even if state dont change. I made a sensor with exact that templat and it works.

Changing attributes is considered a state change. But if you just want to force it, you can use the homeassistant.update_entity service. Documented on the template sensor page

I have tried “homeassistant.update_entity” but the icon still dont change.
I’ll investigate a little, i think i got an idea.

Great thanks for your input

/Andreas

Did you possibly spell the light entity_id wrong in any spots?

I found a solution, i changed "value_template: “{{ is_state(‘switch.doorlock’, ‘on’) }}” to “{{ is_state(‘sensor.door_status’, ‘Låst’) }}” and use the sensor that worked.
Now the switch react to every event.

There were no misspelling, seems as if the icon only update when value_template change.

By the way, do you know if there is possible to add a sensor value as attribute in template switch?

/Andreas

Never tried. From what I remember, back in the day you used to be able to set attributes via the customize section. But I don’t know if you can change them on the fly. Might require an automation. Also, if you are getting that complex, you can just make your own components.

I wish, i have tried to check that with custom_component and such. My head starts to spin on an instant and i give up. :rofl: :crazy_face:
Maybe i dig in to that some day.

Thank you for your input and your suggestions

/Andreas