Automation: (Loops) How to repeat an action 4 times ..at 90 second intervals

I have an automation (below) which will make an announcement/notification to my Alexa Echo Dot and Google Nest Mini when the back door is open longer than 7 minutes.

How can I make the automation repeat the announcement 4 times with 90 seconds wait between each announcement?

- id: '1597611826726'
  alias: Back Door open warning
  trigger:
  - entity_id: binary_sensor.ecolink_door_window_sensor_sensor_2
    for: 00:07:00
    from: 'off'
    platform: state
    to: 'on'
  action:
  - data:
      entity_id: media_player.living_room_speaker
      message: Please don't forget to close the back door!
    service: tts.google_translate_say
  - data:
      target: 
        - media_player.mka_net_echo_dot
      data:
        type: announce
      message: "Please don't forget to close the back door!"
    service: notify.alexa_media
  mode: single

Below, is my best attempt at doing this (which is definitely wrong). I’m hoping someone can check my syntax to see what I’m doing wrong:

- id: '1597611826726'
  alias: Back Door open warning
  trigger:
  - entity_id: binary_sensor.ecolink_door_window_sensor_sensor_2
    for: 00:07:00
    from: 'off'
    platform: state
    to: 'on'
  action:
    service: loop.loop
    data:
      loops: 4
      delay_after: 90
      actions:
        - data:
            entity_id: media_player.living_room_speaker
            message: Please don't forget to close the back door!
          service: tts.google_translate_say
        - data:
            target: 
              - media_player.mka_net_echo_dot
            data:
              type: announce
            message: "Please don't forget to close the back door!"
          service: notify.alexa_media
        mode: single

I have no idea where any of that service code even came from…

Were you just throwing stuff out there and hoped it worked or did you find that somewhere?

Anyway here are the docs for what you want to do.

You might be able to do this more simply with an alert which includes repeat notifications as an option. See https://www.home-assistant.io/integrations/alert/ for further details.

Thanks. Tried to use what I found from the below resource when searching for loops in automations:

Anyway, I’m still having trouble incorporating the Counted Repeat example given in the link you provided here with my automation below. I’d really appreciate assistance to repeat the below notifications 4 times with a 90 second delay between notifications:

- id: '1597611826726'
  alias: Back Door open warning
  trigger:
  - entity_id: binary_sensor.ecolink_door_window_sensor_sensor_2
    for: 00:07:00
    from: 'off'
    platform: state
    to: 'on'
  action:
  - data:
      entity_id: media_player.living_room_speaker
      message: Please don't forget to close the back door!
    service: tts.google_translate_say
  - data:
      data:
        type: announce
      message: Please don't forget to close the back door!
      target:
      - media_player.mka_net_echo_dot
    service: notify.alexa_media
  - data:
      message: Please don't forget to close the back door!
    service: notify.sms_family
  mode: single

Have you tried to follow the docs I linked and add any of the code there to your automation?

All you did above is restate the original code that you posted above already.

I don’t mind helping if you get stuck but you at least have to give it a try first.

I did try to make the automation based on the link you sent me. However, what I came up with looked so far from being correct, I didn’t post what I did. Anyway, below is what I came up with. Don’t laugh:

- id: '1597611826726'
  alias: Back Door open warning
  trigger:
  - entity_id: binary_sensor.ecolink_door_window_sensor_sensor_2
    for: 00:07:00
    from: 'off'
    platform: state
    to: 'on'
  action:
  - data:
      entity_id: media_player.living_room_speaker
      message: Please don't forget to close the back door!
      count: 4
      sequence:
        - delay: '00:01:30'
    service: tts.google_translate_say
  - data:
      data:
        type: announce
      message: Please don't forget to close the back door!
      count: 4
      sequence:
        - delay: '00:01:30'
      target:
      - media_player.mka_net_echo_dot
    service: notify.alexa_media
  mode: single

:laughing:

oh sorry… :wink:

no worries. We all start somewhere.

But it’s always best to give it a try first and try to work it and post what you tried.

anyway…

you only need the repeat code once in the actions.

Then you put the code for the actions under the “sequence” section.

something like:

  - id: '1597611826726'
    alias: Back Door open warning
    trigger:
    - entity_id: binary_sensor.ecolink_door_window_sensor_sensor_2
      for: 00:07:00
      from: 'off'
      platform: state
      to: 'on'
    action:
      - repeat:
          count: 4
          sequence:
            - data:
                entity_id: media_player.living_room_speaker
                message: Please don't forget to close the back door!
              service: tts.google_translate_say
            - data:
                data:
                  type: announce
                message: Please don't forget to close the back door!
                target:
                - media_player.mka_net_echo_dot
              service: notify.alexa_media
            - data:
                message: Please don't forget to close the back door!
              service: notify.sms_family
            - delay: '00:01:30'
    mode: single

