Template binary sensor badge

I made some template binary sensors of gpio switches to view the state of that switches in default view.
binary_sensors.yaml:

- platform: template
  sensors:
    verwarming_badkamer:
      friendly_name: Badkamer
      value_template: "{{ states.switch.verwarming_badkamer.state == 'on' }}"

This works, but when i gave the sensor a customized icon, i can’t see if its ‘on’ or ‘off’
customized.yaml:

binary_sensor.verwarming_badkamer:
  icon: mdi:radiator

The switch changes color when turned on


but the binary template sensor doesn’t

1 Like

What’s the code for the switch look like?

switches.yaml:

- platform: rpi_gpio
  ports:
    4: Reserve
    17: Reserve 2
    27: Verwarming Badkamer
    22: Verluchting
    10: Verwarming Slaapkamer
    9: Verwarming Noor
    11: Verwarming Wc
    5: Verwarming Fien

customized.yaml:

#####  Switches  #####
switch.verwarming_slaapkamer:
  icon: mdi:radiator
  friendly_name: Slaapkamer
switch.verwarming_noor:
  icon: mdi:radiator
  friendly_name: Noor
switch.verwarming_fien:
  icon: mdi:radiator
  friendly_name: Fien
switch.verwarming_badkamer:
  icon: mdi:radiator
  friendly_name: Badkamer
switch.verluchting:
  icon: mdi:fan
  friendly_name: Verluchting

I’m having a little trouble because of the language barrier :sweat_smile:, but is your sensor value testing the state of the switch or the state of the actual device?

I think the state of the switch.
The meaning is, I can see in my default view which heating (verwarming) is on.

That should be right. Is anything showing in the logs when you activate the switch? Maybe activate the switch and note the time then look in the logs. We might find something there…

When I reboot, this is in my log:

