I made an automation to detect when a light is on (or turns on) after I’m away for more than 30 minutes. It is nothing really advanced, but I think it provides a nice example.
The group with all my lights:
lights:
name: Lights
entities: &lights_group_entities
- light.office_ceiling_1
- light.office_ceiling_2
- light.yeelight_desk_bulb
- light.yeelight_desk_strip
- switch.bedroom_strip
- light.living_room_ceiling_r
- light.living_room_ceiling_l
- switch.kitchen_ceiling
The automation:
- alias: Lights - Notify if no one home and lights are on
trigger:
- platform: state
entity_id: binary_sensor.someone_home
to: 'off'
for:
minutes: 30
- platform: state
entity_id: *lights_group_entities
to: 'on'
condition:
condition: and
conditions:
- condition: state
entity_id: binary_sensor.someone_home
state: 'off'
for:
minutes: 30
- condition: state
entity_id: group.lights
state: 'on'
action:
- service: python_script.notify_lights_on
The python_script that checks the lights, ignores some of them, and sends a pretty notification:
lights_group = 'group.lights'
excluded = ['light.yeelight_desk_bulb', 'light.yeelight_desk_strip']
entities_on = []
for entity_id in hass.states.get(lights_group).attributes['entity_id']:
if hass.states.get(entity_id).state is 'on' and entity_id not in excluded:
entities_on.append(hass.states.get(entity_id).attributes["friendly_name"])
if len(entities_on) > 0:
notification_tilte = "Some lights are on - Home Assistant"
notification_message = "The following lights are on: " + ', '.join(entities_on)
hass.services.call('notify', 'pushbullet', {'title' : notification_tilte, 'message': notification_message})
Hope it is useful for someone. More info here: https://ledstripsandcode.blogspot.com/2017/12/home-assistant-notify-when-light-was.html