Contents

Let there be no lights

Walk around to turn off all the lights in the home? Tedious.

Remembering to turn off all the lights in the home? Impossible.

For ages, I have had a script in Home Assistant simply called “TurnOffHome” that turns off most of the lights and ensures that things like the Moccamaster are turned off. It is triggered by a switch at the door that we press when we leave the house in the morning, and by switches next to the beds when we go to sleep. 

The key here is “most of the lights”, as outdoor lights, some window lights, and toilet lights should not be turned off by this. This script had the simplest of designs, a long list of all the lights that should be turned off. For every new lamp added to the system, this script had to be manually updated. There has to be a better way, with templates? But I have never got around to fully grasping templating in Home Assistant, and now that I have realised how competent Le Chat is with it, I never will.

Turn of most lamps, templated

The design is simple, there is a helper/group called light.turnoffhomeexclude where I now can add all my excluded lights and a small templated service call that replaces my old list of lamps as of below. 

service: light.turn_off
target:
  entity_id: >
    {% set excluded_lights = state_attr('light.turnoffhomeexclude', 'entity_id')
    %} {% set all_lights = states.light | map(attribute='entity_id') | list %}
    {{ all_lights | reject('in', excluded_lights) | list }}

The next part is a service call to light.turn_off with a template for the entity_id. It will create a set with the entity_id of all the lights and the excluded lights, then it will remove the excluded lights from the set with all the lights and that set is what is sent to the service. Simple when looking at, but I could never write this myself in a reasonable time. I updated my script and like a well-oiled machine lights turned off left and right, including the lamp in front of me that I were in the middle of adding to a manual list when I got to the breaking point of manual labour.

Natural trigger points for automations

In the end, an automation or script will never feel smarter than how naturally it is triggered. I have opted to trigger this via physical buttons. It is exposed to HomeKit and the Home Assistant app but those never felt natural.

The front door trigger sits next to the alarm panel. A single push starts an automation that checks that all the doors and windows are closed before turning off the house, if something is open it will instead make the hallway light red to prompt a door check. As every good automation needs and override, a long press on the button forces a trigger of the script, regardless of the state of the doors and windows.

The bedside trigger for this is built into the switches used for bed lamps, a long press on off while the bedside lamp is off will trigger the script. The natural flow at night if you are the last person going to sleep is a press to turn off your lamp and then hold it to turn off the whole house.

These two interactions with this automation has become a natural part of life, you reach for the button and know the house is ready for the night or to be left alone for some time.