Realize tho that once this triggers and starts the count the actions will continue to run for 4 repeats even if the door gets closed in the meatime. That might get annoying. (but maybe that’s the plan so they won’t forget to close the door the next time? :wink:)

If that’s not what you want then maybe the alert integration would be better.

2 Likes

Thank you very much. I didn’t realize I would still hear the notifications repeat even after closing the door. That’s definitely not what I’d want.

Below is the alert (alert integration) and respective automation I came up with as a better replacement for the above automation. I wasn’t sure how to specify the condition that the door has been open for 7 minutes. I know it has something to do with setting up an automation for input_boolean

Alert:

alert:
  back_door_open_warning:
    name: Back door open for 7 minutes
    message: "Please don't forget to close the back door!"
    done_message: Thank you for closing the door
    entity_id: entity_id: input_boolean.back_door_open_7_minutes
    state: 'on'
    repeat:
      - 4
    can_acknowledge: true
    skip_first: true
    notifiers:
      - tts.google_translate_say
      - notify.alexa_media
      - notify.sms_family

Automation:

 - id: '1597611826726'
    alias: back door open 7 minutes
    trigger:
    - entity_id: binary_sensor.ecolink_door_window_sensor_sensor_2
      for: 00:07:00
      from: 'off'
      platform: state
      to: 'on'
    action:
         
    mode: single

That’s where you would use “repeat until”. e.g. repeat until the door is closed.

2 Likes

First thing to point out is that you have an extra “entity_id” in the entity_id line.

But in regards to using an input boolean I think I would use a template binary sensor as suggested in the alert docs instead. That way there is no extra automation required.

so the binary sensor would be (I’m pretty sure I have it right):

binary_sensor:
  - platform: template
    sensors:
      door_open_for_7_minutes:
        value_template: "{{ is_state('binary_sensor.ecolink_door_window_sensor_sensor_2', 'on') and (as_timestamp(now()) - as_timestamp(states.binary_sensor.ecolink_door_window_sensor_sensor_2.last_changed))/60 > 7 }}"

its a bit complicated for a newbie but basically the first part is just checking that the sensor is on (for open I assume). then the second part is checking that the time now is more than 7 minutes since the state of the sensor changed. and since the requirement is that the sensor is on that means it will only be true 7 minutes after the sensor turned on and it will ignore any changes to off. Then if you close the door it will be false again.

@finity I just added the alert and the binary sensor to my configuration.yaml file. I also fixed the duplicate “entity_id line”. I’ll test later today. I’ll post here after I do that.

I’m glad you helped out with my binary_sensor template. The good news is I understand the syntax/logic you used in order to set up future time-related templates. I also know how to set up alerts as well, thanks to you. This has been a great learning experience. I also learned how to repeat sequences in Automatons in case I need to do that in the future. Thanks again for everything!

@finity Bad news. There was no alert or notifiers triggered after leaving the back door open for more than 7 minutes. Maybe, I did something wrong? I added the exact code below in my configuration.yaml, deleted my old (working) automation, then rebooted HA via Server Controls:

alert:
  back_door_open_warning:
    name: Back door open for 7 minutes
    message: "Please don't forget to close the back door!"
    done_message: "Thank you for closing the door"
    entity_id: binary_sensor.door_open_for_7_minutes
    state: 'on'
    repeat:
      - 4
    can_acknowledge: true
    skip_first: true
    notifiers:
      - tts.google_translate_say
      - notify.alexa_media

binary_sensor:
  - platform: template
    sensors:
      door_open_for_7_minutes:
        value_template: "{{ is_state('binary_sensor.ecolink_door_window_sensor_sensor_2', 'on') and (as_timestamp(now()) - as_timestamp(states.binary_sensor.ecolink_door_window_sensor_sensor_2.last_changed))/60 > 7 }}"

The entity binary_sensor.door_open_for_7_minutes state stayed at: off indefinitely (while leaving the back door open for over 7 minutes).

My original automation:

- id: '1597611826726'
  alias: Back Door open warning
  trigger:
  - entity_id: binary_sensor.ecolink_door_window_sensor_sensor_2
    for: 00:07:00
    from: 'off'
    platform: state
    to: 'on'
  action:
  - data:
      entity_id: media_player.living_room_speaker
      message: Please don't forget to close the back door!
    service: tts.google_translate_say
  - data:
      target: 
        - media_player.mka_net_echo_dot
      data:
        type: announce
      message: "Please don't forget to close the back door!"
    service: notify.alexa_media
  mode: single

if you place the following into the template editor what do you see when you open & close the door:

{{ states('binary_sensor.ecolink_door_window_sensor_sensor_2') }}

{{ is_state('binary_sensor.ecolink_door_window_sensor_sensor_2', 'on') }}

