Home Assistant: Notify when a light was left on and no one is at home
I'm using Home Asisstant for about a year now, and this is one of those things that I wanted to add for a long time, but have been postponing it. Until a few days ago, when I've arrived home and found out that one of the lights was on all day. Actually, after checking the Home Assistant log, it was my cat that triggered a motion sensor, that turned the light on (more about cats and motion sensors in a future post).
The basic idea here is that if I'm not at home for more than 30 minutes and a light is on (or turns on), I get a notification on my phone. For sending notifications from Home Assistant to my phone, I'm using the Pushbullet component.
I already had a group were I list all the controllable lights in the house and I wanted to be able to use this group directly in the automation, so I don't have to define every light in two separate places. Also, I want to be able to exclude some lights from triggering a notification (but I don't want to remove them from the group). Unfortunately, if I use the group directly as a trigger for the automation, the automation will fire when the group turns on, which happens when one light on the group turns on. Since I want to exclude some lights, those could already be on, thus automation would not fire if another light turns on after that. The solution, shared by user patrickeasters on the Home Assistant's Discord chat, is using YAML anchors! Those allow us to "copy" pieces of a YAML into another place in the same YAML. Below is an excerpt of the lights group. Notice the &lights_group_entities part! That is the YAML anchor which will be used in the automation.
Now, lets look at the automation:
This is a pretty simple automation with two state triggers: one when there is no one home for 30 minutes (using a template binary sensor that goes off when no one is home), and another when any light in the lights group turns on. This is where the YAML anchor is used, with the *lights_group_entities reference. The reference inserts the list of entities directly on the entity_id attribute, making the trigger look at each light's state individually. Since I only want to receive a notification when both conditions are met, I use the same two triggers as conditions, but here I can use the group's state.
Finally, the automation calls the notify_lights_on python_script. This script gets all lights from the lights group that are on, gets their friendly_name and uses Pushbullet to send a notification with them listed on it. The script also has a static list of lights that I don't mind leaving on while I'm away, ignoring them.
Let me know if you have any questions, thoughts or suggestions for improvement!