Dynamic automation

Hello,
I have an automation to remind me to close my window. At the moment I have a switch and a timer, which I turn on and after some minutes the app is sending me a notification to close my window.
Now I have ordered some window contacts and I want to make this automation dynamic. Lets say I have multiple window contacts. And if one changes their states the app should send me a notification with the window friendly name or something like that.

I think the scenario above is inly working for one contact. So I thought about something like “threading” because if a second contact would trigger the automation the timer would reset, i think.

Do someone has a clue how to do this?

This is my config, I have placed an yaml file under packages:

# Timer for open windows (900 sec = 15 min)
timer:
  window_open:
    name: Time remaining (Window)
    duration: 900
    
# Switch for open window
switch:
  - platform: mqtt
    name: "Window open"
    state_topic: "zigbee2mqtt/bridge/config/open_window"
    command_topic: "zigbee2mqtt/bridge/config/open_window"
    payload_on: "true"
    payload_off: "false"

automation:
  # Automation to start timer when window is open
  - id: window_open
    alias: Window open
    trigger:
      platform: state
      entity_id: switch.window_open
      to: "on"
    action:
      service: timer.start
      entity_id: timer.window_open
  # Automation to stop timer when timer finished
  - id: close_window_nofitication
    alias: Notification for open window
    trigger:
      - platform: event
        event_type: timer.finished
        event_data:
          entity_id: timer.window_open
    action:
      - service: timer.cancel
        data:
          entity_id: timer.window_open
      - service: switch.turn_off
        entity_id: switch.window_open
      - service: notify.notify
        data:
          message: "Fenster schliessen"
          title: "Fenster schliessen"

Did you wanted a notification per window? if so you might need to create a timer per window…
As per the doc, TIMER.START restarts the timer is no duration is provided…

You could use the one automation still and trigger on each window and then have a service_template that starts the related timer…

1 Like

Perfect, I think this is exactly what I was looking for. Thank you very much!

You probably don’t even need a timer if you use for: in the state trigger.


  # Automation to stop timer when timer finished
  - id: close_window_nofitication
    alias: Notification for open window
    trigger:
      platform: state
      entity_id: switch.window_open
      to: "on"
      for:
        seconds: 900
1 Like

Thanks for your hints!

Now I have an additional question to this config.
The notify.notify service sends a notification to my android phone. But my wifes iphone doesn’t get the notification. Does anybody know why? Need iphones additional configuration?
Thanks in advance!

Have you looked into alerts? I did find them extremly helpful for reminders for open windows.