Please help me with automation while loop

So im trying to get reminded every minute we leave the pool gate open via Alexa -

I have no idea why this doesnt work, it looks good in the GUI, the actions work, the trigger works (first action fires) and I have tested this with the pool gate open and the ‘WHILE’ config returns back true… but its action never fires?

  alias: Pool Gate Opened
  description: ''
  trigger:
  - type: opened
    platform: device
    device_id: e5d17f6523d82e3f159ebb0a2ea4c505
    entity_id: binary_sensor.pool_gate_multipurpose_sensor_contact
    domain: binary_sensor
  condition: []
  action:
  - service: notify.alexa_media
    data:
      message: Pool gate has been opened
      target:
      - media_player.main_echo_dot
      - media_player.bedroom_echo_dot
      data:
        method: all
        type: announce
  - repeat:
      while:
      - type: is_open
        condition: device
        device_id: e5d17f6523d82e3f159ebb0a2ea4c505
        entity_id: binary_sensor.pool_gate_multipurpose_sensor_contact
        domain: binary_sensor
        for:
          hours: 0
          minutes: 1
          seconds: 0
      sequence:
      - service: notify.alexa_media
        data:
          message: Pool gate is open
          target:
          - media_player.main_echo_dot
          - media_player.bedroom_echo_dot
          data:
            method: all
            type: announce
  mode: single
alias: Pool Gate Opened
  description: ''
  trigger:
  - platform: state
    entity_id: binary_sensor.pool_gate_multipurpose_sensor_contact
    from: 'off'
    to: 'on'
  condition: []
  action:
  - repeat:
      while: "{{ is_state('binary_sensor.pool_gate_multipurpose_sensor_contact', 'on') }}"
      sequence:
      - service: notify.alexa_media
        data:
          message: Pool gate is open
          target:
          - media_player.main_echo_dot
          - media_player.bedroom_echo_dot
          data:
            method: all
            type: announce
      - delay: '00:01:00'
  mode: single
1 Like

THANKYOU!!!

the GUI was leading me astray. chucked in volume as well for anyone interested, works a treat:-

- id: '1655878094313'
  alias: Pool Gate Opened
  description: ''
  trigger:
  - platform: state
    entity_id: binary_sensor.pool_gate_multipurpose_sensor_contact
    from: 'off'
    to: 'on'
  condition: []
  action:
  - repeat:
      while:
      - condition: template
        value_template: '{{ is_state(''binary_sensor.pool_gate_multipurpose_sensor_contact'',
          ''on'') }}'
      sequence:
      - service: media_player.volume_set
        data:
          entity_id: media_player.main_echo_dot
          volume_level: '0.6'
      - service: media_player.volume_set
        data:
          entity_id: media_player.bedroom_echo_dot
          volume_level: '0.6'
      - service: notify.alexa_media
        data:
          message: Pool gate is open
          target:
          - media_player.main_echo_dot
          - media_player.bedroom_echo_dot
          data:
            method: all
            type: announce
      - delay: 00:01:00
  mode: single
1 Like

You may wish to consider moving the volume service calls out of the repeat - while and placing them just before the repeat - while .

The reason is because there’s not much need to repeatedly adjust the volume for each iteration of the repeat - while (just once prior to entering the repeat - while ought to be sufficient).

yeh, I thought about that, however someone might turn the music down if its loud and then we will forget. However, if I wanted to do it, would it be this:

- id: '1655878094313'
  alias: Pool Gate Opened
  description: ''
  trigger:
  - platform: state
    entity_id: binary_sensor.pool_gate_multipurpose_sensor_contact
    from: 'off'
    to: 'on'
  condition: []
  action:
      - service: media_player.volume_set
        data:
          entity_id: media_player.main_echo_dot
          volume_level: '0.6'
      - service: media_player.volume_set
        data:
          entity_id: media_player.bedroom_echo_dot
          volume_level: '0.6'
  - repeat:
      while:
      - condition: template
        value_template: '{{ is_state(''binary_sensor.pool_gate_multipurpose_sensor_contact'',
          ''on'') }}'    
  sequence:
      - service: notify.alexa_media
        data:
          message: Pool gate is open
          target:
          - media_player.main_echo_dot
          - media_player.bedroom_echo_dot
          data:
            method: all
            type: announce
      - delay: 00:01:00
  mode: single

And when it goes back to the music (or whatever), it will blast your ears off, because you changed the volume, but didn’t set it back to the value that was set by hand before (assuming someone had a reason, to lower the volume). :smiley:

You could solve this by setting a scene and safe the volume level from before. But that seems to be a different thing here. :slight_smile:

Your new approach looks good, despite the indentation is out of hand. After action anything that belongs to the action part needs to be indented correctly on the same level (two spaces for -repeat). So -repeat and sequence need to be indented. :slight_smile:

But just to note: what you’re doing here is a perfect fit for the alert component in HA. It does what you want without the need for templates and such, and it gives you the possibility to react on the state change. With an alert, you can send a message for the “open” state, and the alert will stay on, as long as you don’t change the state. But when it turns off, you can automatically send a “solved event” message.

For example: your garage door is open longer than two minutes, you get a notification (in your case as TTS), that will be repeated until you close the garage door. Then it will send a different notification (here TTS), telling you “the garage door is closed again”. :slight_smile:

EDIT:
Something like this:

alert:
  pool_gate_open:
    name: Pool gate is open
    done_message: Pool gate is now closed
    entity_id: binary_sensor.pool_gate_multipurpose_sensor_contact
    state: "on"
    repeat: 1 # repeat every (one) minute
    can_acknowledge: false
    skip_first: false # to get the first notification directly, if the gate opens
    notifiers:
      - your_tts_notifier