Door contact automation stops when I use repeat-delay

Hi,

I’m struggling to get an automation to finish with my garage door

Basically, I’ve got a door contact that announces on 2 speakers that it is open, and then when its closed

I’ve used an IF/THEN/ELSE statement to allow this. which i can get to work with a simple 1 open and 1 close statement

However, when I add a repeat while door open with a delay to keep home assistant saying on speakers the door is open after a set time, it stops the “else” statement “garage door is closed”

This isn’t the end of the world but i’d like to know why and how to fix for future automations

any ideas?

Thanks

alias: "garage door "
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.garage_door_sensor_door
    from: "off"
    to: "on"
  - platform: state
    entity_id:
      - binary_sensor.garage_door_sensor_door
    from: "on"
    to: "off"
condition: []
action:
  - if:
      - condition: state
        entity_id: binary_sensor.garage_door_sensor_door
        state: "on"
    then:
      - repeat:
          sequence:
            - service: media_player.play_media
              target:
                entity_id: media_player.max_s_speaker
              data:
                media_content_id: >-
                  media-source://tts/cloud?message=The+Garage+door+is+open&language=en-US&voice=JennyNeural
                media_content_type: provider
              metadata:
                title: The Garage door is open
                thumbnail: https://brands.home-assistant.io/_/cloud/logo.png
                media_class: app
                children_media_class: null
                navigateIds:
                  - {}
                  - media_content_type: app
                    media_content_id: media-source://tts
                  - media_content_type: provider
                    media_content_id: >-
                      media-source://tts/cloud?message=The+Garage+door+is+open&language=en-US&voice=JennyNeural
            - service: media_player.play_media
              target:
                entity_id: media_player.kitchen_display
              data:
                media_content_id: >-
                  media-source://tts/cloud?message=The+Garage+door+is+open&language=en-US&voice=JennyNeural
                media_content_type: provider
              metadata:
                title: The Garage door is open
                thumbnail: https://brands.home-assistant.io/_/cloud/logo.png
                media_class: app
                children_media_class: null
                navigateIds:
                  - {}
                  - media_content_type: app
                    media_content_id: media-source://tts
                  - media_content_type: provider
                    media_content_id: >-
                      media-source://tts/cloud?message=The+Garage+door+is+open&language=en-US&voice=JennyNeural
            - delay:
                hours: 0
                minutes: 1
                seconds: 0
                milliseconds: 0
          until:
            - condition: state
              entity_id: binary_sensor.garage_door_sensor_door
              state: "off"
    else:
      - service: media_player.play_media
        target:
          entity_id: media_player.max_s_speaker
        data:
          media_content_id: >-
            media-source://tts/cloud?message=The+Garage+door+is+closed&language=en-US&voice=JennyNeural
          media_content_type: provider
        metadata:
          title: The Garage door is closed
          thumbnail: https://brands.home-assistant.io/_/cloud/logo.png
          media_class: app
          children_media_class: null
          navigateIds:
            - {}
            - media_content_type: app
              media_content_id: media-source://tts
            - media_content_type: provider
              media_content_id: >-
                media-source://tts/cloud?message=The+Garage+door+is+closed&language=en-US&voice=JennyNeural
      - service: media_player.play_media
        target:
          entity_id: media_player.kitchen_display
        data:
          media_content_id: >-
            media-source://tts/cloud?message=The+Garage+door+is+closed&language=en-US&voice=JennyNeural
          media_content_type: provider
        metadata:
          title: The Garage door is closed
          thumbnail: https://brands.home-assistant.io/_/cloud/logo.png
          media_class: app
          children_media_class: null
          navigateIds:
            - {}
            - media_content_type: app
              media_content_id: media-source://tts
            - media_content_type: provider
              media_content_id: >-
                media-source://tts/cloud?message=The+Garage+door+is+closed&language=en-US&voice=JennyNeural
mode: single

mode: single does not let it run again. So, you are in your loop within your if. When it goes off, that trigger won’t happen.

I have not had a reason to use this, but based on reading, I think mode: restart will do what you need.

1 Like

Like jeffcrum said, it’s because of mode: single

Check the Log and you’re likely to find a warning message that the automation was triggered while it was still busy executing actions from the previous time it was triggered. Single mode ignores a trigger that occurs while the automation is busy.

Consider this scenario:

The automation has played the message at least once and is now counting down the 1-minute delay. During the countdown, the garage door is closed.

That serves to trigger the automation but single mode ignores it because it’s still busy with the countdown. After it completes it, the repeat’s condition is satisfied (i.e. the door is closed) and the automation ends normally. Except it didn’t announce the garage door is closed because that trigger was ignored during the delay’s countdown.

By changing the mode to restart, it will abort the countdown the instant it is triggered again (by the door closing).

Be advised that there is a possibility it can also abort immediately after playing the first message so the second one isn’t played.

1 Like

Is there a reason why you needed to create a separate media_player.play_media call for each of the two media players?

It seems like the two calls are identical so they can be consolidated into one call. In fact, with a tiny bit of templating, all four calls (two for open, two for closed) can be consolidated into one.

1 Like

