Home assistant labels
How to write behaviour once and then use labels to configure devices
I’ve been using Home Assistant since version 0.25 or so and I’ve always found writing a specific type of automation annoying - like the kind where a sensor controls a light. Sure enough, there are now blueprints but I still find them too annoying to use.
One of the use cases I have is this: In several rooms (bathroom, kitchen, wardrobe) we don’t always turn on the light - but when we do we don’t always turn them off again (and in those rooms we have halogen spot lights - if I turn on the kitchen and the bathroom on full brightness my power usage goes up by a whole kW!). The other case is that I want to easily mark devices that I care more about than others (like entities that go unavailable).
Light labeling
Therefore I want to turn off the light if the room hasn’t detected any motion for 15 minutes. Easy peasy with an automation, but annoying to do several times.
I wanted to see if I could utilize the labels functionality. The idea is that I label the motion sensor and the light entity and let the automation figure out what rooms they are in automatically.
To do this I’ve created a label called auto_off. I’ve attached it to the sensor I want to follow and the lights that I want it to turn off.
The automation is kind of dumb, as Home Assistant doesn’t expose all functionality. It listens to all state_changed events, and then filters out everything except binary_sensors going to off that are labeled auto_off, hopefully in an efficient way (I haven’t noticed any performance issues, but I haven’t looked either). If it passes through that, we check that the sensor is assigned to a room - if it is we resolve all binary sensors in that room and wait until they’ve all been off for 15 minutes - then we resolve all lights and switches in that room and turn them off.
How to use
Create a label auto_off and add this automation. If you have many rooms you might need to increase the max property of this automation.
alias: auto_offdescription: "Turn off labeled lights/switches in a room after its sensor stays clear for 15 min"mode: paralleltriggers: - trigger: event event_type: state_changedconditions: - alias: "A binary_sensor labeled auto_off just turned off" condition: template value_template: > {% set e = trigger.event.data %} {{ e.new_state is not none and e.entity_id.split('.', 1)[0] == 'binary_sensor' and e.new_state.state == 'off' and e.entity_id in label_entities('auto_off') }}actions: - variables: room: "{{ area_name(trigger.event.data.entity_id) }}" # Bail out (and free the parallel slot) if the sensor has no area - condition: template value_template: "{{ room is not none }}" - wait_template: > {{ expand(label_entities('auto_off')) | selectattr('domain', 'eq', 'binary_sensor') | selectattr('entity_id', 'in', area_entities(room)) | map(attribute='entity_id') | select('is_state', 'on') | list | count > 0 }} continue_on_timeout: true timeout: "00:15:00" - choose: - conditions: - condition: template value_template: "{{ not wait.completed }}" sequence: - action: homeassistant.turn_off target: entity_id: > {{ expand(label_entities('auto_off')) | selectattr('domain', 'in', ['light', 'switch']) | selectattr('entity_id', 'in', area_entities(room)) | map(attribute='entity_id') | list }}Then you assign the auto_off label to those sensors you want to track (motion sensors, door sensors - what have you) and assign the same label to the lights and switches you want to turn off. Make sure the sensors, lights and switches are assigned to the correct room! Then it’ll just work. If you don’t want a light to be turned off - you just unlabel auto_off and you’re done.
Unavailable labeling
I have a chest freezer that for some reason lives on the same RCD as my outdoor outlets - which means that after heavy rain (and I forgot to disconnect something outside) it trips. And since the chest freezer is in the basement I don’t always notice it. However, the freezer is connected through a Zigbee power meter plug - so I trigger on when that plug is going unavailable.
As I have other things that also should be unavailable I created a label for that as well, always_available. Here again we’re moving in territory where the functionality isn’t exposed by Home Assistant, so one needs to create a helper that keeps count of unavailable entities, and an automation to force the template to refresh itself when entities get new labels - as expand doesn’t catch their inner dependency.
How to use
- Create a label
always_available - Create a new template sensor,
unavailable_sensors_countwith the template set to:{{ expand(label_entities('always_available'))| selectattr('state', 'in', ['unknown', 'unavailable'])| list| length }} - Create an automation that triggers on label changes on entities, and forces the template sensor to do a refresh. Like:
alias: "Hack: force update of template sensor when messing with labels"description: ""triggers:- trigger: eventevent_type: entity_registry_updatedconditions:- condition: templatevalue_template: "{{ 'labels' in trigger.event.data.changes or [] }}"actions:- action: homeassistant.update_entitymetadata: {}data:entity_id:- sensor.unavailable_sensors_countmode: single
- Create an automation that does what you want it to do when the entity is unavailable. For inspiration, here’s mine. It listens to the template sensor we created, and send a notification with all the unwell entities:
alias: Notify missing devicesdescription: ""triggers:- trigger: stateentity_id:- sensor.unavailable_sensors_countconditions: []actions:- action: notify.mobile_app_phonedata:message: >{{label_entities('always_available') | reject("has_value") | list |join(',') }}title: Devices that are unwellmode: single
- Assign
always_availableto those entities you want to be warned about.