Change Icon based on sensor state without creating new sensor

Hi,

I have a binary_sensor called ‘binary_sensor.garage_door_input’. It is the reed swtich on my Shelly1 that tells me whether the garage door is open or not.

By default it’s state would be ‘On’ or ‘Off’.

By over-riding it’s device-class attribute in ‘Customisations’ and setting to ‘garage-door’ I now have it showing the State as either ‘Open’ or ‘Closed’.

It’s default icon is ‘mdi:garage’.

It’s friendly name is ‘Garage Door Status’
garage door status

To that end it works fine:
garage door status dash

What I am trying to do now is simply change the icon based on the sensor state
If the State is ‘On’ /‘Open’ then icon = ‘mdi:garage-open’
if the State is ‘Off’/‘Closed’ then icon = ‘mdi:garage’

I searched this site to find an answer but the info I find is usually a bit old and/or creates a new binary_sensor and/or HA barks at me the format is wrong.

Does anyone have a clear example of the code that will do this for me without creating a new sensor? I assume it will go in ‘customization.yaml’ ?

Thanks in advance.

The device class should already do that. See: https://community.home-assistant.io/t/binary-sensor-device-classes/243493

EDIT: Delete icon: mdi:garage from your customisation. It is overriding the changing icons of the device class.

Thanks @tom_l

However I couldn’t get this to work. The icon will only change colour when state changes.

I deleted the icon value via Configuration > Customisations and clicked ‘Save’.
deleted icon

Restarted HA.
Icon when Open:
garage open

Icon when Closed:
garage door status dash

Everytime I go back to ’ Configuration > Customisations’ the icon is there again.

The customize.yaml doesn’t show any icon either.

binary_sensor.garage_door_input:
  device_class: garage_door

Is there anywhere else this icon is being forced?

Not sure if this makes any difference, this is the Shelly native intergration, not ‘ShellyforHACS’ or anything else custom like Shelly Discover. I did not create this binary_sensor in configuration.yaml .

Very strange.

It could be two things that I can think of,

  1. An issue with the Shelly Integration (would not be the first).

  2. Something persistent in the .storage files if you used the UI to customise this binary sensor. Did you use YAML or the UI?

I know you specifically asked not to create a new entity, but i have a perfect example to do this via creating a new cover entity

cover:
  - platform: template
    covers:
      garage_door:
        device_class: garage
        friendly_name: "Garage Door"
        value_template: "{{ states.switch.garage_door.attributes.current_consumption | float * 10 > 0 }}"
        open_cover:
          service: script.garage_door_open
        close_cover:
          service: script.garage_door_close

I used the UI to set the device_class attribute and to try and delete the default icon.

ok so that worked! Thanks!

My code:


          
          cover:
	    - platform: template
	      covers:
	        garage_door:
	          device_class: garage
	          friendly_name: "Garage Door"
	          value_template: >-
	            {% if is_state('binary_sensor.garage_door_input','on') %}
	              Open
	            {% else %}
	              Closed
	            {% endif %}
	          open_cover:
	            service: switch.turn_on
	            data:
	              entity_id: switch.garage_door
	          close_cover:
	            service: switch.turn_off
	            data:
	              entity_id: switch.garage_door
	          stop_cover:
	            service: switch.turn_on
	            data:
	              entity_id: switch.garage_door
	          icon_template: >-
	            {% if is_state('binary_sensor.garage_door_input','on') %}
	              mdi:garage-open
	            {% else %}
	              mdi:garage
                    {% endif %}

It does take a little while for the cover to update. Is there a time setting for polling the state?

You can replace this:

	          value_template: >-
	            {% if is_state('binary_sensor.garage_door_input','on') %}
	              Open
	            {% else %}
	              Closed
	            {% endif %}

With this:

	          value_template: "{{ is_state('binary_sensor.garage_door_input','on') }}"

As true/false are valid results for the template as well as open/closed.

The state is not polled. It should update as soon as your garage door binary sensor changes. If that sensor is polled then that is where your delay is.

Thanks that change gave me a better looking card for that cover where the switch and the state icon are on same line.

No change to timing though , still a little delayed. I dont know where to check if it is polled or not, just using the Shelly native intergration. it’s no biggy as this reed switch really only tells me when garage is fully open. Magnet only connects to switch when door is at maximum openess. I need to place another sensor on bottom of door to tell me when it’s ‘not 100% closed’. I’ll use that to drive automations like turning lights on.

From the docs the Shelly integration is local push:

Screenshot 2021-07-17 at 19-56-46 Shelly

So there should be very little delay in the binary sensor, and subsequently the cover template, being updated.

What sort of delay are you seeing?

about 10 seconds from the door being fully open to to it registering the state on HA.

Watch the binary sensor and the cover. Do they both have the delay?

yes exact same delay.

Do you have an MQTT broker set up?

I use that for communicating with my Shelly devices and it is instant.

I have Mosquito MQTT integration but have not enabled MQTT on the device.

Well if you want to try it:

  1. Enable MQTT and set up the broker details in the device.

  2. Test this binary sensor config to see if it has the delay:

- platform: mqtt
  state_topic: "shellies/<device_id_here>/input/0"
  name: "Test garage sensor"
  payload_on: '1'
  payload_off: '0'
  qos: 1

I found this in the logs:

2021-07-17 20:15:51 WARNING (MainThread) [homeassistant.components.sensor] Updating rest sensor took longer than the scheduled update interval 0:00:01
2021-07-17 20:15:57 WARNING (MainThread) [homeassistant.components.sensor] Updating rest sensor took longer than the scheduled update interval 0:00:01
2021-07-17 20:15:57 WARNING (MainThread) [homeassistant.components.sensor] Updating rest sensor took longer than the scheduled update interval 0:00:01

Are you sure you are using the core Shelly integration?

I believe I am using ‘core Shelly integration’ yes:

Intergrations

I enabled MQTT and setup the code in configuration.yaml, but it did not change state.

MQTT logs

1626393717: New connection from 172.30.32.1 on port 1883.
1626393717: New client connected from 172.30.32.1 as 2HJjIz4wmU7Z3pJPOwtyud (p2, c1, k60, u'homeassistant').
1626393926: Socket error on client 2HJjIz4wmU7Z3pJPOwtyud, disconnecting.

Doesn’t look like the integration can connect to the broker correctly.