Automation that sends an alarm if a light is still on (at night)

What would be the code for the subject. I have around 20-25 lights, wish to have an automation that (for example) send me a message/alarm at midnight, in case I forgot a light on, somewhere

For a starting point I’d create a group called all_lights then at midnight have an automation that alerts if they’re on:

trigger:
  platform: state
  state: 'on'
  entity_id: group.all_lights
action:
  service: notify.yournotifier
  data:
    message: 'You left a light on'

Using templates, you could probably make that smarter - maybe group by room, have all the rooms as individual triggers, since triggers are an or, and then using the triggering entity in the message. Don’t have enough experience of templates to know how easy that is though.

but like this is enough ONE light ON to have the action?

Yes, because once one member of a group is on (or home, etc) then the group takes on that status.

Hi,

I like this, what would you need to add so that a notification is sent to tell you exactly which lights are still on ?

A quick Google found me this thread, which suggests that if you made it a list of trigger conditions you could make it work:

trigger:
  - platform: state
    state: 'on'
    entity_id: light.first_light
  - platform: state
    state: 'on'
    entity_id: light.second_light
action:
  service: notify.yournotifier
  data:
    message: 'You left {{ trigger.entity_id|replace('light.', '')|replace('_', ' ') }} on'

Do take the time to read the whole thread though. There’s one in there that iterates through everything, which you could use to get a list of all the offending lights.

I would think you would want the trigger to be time (midnight)
Condtion would be group,lights is on

and the message template would look something like this, It would only tell you the lights that are on.

{% for state in states.light if state.state == 'on'-%}
  {%- if loop.first %}The {% elif loop.last %} and the {% else %}, the {% endif -%}
  {{ state.name | lower }} is {{state.state}}
{%- endfor -%}.