Thanks Taras, that makes much more sense now

There’s only 2 logs i can see regarding the garage door,

Logger: homeassistant.components.automation.garage_door
Source: helpers/script.py:1783
Integration: Automation (documentation, issues)
First occurred: 20:59:35 (39 occurrences)
Last logged: 21:58:03

garage door : Already running


Logger: pychromecast.controllers
Source: /usr/local/lib/python3.11/site-packages/pychromecast/controllers/media.py:544
First occurred: 19:41:16 (7 occurrences)
Last logged: 19:41:57

Quick Play failed for http://REMOVED/api/tts_proxy/2fd4eaf7d728e2a26697eeb6d56341b0e8514571_en_-_tts.google_en_com.mp3:audio/mpeg({})

Is there a reason why you needed to create a separate media_player.play_media call for each of the two media players?

The main reason is I’m using the visual editor and I can only pick 1 speaker or all speakers at one time, these have been added from google home i assume basically each speaker and a speaker group I have therefore its adding them a weird way in the yaml editor

is this as simple as,

- service: media_player.play_media
              target:
                entity_id: media_player.kitchen_display <<<<adding this line
                entity_id: media_player.max_s_speaker
              data:
                media_content_id: >-
                  media-source://tts/cloud?message=The+Garage+door+is+open&language=en-US&voice=JennyNeural
                media_content_type: provider
              metadata:
                title: The Garage door is open
                thumbnail: https://brands.home-assistant.io/_/cloud/logo.png
                media_class: app
                children_media_class: null
                navigateIds:
                  - {}
                  - media_content_type: app
                    media_content_id: media-source://tts
                  - media_content_type: provider
                    media_content_id: >-
                      media-source://tts/cloud?message=The+Garage+door+is+open&language=en-US&voice=JennyNeural

and then deleting

- service: media_player.play_media
              target:
                entity_id: media_player.kitchen_display
              data:
                media_content_id: >-
                  media-source://tts/cloud?message=The+Garage+door+is+open&language=en-US&voice=JennyNeural
                media_content_type: provider
              metadata:
                title: The Garage door is open
                thumbnail: https://brands.home-assistant.io/_/cloud/logo.png
                media_class: app
                children_media_class: null
                navigateIds:
                  - {}
                  - media_content_type: app
                    media_content_id: media-source://tts
                  - media_content_type: provider
                    media_content_id: >-
                      media-source://tts/cloud?message=The+Garage+door+is+open&language=en-US&voice=JennyNeural

I did have a look at https://www.home-assistant.io/docs/configuration/templating/

but right now templating is out of my skill set so ill clean up these media players and leave as is for now

thanks again!

Mark

That’s the one; far more brief than my explanation. :slightly_smiling_face:


Here’s what I had suggested (untested):

alias: "garage door "
description: ""
trigger:
  - id: 'open'
    platform: state
    entity_id:
      - binary_sensor.garage_door_sensor_door
    from: "off"
    to: "on"
  - id: 'closed'
    platform: state
    entity_id:
      - binary_sensor.garage_door_sensor_door
    from: "on"
    to: "off"
condition: []
action:
  - repeat:
      sequence:
        - service: media_player.play_media
          target:
            entity_id:
              - media_player.max_s_speaker
              - media_player.kitchen_display
          data:
            media_content_id: >-
              media-source://tts/cloud?message=The+Garage+door+is+{{trigger.id}}&language=en-US&voice=JennyNeural
            media_content_type: provider
          metadata:
            title: "The Garage door is {{trigger.id}}"
            thumbnail: https://brands.home-assistant.io/_/cloud/logo.png
            media_class: app
            children_media_class: null
            navigateIds:
              - {}
              - media_content_type: app
                media_content_id: media-source://tts
              - media_content_type: provider
                media_content_id: >-
                  media-source://tts/cloud?message=The+Garage+door+is+{{trigger.id}}&language=en-US&voice=JennyNeural
        - delay:
            minutes: 1
      until:
        - condition: state
          entity_id: binary_sensor.garage_door_sensor_door
          state: "off"
mode: restart

That’s great thanks, so your code works great, its response time seems to be faster, so it will be the one i’m using

It makes sense to me reading it but changing anything in future may prove hard for me as it doesn’t translate properly into the visual editor

from a learning point of view, I did try and clean up my existing code to put both speakers from one media_player.play_media but as soon as i change anything a red line appears down the left hand side, with no text telling me why, and i cannot save it or go look at the visual editor to see any errors

I did remove a line sequence: as i thought it was now not the case considering I had changed it from 2 media_player.play_media to 1

I then thought it was because of a thumbnail for the kitchen display when the other speaker is just a speaker, but your code has that also with 2 speakers

any ideas?

i had to change this

                entity_id:
              - media_player.max_s_speaker
              - media_player.kitchen_display


              

to this , only took a few hours :face_with_raised_eyebrow:

  entity_id:
                  - media_player.max_s_speaker
                  - media_player.kitchen_display
  • What you changed it from had incorrect indentation (and isn’t how it’s displayed in my example).

  • What you changed it to has correct indentation but more than is necessary (refer to my example above).

1 Like