{{ as_timestamp(states.binary_sensor.ecolink_door_window_sensor_sensor_2.last_changed)) }}

{{ (as_timestamp(now()) - as_timestamp(states.binary_sensor.ecolink_door_window_sensor_sensor_2.last_changed))/ 60 }}

{{ is_state('binary_sensor.ecolink_door_window_sensor_sensor_2', 'on') and (as_timestamp(now()) - as_timestamp(states.binary_sensor.ecolink_door_window_sensor_sensor_2.last_changed))/60 > 7 }}

for all of these when you open & close the door you will need to update the page by putting and extra space between the lines ore hit the return key on your keyboard for it to pick up the changes.

with the first one you should see the result change from on to off and back

for the second you should see the state change from true to false and back

for the third you should see the time change to the time you operated the door either opened or closed

for the fourth you should see the number of minutes that have passed since the door was either last opened or closed.

The last one just puts it all back together again and you should see true or false.

I had to change the below line (there was extra right parenthesis):

{{ as_timestamp(states.binary_sensor.ecolink_door_window_sensor_sensor_2.last_changed)) }}

to

{{ as_timestamp(states.binary_sensor.ecolink_door_window_sensor_sensor_2.last_changed) }}

These are the results after opening the door for 1 minute:

on

True

1597852784.137449

1.030290965239207

False

Results after opening the door for around 27 minutes:

on

True

1597852784.137449

26.89650390148163

True 

The above tests seem to return correct results. I’m not sure if this is relevant… but, when I check the entity state of binary_sensor.door_open_for_7_minutes from my existing configuration (after keeping the door open for over 7 minutes), it shows a state of off, instead of true:

entitystate

Existing Configuration:

binary_sensor:
  - platform: template
    sensors:
      door_open_for_7_minutes:
        value_template: "{{ is_state('binary_sensor.ecolink_door_window_sensor_sensor_2', 'on') and (as_timestamp(now()) - as_timestamp(states.binary_sensor.ecolink_door_window_sensor_sensor_2.last_changed))/60 > 7 }}"

alert:
  back_door_open_warning:
    name: Back door open for 7 minutes
    message: "Please don't forget to close the back door!"
    done_message: "Thank you for closing the door"
    entity_id: binary_sensor.door_open_for_7_minutes
    state: 'on'
    repeat:
      - 4
    can_acknowledge: true
    skip_first: true
    notifiers:
      - tts.google_translate_say
      - notify.alexa_media

Hey, I’m really sorry to have led you down this wild goose chase.

I seem to have been bitten by the “template entity” bug.

Templates are only ever evaluated when one of the associated entities updates. But now() isn’t an entity so the now()) time changing doesn’t trigger an evaluation of the template so even if the conditions of the template are met then the template never evaluates therefore never sets the binary sensor state to on. You can fix that situation by adding an entity_id to watch for a change in to force the sensor to update.

I knew it was that way for regular analog sensors but wasn’t even thinking about that in relation to the binary sensor.

That said there are two ways you can proceed.

The first and recommended way is to add the sensor.time entity_id to the binary sensor. That will cause the template to re-evaluate every minute. But you’ll have to ensure you add the date_time sensor (and all of it’s different entities) to your HA config. It isn’t hard and it should be standard anyway since it’s immensely useful. Just like in this case.

The downside to this method will be that it won’t necessarily evaluate at exactly seven minutes that the door is open since the door will open sometime within the minute and the sensor time will only update every minute at the 0 seconds mark. So it’s possible that the binary sensor won’t update at exactly seven minutes but may be up to almost a minute later. It probably isn’t a big deal in this situation but it might be in some other use case. So keep it in mind.

The second way is to create an input boolean and set it’s state with an additional two automations and completely forego the binary sensor. Then you can use the state of the input boolean in the alert config instead of the binary sensor. The benefit here is that the alert will trigger at exactly 7 minutes. The downside is you have to create an input boolean and two more automations.

here are both setups…

using the sensor.time:

sensor:
  - platform: time_date
    display_options:
      - 'time'
      - 'date'
      - 'date_time'
      - 'date_time_utc'
      - 'date_time_iso'
      - 'time_date'
      - 'time_utc'

binary_sensor:
  - platform: template
    sensors:
      door_open_for_7_minutes:
        entity_id: sensor.time
        value_template: "{{ is_state('binary_sensor.ecolink_door_window_sensor_sensor_2', 'on') and (as_timestamp(now()) - as_timestamp(states.binary_sensor.ecolink_door_window_sensor_sensor_2.last_changed))/60 > 7 }}"

then just use the alert as above.

or using the input boolean:

input_boolean:
  door_open_for_7_minutes:

