I posted this under a different topic before, but this has been so incredibly useful for me that I think it deserves an update. This is the #1 UI element of my Smarthome system. It is simple, self-explanatory, and has the seal of officially being wife approved. Let’s cut to the chase:
Let’s take a look at the GUI code, which is a simple glance card that leverages tap_action
and the custom script (included after the UI) script.sensor_target_toggle
. For each annunciator item you want to control, you specify the entity id of that item as the target. In order to show an item as on/off in the UI, you also need to specify a sensor, by providing the entity id of the object which represents the on/off state of the annunciator item. More often than not, this will be the same entity id as the control item. Huh? Isn’t that obvious? If you want to see the state of the Kitchen Ceiling Fan, of course you should look at its state. But there are cases where you may have several different lights, which taken together make up a scene. Suppose you have lights A,B,C which make up switch.my_scene, and you consider the scene to be on/off if light B is on/off. To control this with the annunciator, the target_id would be set to switch.my_scene
, and the sensor_id to be light.B
.
Lovelace YAML (be sure to visit material design for lots of great icons!)
type: glance
title: My Annunciator
columns: 4
entities:
- entity: binary_sensor.family_room_main
icon: 'mdi:sofa'
name: Living Room
tap_action:
action: call-service
service: script.sensor_target_toggle
service_data:
sensor_id: binary_sensor.family_room_main
target_id: switch.family_room_main
- entity: binary_sensor.kitchen_main
icon: 'mdi:silverware-fork-knife'
name: Kitchen
tap_action:
action: call-service
service: script.sensor_target_toggle
service_data:
sensor_id: binary_sensor.kitchen_main
target_id: switch.kitchen_main
- entity: binary_sensor.movie
icon: 'mdi:movie-open'
name: Movie
tap_action:
action: call-service
service: script.sensor_target_toggle
service_data:
sensor_id: binary_sensor.movie
target_id: switch.movie
- entity: switch.2frb1_fr_fan
icon: 'mdi:fan'
name: Family Room
tap_action:
action: call-service
service: script.sensor_target_toggle
service_data:
sensor_id: switch.2frb1_fr_fan
target_id: switch.2frb1_fr_fan
- entity: binary_sensor.stairs_all
icon: 'mdi:stairs'
name: Stairs
tap_action:
action: call-service
service: script.sensor_target_toggle
service_data:
sensor_id: binary_sensor.stairs_all
target_id: switch.stairs_all
- entity: binary_sensor.upstairs
icon: 'mdi:lamp'
tap_action:
action: call-service
service: script.sensor_target_toggle
service_data:
sensor_id: binary_sensor.upstairs
target_id: switch.upstairs
- entity: binary_sensor.dimmer
icon: 'mdi:movie-open'
name: Dimmer
tap_action:
action: call-service
service: script.sensor_target_toggle
service_data:
sensor_id: binary_sensor.dimmer
target_id: switch.dimmer
- entity: light.1sta1_porch_light
icon: 'mdi:coach-lamp'
name: Porch
tap_action:
action: call-service
service: light.toggle
service_data:
entity_id: light.1sta1_porch_light
- entity: light.2frb3_deck
icon: 'mdi:coach-lamp'
name: Deck
tap_action:
action: call-service
service: light.toggle
service_data:
entity_id: light.2frb3_deck
- entity: light.39351_zw3005_in_wall_smart_dimmer_level
icon: 'mdi:coach-lamp'
name: Attic fan
tap_action:
action: call-service
service: light.toggle
service_data:
entity_id: light.attic_fan
- entity: binary_sensor.nightlight
icon: 'mdi:movie-open'
name: Nightlight
tap_action:
action: call-service
service: script.sensor_target_toggle
service_data:
sensor_id: binary_sensor.nightlight
target_id: switch.nightlight
- entity: switch.mbr_reading
icon: 'mdi:coach-lamp'
name: Reading
tap_action:
action: call-service
service: script.sensor_target_toggle
service_data:
sensor_id: switch.mbr_reading
target_id: switch.mbr_reading
- entity: switch.mbr_master_bedroom_general
icon: 'mdi:coach-lamp'
name: MBR
tap_action:
action: call-service
service: script.sensor_target_toggle
service_data:
sensor_id: switch.mbr_master_bedroom_general
target_id: switch.mbr_master_bedroom_general
- entity: switch.mba_bathroom
icon: 'mdi:coach-lamp'
name: MBA
tap_action:
action: call-service
service: script.sensor_target_toggle
service_data:
sensor_id: switch.mba_bathroom
target_id: switch.mba_bathroom
- entity: switch.shower
icon: 'mdi:movie-open'
name: Shower
tap_action:
action: call-service
service: script.sensor_target_toggle
service_data:
sensor_id: switch.shower
target_id: switch.shower
- entity: switch.hs_ws100_wall_switch_switch
icon: 'mdi:fan'
name: MBA Fan
tap_action:
action: call-service
service: script.sensor_target_toggle
service_data:
sensor_id: switch.hs_ws100_wall_switch_switch
target_id: switch.hs_ws100_wall_switch_switch
and the code for the script:
sensor_target_toggle:
alias: 'Sensor Target toggle'
description: 'Get the sensor state and toggle the target'
fields:
sensor_id:
description: 'Sensor entity_id'
example: binary_sensor.porch_deck
target_id:
description: 'Target entity_id'
example: switch.porch_deck
sequence:
- service_template: >
{% if is_state(sensor_id, 'on') %}
switch.turn_off
{% else %}
switch.turn_on
{% endif %}
data_template:
entity_id: >
{% set entity = target_id %}
{{entity}}
You can also create your own binary sensors if you want a more elaborate “scene sensor”. The example below is a binary sensor that determines if the “Family Room Main” scene is on if the brightness of light.fr_spotlight
is between 50% and 60%.
family_room_main:
value_template: >-
{% set pct = ((states.light.fr_spotlight.attributes.brightness | float / 255) | abs )%}
{% set ret = (0.5 <= pct) and (pct <= 0.6) %}
{{ ret }}