Looping till state changes

I have toying with is and not quite like ive found here in the docs. Cant seem to accomplish task i want.

following automation works. I would like to have the water tank valve ON automation loop/repeat till state changes to off.

- id: '1556492133047'
  alias: Say Water ON
  trigger:
  - entity_id: switch.water_tank
    from: 'off'
    platform: state
    to: 'on'
  condition: []
  action:
  - service: tts.google_say
    entity_id: media_player.living_room_speaker
    data:
      message: Warning, Warning , Water tank valve is ON! Caution.
- id: '1556493900287'
  alias: Say Valve Now Off
  trigger:
  - entity_id: switch.water_tank
    from: 'on'
    platform: state
    to: 'off'
  condition: []
  action:
  - data: {}
    service: tts.google_say
    entity_id: media_player.living_room_speaker
    data:
      message: Water tank valve is now closed and shut off.
1 Like

Do you mean you want it to announce:

Warning, Warning , Water tank valve is ON! Caution.

repeatedly until switch.water_tank is turned off?

If that’s what you want, I can offer a solution that uses a timer to repeat the message at specified intervals (like every 10 seconds). Turning off the switch would also stop the timer.

yes , exactly what im trying to do.

Click to reveal the original example posted May 2019

Create a timer in your config file and set its duration to be longer than it takes to play the ‘Warning’ announcement. In this example I’ve set it to 15 seconds.

timer:
  looper:
    duration: '00:00:15'

Modify your two existing automations as shown below.

  • Turning on switch.water_tank starts timer.looper.
  • Turning off switch.water_tank stops timer.looper then plays the ‘Valve now closed’ announcement.
- id: '1556492133047'
  alias: Say Water ON
  trigger:
  - entity_id: switch.water_tank
    from: 'off'
    platform: state
    to: 'on'
  condition: []
  action:
  - service: timer.start
    entity_id: timer.looper

- id: '1556493900287'
  alias: Say Valve Now Off
  trigger:
  - entity_id: switch.water_tank
    from: 'on'
    platform: state
    to: 'off'
  condition: []
  action:
  - service: timer.finish
    entity_id: timer.looper
  - service: tts.google_say
    entity_id: media_player.living_room_speaker
    data:
      message: Water tank valve is now closed and shut off.

Now add two more automations that are triggered by the timer’s events.

  • When timer.looper starts it plays the ‘Warning’ message.
  • When timer.looper finishes it checks the state of switch.water_tank.
  • If the switch is on then the timer restarts itself (and plays the Warning message again).
  • If the switch is off the timer does not restart itself.
- alias: 'Looper timer started'
  trigger:
  - platform: event
    event_type: timer.started
    event_data:
      entity_id: timer.looper
  action:
  - service: tts.google_say
    entity_id: media_player.living_room_speaker
    data:
      message: Warning, Warning , Water tank valve is ON! Caution.

- alias: 'Looper timer finished'
  trigger:
  - platform: event
    event_type: timer.finished
    event_data:
      entity_id: timer.looper
  condition:
    condition: state
    entity_id: switch.water_tank
    state: 'on'
  action:
  - service: timer.start
    entity_id: timer.looper

Run Config Check to ensure there are no syntax errors and then restart Home Assistant.


EDIT

2021-03-17

I have noticed that some users are still referring to this old post. Be advised that although the suggested example still works, Home Assistant has been greatly enhanced since May 2019 and it is now possible to use a single automation that employs repeat with while-loop.

- id: '1556492133047'
  alias: Say Water ON
  trigger:
    - entity_id: switch.water_tank
      from: 'off'
      platform: state
      to: 'on'
  condition: []
  action:
    - repeat:
        while: 
          - condition: state
            entity_id: switch.water_tank
            state: 'on'
        sequence:
          - service: tts.google_say
            entity_id: media_player.living_room_speaker
            data:
              message: Warning, Warning , Water tank valve is ON! Caution.
          - delay: '00:00:15'
    - service: tts.google_say
      entity_id: media_player.living_room_speaker
      data:
        message: Water tank valve is now closed and shut off.

An easier way to repeat a message, with more flexibility, is to employ the Alert integration:

3 Likes

worked well until i upgraded to .92.
Now google say is not working. Get one ding. Looking into logs and such this weekend to see what happened. whole system now not acting right after last upgrade.

Have you read the Release Notes for version 0.92?

Version 0.92 contains a Breaking Change for Google Text To Speech:

Google TTS - The google tts platform has changed to google_translate . Default configs will be migrated to the new platform during 0.92 startup. A manual update will be required if the user has changed default tts or is loading the tts configuration from another location. (@awarecan - #23090) (google docs) (google_translate docs) (tts docs) (new-integration)

The platform’s name has changed from google_say to google_translate. You’ll need to update your configuration file and any automations that use google_say.