Actionable Notification for Lights Left on for 30+ Minutes

Hi all,

I have an actionable notification working which will fire when a light has been left on for 30 minutes, and the action takes me to a dashboard where I can turn off the light. However it’s currently got a few issues:

  • It tells me all the lights that are currently on, not those that have been on for the set time
  • I’d like to be able to turn the offending light off using the notification, without having to go into the app

I have a feeling this will need templates and whilst I’ve built a few to support my automations, I’ve not done any time-based ones so not sure where to begin!

My current automation (simplified to not have all my lights in as triggers) is:

alias: Lights Left On - Notification
description: ""
trigger:
  - platform: device
    type: turned_on
    device_id: 4e5ba4fe7a391c7f8f18de7ac8a98cf4
    entity_id: light.dining_room_lights
    domain: light
    for:
      hours: 0
      minutes: 30
      seconds: 0
  - platform: device
    type: turned_on
    device_id: d1afa681e9d78c626e0c81311fe967de
    entity_id: light.downstairs_toilet_lights
    domain: light
    for:
      hours: 0
      minutes: 30
      seconds: 0
condition: []
action:
  - service: notify.notify
    data:
      message: |-
        {{ expand('light.lights')
           | selectattr('state', 'eq', 'on') 
           | map(attribute='name') 
           | list 
           | join (', ')
        }}
      title: Lights On For 30 Mins+
      data:
        ttl: 0
        priority: high
        actions:
          - action: URI
            title: Open Lights Dashboard
            uri: /lovelace-mushroom/lightson
        tag: lights-on
  - wait_for_trigger:
      - platform: state
        entity_id:
          - sensor.pixel_6_pro_app_importance
        to: foreground
  - service: notify.notify
    data:
      message: clear_notification
      data:
        ttl: 0
        priority: high
        tag: lights-on
mode: single

To know which trigger was activated (and therefore the entity linked with it) you can use {{ trigger.entity }} which will give you which light triggered the automation … but an easier way is to use variables like this:

alias: Lights Left On - Notification
description: ""
trigger:
  - platform: device
    type: turned_on
    device_id: 4e5ba4fe7a391c7f8f18de7ac8a98cf4
    entity_id: light.dining_room_lights
    domain: light
    for:
      hours: 0
      minutes: 30
      seconds: 0
    variables:
       message: "the message you want to sent"
  - platform: device
    type: turned_on
    device_id: d1afa681e9d78c626e0c81311fe967de
    entity_id: light.downstairs_toilet_lights
    domain: light
    for:
      hours: 0
      minutes: 30
      seconds: 0
    variables:
       message: "another  message you want to sent"
condition: []
action:
  - service: notify.notify
    data:
      message: |-
        {{ message
        }}
      title: Lights On For 30 Mins+
      data:
        ttl: 0
        priority: high
        actions:
          - action: URI
            title: Open Lights Dashboard
            uri: /lovelace-mushroom/lightson
        tag: lights-on
1 Like

Thank you - is it also possible to use variables to define the action for the notification too, using the same logic?

So presumably that would be including ‘action: […]’ under the variables section for each light, and then using {{ action }} in the appropriate place for the notify service?

to be tested but services can be composed of trigger variables like for example (I am using this a lot):

  - service: input_boolean.turn_{{ trigger.to_state }}

so it should work with other variables as well like (if action is a variable defined before) but to be tested:

  - service: {{ action }}
1 Like