Automation tip: random time + notification

Hi, would like to have some assistance in setting this up:

  1. Automation starts on trigger
  2. set random delay of x minutes to variable
  3. send actionable notification which says “steps will run in x minutes”, and which includes a “cancel” button
  4. start delay set in the variable
  5. if the cancel button on the notification is not pressed before the end of the delay, continue with the actions - ELSE - finish other automation steps so it is ready for the next time the trigger occurs

Thx!

What triggers the automation?

What is the maximum length of the delay?

An actionable notification for iOS or Android?

If the cancel button is pressed within the delay period you said it should finish “other automation steps”. What are the other steps and how do they differ from the ones executed when the cancel button is not pressed?

I’d recommend giving it a try yourself and then if you get stuck at a certain point post what you have so far here and ask for help then.

Hi again, thanks for responding, things have been a bit hectic around here.

Automation is used to turn off the lights in the evening, but I’d like to build in a “warning” message that allows me to prevent the lights from turning off (eg. in the case of guests, you never know, once that is allowed again)

This is my current automation:

---
# Turn off the lights in the living room around midnight, and send notification
#
alias: Turn Off Evening Lights
id: turn_off_evening_lights
trigger:
- at: '23:30:00'
  platform: time
action:
- delay: '{{ (range(0, 1)|random|int) }}:{{ (range(1, 59)|random|int) }}:00'
- entity_id:
  - 'group.downstairs_lights'
  service: switch.turn_off
- condition: state
  entity_id: group.downstairs_lights
  state: 'on'
- data:
    message: 'Living room lights turned off automatically'
  service: notify.mainplatform

Update: got it working perfectly, hope this helps someone:

I send the message at 23:00 that lights will turn of at X.

  1. when they are turned off manually, then persistant message is cleared, and a “someone else did it” is sent
  2. if I press the do it now, lights are turned off instantly
  3. if I press leave them on, automation ends and lights stay on
  4. if no action, then default action happens at lights off time

** edited for timezone issue **

---
# Turn off the lights in the living room at random time between half hour before and half hour after midnight.
#
alias: Turn Off Evening Lights
id: turn_off_evening_lights
mode: single
# max_exceeded: silent
variables:
  delay: '{{ (range(30, 91)|random|int) * 60  }}'
  lightsofftime: '{{ (now().timestamp() | int + (delay)) | timestamp_custom("%H:%M:%S") }}'
  action_now: '{{ "do_it_now" }}'
  action_not: '{{ "dont_do_it" }}'

trigger:
  - platform: time
    at: '23:00:00'
action:
  - alias: Notify about automatic light out time
    service: notify.mobile_app_mi_8 
    data:
      title: Lights Out!
      message: >
        Lights will turn off at {{ lightsofftime }}, so the delay is {{ (delay | timestamp_custom("%H:%M:%S", False)) }}.
      data:
        tag: evening_lights_off_notif
        sticky: true
        persistent: true
        actions:
          - title: Do it now
            action: '{{ action_now }}'
          - title: Leave them on
            action: '{{ action_not }}'
  - wait_for_trigger:
      - platform: event
        event_type: mobile_app_notification_action
        event_data:
          action: '{{ action_now }}'
      - platform: event
        event_type: mobile_app_notification_action
        event_data:
          action: '{{ action_not }}'
      - platform: state
        entity_id: group.downstairs_lights
        to: 'off'
    timeout: >
      {{ (delay | timestamp_custom("%H:%M:%S", False)) }}
  - choose:
      - conditions: '{{ states("group.downstairs_lights") == "off" }}'
        sequence:
          - service: notify.mobile_app_mi_8 
            data:
              title: Easy peasy
              message: >
                someone else did it for you
              data:
                tag: evening_lights_off_notif_2
          - service: notify.mobile_app_mi_8 
            data:
              message: clear_notification
              data:
                tag: evening_lights_off_notif
      - conditions: '{{ wait.trigger.event.data.action == action_now }}'
        sequence:
          - service: switch.turn_off
            entity_id: group.downstairs_lights
          - service: notify.mobile_app_mi_8 
            data:
              message: clear_notification
              data:
                tag: evening_lights_off_notif
      - conditions: '{{ wait.trigger.event.data.action == action_not }}'
        sequence:
          - service: notify.mobile_app_mi_8 
            data:
              message: clear_notification
              data:
                tag: evening_lights_off_notif
    default:
      - service: switch.turn_off
        entity_id: group.downstairs_lights
      - service: notify.mobile_app_mi_8 
        data:
          message: clear_notification
          data:
            tag: evening_lights_off_notif
9 Likes