automation:
  - alias: turn boolean on
    trigger:
      - platform: state
        entity_id: binary_sensor.ecolink_door_window_sensor_sensor_2
        to: 'on'
        for:
          minutes: 7
    action:
      - service: input_boolean.turn_on
        entity_id: input_boolean.door_open_for_7_minutes

  - alias: turn boolean off
    trigger:
      - platform: state
        entity_id: binary_sensor.ecolink_door_window_sensor_sensor_2
        to: 'off'
    action:
      - service: input_boolean.turn_off
        entity_id: input_boolean.door_open_for_7_minutes

alert:
  back_door_open_warning:
    name: Back door open for 7 minutes
    message: "Please don't forget to close the back door!"
    done_message: "Thank you for closing the door"
    entity_id:  input_boolean.door_open_for_7_minutes
    state: 'on'
    repeat:
      - 4
    can_acknowledge: true
    skip_first: true
    notifiers:
      - tts.google_translate_say
      - notify.alexa_media

hopefully one of those options works for you.

again, sorry for leading you down a rabbit hole.

1 Like

Wow, thank you for all your help! If there are no performance/reliability differences between either method, I would rather choose the method requiring two automations + input_boolean + alert. I’ll give it a shot and let you know how it goes…

1 Like

@finity I just setup the input_boolean method. It is almost working!

input_boolean.door_open_for_7_minutes
Will switch to on after the door has been open for 7 minutes. It switches to off when the door is closed.

alert.back_door_open_warning
Will switch to on when the input_boolean entity above also turns on. It will switch to idle when the door is closed.

However, I don’t actually get any kind of alert in the Home Assistant GUI. I also won’t hear any voice notifications on my Alexa/Google devices.

Any ideas what we might be missing? Maybe, I did something wrong? Below is EXACTLY what’s in my configuration.yaml and automation.yaml, respectively:

configuration.yaml:

input_boolean:
  door_open_for_7_minutes:

alert:
  back_door_open_warning:
    name: Back door open for 7 minutes
    message: "Please don't forget to close the back door!"
    done_message: "Thank you for closing the door"
    entity_id:  input_boolean.door_open_for_7_minutes
    state: 'on'
    repeat:
      - 4
    can_acknowledge: true
    skip_first: true
    notifiers:
      - tts.google_translate_say
      - notify.alexa_media

automation.yaml

- id: '1597954272382'
  alias: turn boolean on for back door
  trigger:
  - entity_id: binary_sensor.ecolink_door_window_sensor_sensor_2
    for: 0:07:00
    platform: state
    to: 'on'
  action:
  - entity_id: input_boolean.door_open_for_7_minutes
    service: input_boolean.turn_on
  mode: single
- id: '1597954540466'
  alias: turn boolean off on for back door
  trigger:
  - entity_id: binary_sensor.ecolink_door_window_sensor_sensor_2
    platform: state
    to: 'off'
  action:
  - entity_id: input_boolean.door_open_for_7_minutes
    service: input_boolean.turn_off
  mode: single

I would love to get this method working, if possible, since it’s very precise. If you don’t think it’s possible to get this method working, I can go ahead and try the other solution.

EDIT: I also tried changing the alert’s skip_first: true to skip_first: false; however, I still don’t get an alert (GUI or voice messages). I would like to get the alert immediately after the door has been open for 7 minutes. I’m not sure which setting is the right setting.

You may have two issues.

the first is that the alexa_media notifier needs extra config options that the alert doesn’t provide. However look at the docs for the alert and it gives you a solution for that problem where it talks about creating notifier groups with the required config.

the second is possibly the same as the first but I can’t know for sure since I don’t use any google devices. You may have to create a notifier group for that one too.

for testing sake just to make sure the alert is working then just set up a basic text based notification integration (I use Pushbullet and it works great). Then if you can get basic text alerts then you know you just have to figure out how to configure the alert to use voice announcements. if the text alerts don’t work either then you know you have to look further upstream.

I’m not sure if this matters; but, I can already send notifications to both notifiers using my original automation in the OP. Are the missing parameters in my original Automation?

Also, isn’t the alert that I setup supposed to at least show an alert in the HA GUI even if both notifiers are missing parameters? When you say I have to look further upstream, where would you suggest I can look?

Anyway thanks for all your help. You’ve already helped me so much. I appreciate it!

yes.

these:

    data:
      entity_id: media_player.living_room_speaker
      message: Please don't forget to close the back door!

    - data:
      target: 
        - media_player.mka_net_echo_dot
      data:
        type: announce
      message: "Please don't forget to close the back door!"

Not that I know of.

The notifications you get in the HA GUI are called persistent_notifications. I don’t think the alert creates those.

I wouldn’t bother with that until you know that the alert itself isn’t working.

that’s why I suggested setting up an easier to work with notification integration that doesn’t take all of the extra parameters.

I’m pretty sure that your issues are that you are missing the extra parameters for the TTS services you are using.