16-06-25 16:38:00 homeassistant.core: WorkerPool:All 24 threads are busy and 73 jobs pending
16-06-25 16:38:00 homeassistant.core: WorkerPool:Current job from 2016-06-25T16:38:00.503637+02:00: (<function EntityPlatform._update_entity_states at 0x74d51ae0>, <Event time_changed[L]: now=2016-06-25T16:38:00.501116+02:00>)
16-06-25 16:38:00 homeassistant.core: WorkerPool:Current job from 2016-06-25T16:38:00.509284+02:00: (<function BinarySensorTemplate.__init__.<locals>.template_bsensor_state_listener at 0x6e1c89c0>, <Event state_changed[L]: old_state=<state sensor.time=16:37; friendly_name=Tijd, icon=mdi:clock @ 2016-06-25T16:37:05.480839+02:00>, new_state=<state sensor.time=16:38; friendly_name=Tijd, icon=mdi:clock @ 2016-06-25T16:38:00.504459+02:00>, entity_id=sensor.time>)
16-06-25 16:38:00 homeassistant.core: WorkerPool:Current job from 2016-06-25T16:38:00.518145+02:00: (<function Group._state_changed_listener at 0x6c16d978>, <Event state_changed[L]: old_state=<state sensor.time=16:37; friendly_name=Tijd, icon=mdi:clock @ 2016-06-25T16:37:05.480839+02:00>, new_state=<state sensor.time=16:38; friendly_name=Tijd, icon=mdi:clock @ 2016-06-25T16:38:00.504459+02:00>, entity_id=sensor.time>)
16-06-25 16:38:00 homeassistant.core: WorkerPool:Current job from 2016-06-25T16:38:00.519353+02:00: (<function BinarySensorTemplate.__init__.<locals>.template_bsensor_state_listener at 0x6e1c8078>, <Event state_changed[L]: old_state=<state sensor.time=16:37; friendly_name=Tijd, icon=mdi:clock @ 2016-06-25T16:37:05.480839+02:00>, new_state=<state sensor.time=16:38; friendly_name=Tijd, icon=mdi:clock @ 2016-06-25T16:38:00.504459+02:00>, entity_id=sensor.time>)
16-06-25 16:38:00 homeassistant.core: WorkerPool:Current job from 2016-06-25T16:38:00.525505+02:00: (<function BinarySensorTemplate.__init__.<locals>.template_bsensor_state_listener at 0x6e1c8618>, <Event state_changed[L]: old_state=<state sensor.time=16:37; friendly_name=Tijd, icon=mdi:clock @ 2016-06-25T16:37:05.480839+02:00>, new_state=<state sensor.time=16:38; friendly_name=Tijd, icon=mdi:clock @ 2016-06-25T16:38:00.504459+02:00>, entity_id=sensor.time>)
16-06-25 16:38:00 homeassistant.core: WorkerPool:Current job from 2016-06-25T16:38:00.531805+02:00: (<function BinarySensorTemplate.__init__.<locals>.template_bsensor_state_listener at 0x6e1c86a8>, <Event state_changed[L]: old_state=<state sensor.time=16:37; friendly_name=Tijd, icon=mdi:clock @ 2016-06-25T16:37:05.480839+02:00>, new_state=<state sensor.time=16:38; friendly_name=Tijd, icon=mdi:clock @ 2016-06-25T16:38:00.504459+02:00>, entity_id=sensor.time>)
16-06-25 16:38:00 homeassistant.core: WorkerPool:Current job from 2016-06-25T16:38:00.538018+02:00: (<function BinarySensorTemplate.__init__.<locals>.template_bsensor_state_listener at 0x6e1c8198>, <Event state_changed[L]: old_state=<state sensor.time=16:37; friendly_name=Tijd, icon=mdi:clock @ 2016-06-25T16:37:05.480839+02:00>, new_state=<state sensor.time=16:38; friendly_name=Tijd, icon=mdi:clock @ 2016-06-25T16:38:00.504459+02:00>, entity_id=sensor.time>)
16-06-25 16:38:00 homeassistant.core: WorkerPool:Current job from 2016-06-25T16:38:00.544399+02:00: (<function BinarySensorTemplate.__init__.<locals>.template_bsensor_state_listener at 0x72d38d20>, <Event state_changed[L]: old_state=<state sensor.time=16:37; friendly_name=Tijd, icon=mdi:clock @ 2016-06-25T16:37:05.480839+02:00>, new_state=<state sensor.time=16:38; friendly_name=Tijd, icon=mdi:clock @ 2016-06-25T16:38:00.504459+02:00>, entity_id=sensor.time>)
16-06-25 16:38:00 homeassistant.core: WorkerPool:Current job from 2016-06-25T16:38:00.550878+02:00: (<function BinarySensorTemplate.__init__.<locals>.template_bsensor_state_listener at 0x72d38e88>, <Event state_changed[L]: old_state=<state sensor.time=16:37; friendly_name=Tijd, icon=mdi:clock @ 2016-06-25T16:37:05.480839+02:00>, new_state=<state sensor.time=16:38; friendly_name=Tijd, icon=mdi:clock @ 2016-06-25T16:38:00.504459+02:00>, entity_id=sensor.time>)
16-06-25 16:38:00 homeassistant.core: WorkerPool:Current job from 2016-06-25T16:38:00.557311+02:00: (<function BinarySensorTemplate.__init__.<locals>.template_bsensor_state_listener at 0x72d38df8>, <Event state_changed[L]: old_state=<state sensor.time=16:37; friendly_name=Tijd, icon=mdi:clock @ 2016-06-25T16:37:05.480839+02:00>, new_state=<state sensor.time=16:38; friendly_name=Tijd, icon=mdi:clock @ 2016-06-25T16:38:00.504459+02:00>, entity_id=sensor.time>)
16-06-25 16:38:00 homeassistant.core: WorkerPool:Current job from 2016-06-25T16:38:00.563077+02:00: (<function SensorTemplate.__init__.<locals>.template_sensor_state_listener at 0x741fd420>, <Event state_changed[L]: old_state=<state sensor.time=16:37; friendly_name=Tijd, icon=mdi:clock @ 2016-06-25T16:37:05.480839+02:00>, new_state=<state sensor.time=16:38; friendly_name=Tijd, icon=mdi:clock @ 2016-06-25T16:38:00.504459+02:00>, entity_id=sensor.time>)
16-06-25 16:38:00 homeassistant.core: WorkerPool:Current job from 2016-06-25T16:38:00.569710+02:00: (<function EntityPlatform._update_entity_states at 0x72d61198>, <Event time_changed[L]: now=2016-06-25T16:38:00.501116+02:00>)
16-06-25 16:38:00 homeassistant.core: WorkerPool:Current job from 2016-06-25T16:38:00.571152+02:00: (<function EntityPlatform._update_entity_states at 0x74d51588>, <Event time_changed[L]: now=2016-06-25T16:38:00.501116+02:00>)
16-06-25 16:38:00 homeassistant.core: WorkerPool:Current job from 2016-06-25T16:38:00.573366+02:00: (<function EntityPlatform._update_entity_states at 0x6c183b28>, <Event time_changed[L]: now=2016-06-25T16:38:00.501116+02:00>)
16-06-25 16:38:00 homeassistant.core: WorkerPool:Current job from 2016-06-25T16:38:00.573982+02:00: (<function EntityPlatform._update_entity_states at 0x741c7fa8>, <Event time_changed[L]: now=2016-06-25T16:38:00.501116+02:00>)
16-06-25 16:38:00 homeassistant.core: WorkerPool:Current job from 2016-06-25T16:38:00.574887+02:00: (<function EntityPlatform._update_entity_states at 0x741cefa8>, <Event time_changed[L]: now=2016-06-25T16:38:00.501116+02:00>)
16-06-25 16:38:00 homeassistant.core: WorkerPool:Current job from 2016-06-25T16:38:00.575388+02:00: (<function EntityPlatform._update_entity_states at 0x72d59f60>, <Event time_changed[L]: now=2016-06-25T16:38:00.501116+02:00>)
16-06-25 16:38:00 homeassistant.core: WorkerPool:Current job from 2016-06-25T16:38:00.576280+02:00: (<function setup_scanner_platform.<locals>.device_tracker_scan at 0x72d38618>, <Event time_changed[L]: now=2016-06-25T16:38:00.501116+02:00>)

