Repeat until input_button is pressed

I create an input_button helper called input_button.clear_alarm and my idea is to use it to break alarm loops create using a Repeat Until loop.

This is the repeat until loop:

repeat:
  until:
    - condition: or
      conditions:
        - condition: state
          entity_id: binary_sensor.water_leak_sensors
          state: 'off'
        - condition: state
          entity_id: input_button.clear_alarm
  sequence:
    - service: media_player.play_media
      data:
        media_content_id: /local/audio/internal_alarm.wav
        media_content_type: music
        enqueue: play
      target:
        entity_id: media_player.house_announcement
    - service: tts.google_translate_say
      data:
        entity_id: media_player.house_announcement
        message: >-
          Water leak detected in {{ trigger.to_state.attributes.friendly_name
          }}!
        cache: true
    - delay:
        hours: 0
        minutes: 0
        seconds: 10
        milliseconds: 0

When I try to save it complains that I am not defining the input_button.clear_alarm state and I don know what to use since the button press state is the time stamp of when it was pressed.

I tried to use this template {{ as_timestamp(states('input_button.clear_alarm')) == as_timestamp(now()) }} but now() is never exactly equal to the time stamp of the button press.

Just make a Template Condition that checks if the time of the most recent button press is within the last 15 seconds.

repeat:
  until:
    - condition: or
      conditions:
        - condition: state
          entity_id: binary_sensor.water_leak_sensors
          state: 'off'
        - "{{ now() - states('input_button.clear_alarm') | as_datetime < timedelta(seconds=15) }}"

It could be shorter than 15 seconds but your repeat contains a 10 second delay so we have to take that into account.

1 Like

Forgot about the timedelta! I tried something similar, but was struggling! Thanks @123!!!

1 Like