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

@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.

@finity I feel really bad for pulling you into this message. You’ve gone completely out of your way to help me. Please don’t feel obligated to help me. I don’t wantto take advantage of your kindness.

Having said that… and, taking a couple of steps back… Is there a reason why I can’t just stick to the original plan to use a single automation… using the repeat feature… then, use the until condition to stop it from repeating. If I understood right, the only reason we dumped the idea of using the repeat feature is because it would continue to repeat even if the condition isn’t true anymore.

we never dumped the repeat feature. :wink: You never said you wanted to use that method. You can use whichever method fits your needs and skill set.

And no I don’t mind helping at all and don’t feel obligated in any way. You’re just some “stranger on the internet” so If I wanted to walk away I could do so any time. :laughing:

At least your trying to work thru things and figure it out.

The people I really don’t like to help are the ones that don’t even try. And what’s worse are the ones who keep coming back over and over basically expecting you to write the code for them because they don’t want to put the effort in to learn. There’s been a few of those over the years.

Oh, and you don’t have to put the @username symbol in the post. Just hit the reply button in my post and it will notify me too.

Thanks. I was just trying to say is when you had mentioned that if I use the repeat feature, “…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 meantime.”
That’s what turned me off from using that method; looking for a way that doesn’t have that limitation.

I had missed Tom_I’s comment that “That’s where you would use “repeat until”. e.g. repeat until the door is closed.” until noticed it just a little while ago.

Anyway, I added the until condition to the script below. I’m not sure if it’s right… I haven’t had a chance to test it; but, I’m hoping it does what I think it does…

- 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
      until:
      - condition: state
        entity_id: binary_sensor.ecolink_door_window_sensor_sensor_2
        state: 'off'
  mode: single

1 Like

Looks reasonable.

The new automation functions are very new and I haven’t used them enough to be confident in them yet.

But looking at the docs I’m not sure that the “count:” option is available in the until structure. So you may be in a similar boat as you were before with it continuing to run until the door is closed instead of it running only 4 times.

But try it and see. It may just not be documented.

That example doesn’t pass Configuration Check on my system.
Screenshot from 2020-08-21 10-06-26

It should use only one way to repeat (loop) but it uses both count and until.

  • count is for repeating a given number of times (4) in your example.
  • until is for repeating until some condition is met.

Use one or the other, not both.

Thanks 123. I’ll remove the count and keep the until.

finity, using repeat… until works great (after removing count). This is been a great learning experience. I appreciate your guidance. I still plan on getting my alexa/google notifiers setup later in case I ever need to use HA alerts.

1 Like

Hi there,
I had a similar automation that neatly emitted a notification if the door was open that I wanted to adjust so that this notification is repeated if the door is left open.
Now that I’ve added ‘repeat until’, however, I get an error

type oInvalid config for [automation]: expected dict for dictionary value @ data['action'][1]['repeat']['sequence'][0]['data']. Got None extra keys not allowed @ data['action'][1]['repeat']['sequence'][0]['message']. Got None extra keys not allowed @ data['action'][1]['until']. Got None. (See ?, line ?).r paste code here

Here’s my automation file (entities\automations\kitchen\open_door.yaml :

alias: 'deur_open'
initial_state: 'on'
trigger:
  - platform: state
    entity_id: binary_sensor.sonoff_keuken_deur_01
    from: 'off'
    to: 'on'
    for: '00:00:30'

condition:
  - condition: state
    entity_id: binary_sensor.sonoff_keuken_deur_01
    state:  'off'

mode: single

action:
- service: media_player.volume_set
  data_template:
    entity_id: media_player.keuken
    volume_level: 0.8

- alias: herhaal bericht totdat de deur gesloten is
- repeat:
#    count: '4'
    sequence:
      - service: tts.google_say
        data:
          entity_id: media_player.keuken
          message: 'Hallo! Graag de deur dicht doen, anders wordt de verwarming uitgeschakeld'
      - delay: '00:00:10'  
  until:
    - condition: state
      entity_id: binary_sensor.sonoff_keuken_deur_01
      state:  off

Any help is appreciated

Hi @Plaatjesdraaier . I wish I knew how to help you with the error you’re getting. Does that error only happen after you add “repeat until”? Your code looks very similar to mine. This probably requires someone with a deeper knowledge of Home Assistant than me such as @finity

My original flle looks like this and is working fine:

alias: 'deur_open'
initial_state: 'on'
trigger:
  - platform: state
    entity_id: binary_sensor.sonoff_keuken_deur_01
    to: 'on'
    for: '00:0:30'
  - platform: state
    entity_id: binary_sensor.sonoff_hk_terras_deur
    to: 'on'
    for: '00:0:30'    

action:

  - service: media_player.volume_set
    data_template:
      entity_id: media_player.keuken
      volume_level: 0.8  
  - service: tts.google_say
    data:
      entity_id: media_player.keuken
      message: 'Hallo! Graag de deur dicht doen, anders wordt de verwarming uitgeschakeld'
mode: single

I tried to adjust the code so that the message is repeated using ‘repeat until’ but no luck so far.