When I turn my switch on or off nothing happens inn the log

When I view logs in terminal, I get this when I turn it on and off:

INFO:homeassistant.core:Bus:Handling <Event call_service[L]: service_data=entity_id=switch.verwarming_badkamer, service=turn_on, service_call_id=1978823728-5, domain=homeassistant>
INFO:homeassistant.core:Bus:Handling <Event call_service[L]: service_data=entity_id=['switch.verwarming_badkamer'], service=turn_on, service_call_id=1978823728-6, domain=switch>
INFO:homeassistant.core:Bus:Handling <Event state_changed[L]: entity_id=switch.verwarming_badkamer, old_state=<state switch.verwarming_badkamer=off; friendly_name=Badkamer, icon=mdi:radiator @ 2016-06-25T16:50:19.560996+02:00>, new_state=<state switch.verwarming_badkamer=on; friendly_name=Badkamer, icon=mdi:radiator @ 2016-06-25T16:51:24.069015+02:00>>
INFO:homeassistant.core:Bus:Handling <Event service_executed[L]: service_call_id=1978823728-6>
INFO:homeassistant.core:Bus:Handling <Event state_changed[L]: entity_id=binary_sensor.verwarming_badkamer, old_state=<state binary_sensor.verwarming_badkamer=off; friendly_name=Badkamer, icon=mdi:radiator @ 2016-06-25T16:50:19.704406+02:00>, new_state=<state binary_sensor.verwarming_badkamer=on; friendly_name=Badkamer, icon=mdi:radiator @ 2016-06-25T16:51:24.183687+02:00>>
INFO:homeassistant.core:Bus:Handling <Event service_executed[L]: service_call_id=1978823728-5>
INFO:homeassistant.core:Bus:Handling <Event state_changed[L]: entity_id=group.all_switches, old_state=<state group.all_switches=off; entity_id=('switch.reserve', 'switch.verwarming_wc', 'switch.verwarming_slaapkamer', 'switch.verluchting', 'switch.verwarming_badkamer', 'switch.reserve_2', 'switch.verwarming_fien', 'switch.verwarming_noor'), order=2, hidden=True, auto=True, friendly_name=all switches @ 2016-06-25T16:50:19.818582+02:00>, new_state=<state group.all_switches=on; entity_id=('switch.reserve', 'switch.verwarming_wc', 'switch.verwarming_slaapkamer', 'switch.verluchting', 'switch.verwarming_badkamer', 'switch.reserve_2', 'switch.verwarming_fien', 'switch.verwarming_noor'), order=2, hidden=True, auto=True, friendly_name=all switches @ 2016-06-25T16:51:24.225002+02:00>>
INFO:homeassistant.core:Bus:Handling <Event state_changed[L]: entity_id=group.badkamer, old_state=<state group.badkamer=off; entity_id=('switch.verwarming_badkamer',), order=14, friendly_name=Badkamer @ 2016-06-25T16:50:19.765027+02:00>, new_state=<state group.badkamer=on; entity_id=('switch.verwarming_badkamer',), order=14, friendly_name=Badkamer @ 2016-06-25T16:51:24.489112+02:00>>
INFO:homeassistant.core:Bus:Handling <Event state_changed[L]: entity_id=group.default_view, old_state=<state group.default_view=off; entity_id=('input_select.wie_slaapt', 'input_select.scene_selection', 'group.time', 'group.booleans', 'updater.updater', 'binary_sensor.verluchting', 'binary_sensor.verwarming_fien', 'binary_sensor.verwarming_noor', 'binary_sensor.verwarming_badkamer', 'binary_sensor.verwarming_slaapkamer', 'binary_sensor.verwarming_wc', 'binary_sensor.verwarming_bureau', 'binary_sensor.opladers'), order=3, hidden=True, friendly_name=default_view, view=True @ 2016-06-25T16:50:19.925517+02:00>, new_state=<state group.default_view=on; entity_id=('input_select.wie_slaapt', 'input_select.scene_selection', 'group.time', 'group.booleans', 'updater.updater', 'binary_sensor.verluchting', 'binary_sensor.verwarming_fien', 'binary_sensor.verwarming_noor', 'binary_sensor.verwarming_badkamer', 'binary_sensor.verwarming_slaapkamer', 'binary_sensor.verwarming_wc', 'binary_sensor.verwarming_bureau', 'binary_sensor.opladers'), order=3, hidden=True, friendly_name=default_view, view=True @ 2016-06-25T16:51:24.504414+02:00>>
INFO:homeassistant.components.http:192.168.0.29 - - [25/Jun/2016 16:51:24] "POST /api/services/homeassistant/turn_on HTTP/1.1" 200 1957 0.520097
INFO:homeassistant.components.device_tracker.nmap_tracker:Scanning
INFO:homeassistant.core:Bus:Handling <Event state_changed[L]: entity_id=group.boven, old_state=<state group.boven=off; entity_id=('group.wie_slaapt', 'group.badkamer', 'group.slaapkamer', 'group.slaapkamer_noor', 'group.slaapkamer_fien', 'group.bureau'), order=10, hidden=True, friendly_name=Boven, view=True @ 2016-06-25T16:50:19.997166+02:00>, new_state=<state group.boven=on; entity_id=('group.wie_slaapt', 'group.badkamer', 'group.slaapkamer', 'group.slaapkamer_noor', 'group.slaapkamer_fien', 'group.bureau'), order=10, hidden=True, friendly_name=Boven, view=True @ 2016-06-25T16:51:24.635478+02:00>>
INFO:homeassistant.core:Bus:Handling <Event call_service[L]: service_data=entity_id=switch.verwarming_badkamer, service=turn_off, service_call_id=1978823728-7, domain=homeassistant>
INFO:homeassistant.core:Bus:Handling <Event call_service[L]: service_data=entity_id=['switch.verwarming_badkamer'], service=turn_off, service_call_id=1978823728-8, domain=switch>
INFO:homeassistant.core:Bus:Handling <Event state_changed[L]: entity_id=switch.verwarming_badkamer, old_state=<state switch.verwarming_badkamer=on; friendly_name=Badkamer, icon=mdi:radiator @ 2016-06-25T16:51:24.069015+02:00>, new_state=<state switch.verwarming_badkamer=off; friendly_name=Badkamer, icon=mdi:radiator @ 2016-06-25T16:51:25.733033+02:00>>
INFO:homeassistant.core:Bus:Handling <Event service_executed[L]: service_call_id=1978823728-8>
INFO:homeassistant.core:Bus:Handling <Event service_executed[L]: service_call_id=1978823728-7>
INFO:homeassistant.core:Bus:Handling <Event state_changed[L]: entity_id=group.all_switches, old_state=<state group.all_switches=on; entity_id=('switch.reserve', 'switch.verwarming_wc', 'switch.verwarming_slaapkamer', 'switch.verluchting', 'switch.verwarming_badkamer', 'switch.reserve_2', 'switch.verwarming_fien', 'switch.verwarming_noor'), order=2, hidden=True, auto=True, friendly_name=all switches @ 2016-06-25T16:51:24.225002+02:00>, new_state=<state group.all_switches=off; entity_id=('switch.reserve', 'switch.verwarming_wc', 'switch.verwarming_slaapkamer', 'switch.verluchting', 'switch.verwarming_badkamer', 'switch.reserve_2', 'switch.verwarming_fien', 'switch.verwarming_noor'), order=2, hidden=True, auto=True, friendly_name=all switches @ 2016-06-25T16:51:25.751635+02:00>>
INFO:homeassistant.core:Bus:Handling <Event state_changed[L]: entity_id=binary_sensor.verwarming_badkamer, old_state=<state binary_sensor.verwarming_badkamer=on; friendly_name=Badkamer, icon=mdi:radiator @ 2016-06-25T16:51:24.183687+02:00>, new_state=<state binary_sensor.verwarming_badkamer=off; friendly_name=Badkamer, icon=mdi:radiator @ 2016-06-25T16:51:25.885854+02:00>>
INFO:homeassistant.core:Bus:Handling <Event state_changed[L]: entity_id=group.badkamer, old_state=<state group.badkamer=on; entity_id=('switch.verwarming_badkamer',), order=14, friendly_name=Badkamer @ 2016-06-25T16:51:24.489112+02:00>, new_state=<state group.badkamer=off; entity_id=('switch.verwarming_badkamer',), order=14, friendly_name=Badkamer @ 2016-06-25T16:51:25.915178+02:00>>
INFO:homeassistant.core:Bus:Handling <Event state_changed[L]: entity_id=group.boven, old_state=<state group.boven=on; entity_id=('group.wie_slaapt', 'group.badkamer', 'group.slaapkamer', 'group.slaapkamer_noor', 'group.slaapkamer_fien', 'group.bureau'), order=10, hidden=True, friendly_name=Boven, view=True @ 2016-06-25T16:51:24.635478+02:00>, new_state=<state group.boven=off; entity_id=('group.wie_slaapt', 'group.badkamer', 'group.slaapkamer', 'group.slaapkamer_noor', 'group.slaapkamer_fien', 'group.bureau'), order=10, hidden=True, friendly_name=Boven, view=True @ 2016-06-25T16:51:25.964611+02:00>>
INFO:homeassistant.core:Bus:Handling <Event state_changed[L]: entity_id=group.default_view, old_state=<state group.default_view=on; entity_id=('input_select.wie_slaapt', 'input_select.scene_selection', 'group.time', 'group.booleans', 'updater.updater', 'binary_sensor.verluchting', 'binary_sensor.verwarming_fien', 'binary_sensor.verwarming_noor', 'binary_sensor.verwarming_badkamer', 'binary_sensor.verwarming_slaapkamer', 'binary_sensor.verwarming_wc', 'binary_sensor.verwarming_bureau', 'binary_sensor.opladers'), order=3, hidden=True, friendly_name=default_view, view=True @ 2016-06-25T16:51:24.504414+02:00>, new_state=<state group.default_view=off; entity_id=('input_select.wie_slaapt', 'input_select.scene_selection', 'group.time', 'group.booleans', 'updater.updater', 'binary_sensor.verluchting', 'binary_sensor.verwarming_fien', 'binary_sensor.verwarming_noor', 'binary_sensor.verwarming_badkamer', 'binary_sensor.verwarming_slaapkamer', 'binary_sensor.verwarming_wc', 'binary_sensor.verwarming_bureau', 'binary_sensor.opladers'), order=3, hidden=True, friendly_name=default_view, view=True @ 2016-06-25T16:51:26.070474+02:00>>
INFO:homeassistant.components.http:192.168.0.29 - - [25/Jun/2016 16:51:26] "POST /api/services/homeassistant/turn_off HTTP/1.1" 200 1700 0.345073

This is sort of common and is the new normal - it doesn’t seem to cause any issues.

The second log seems to show the switch firing and even the update of states to several groups but it almost looks like the sensor isn’t getting enough time to update it’s state. By time you switched off the switch, the state on the sensor was still “off”.

Not sure why but maybe this will give you someplace to look?

That was just manual switched on and of, maybe to fast.


The state of the sensor changes, but the color of the badge doesn’t change with it.

Now I’m stumped. I see you have a custom icon for it - just for the sake of trying something else, try commenting out the icon line and restarting HA and trying the same test.

I’ve commented out the custom icon.


Well, at least the behavior is consistent… I’m stumped though.

Correct me if i’m wrong, the badge (customized) must also change color from blue to yellow.

I’m pretty sure you are correct; it’s been a long time since I had any badges on my setup.

Binary sensor badge icons do not light up when ‘active/true/on/etc’. Instead they differentiate by changing the icon between V and O. When you set a custom icon, this behavior is overwritten.

Can’t this be implemented? I use them to see if a heating is on. And it’s nicer whit a custom icon.

1 Like

How do I change the icons for the various states? I’ve found templates using text, but not images.