Automation actions: While loop not behaving as expected?

Example code:

alias: Sound alarm
description: ''
trigger:
  - platform: state
    entity_id: E1
    to: notifying
  - platform: state
    entity_id: E2
    to: notifying
condition: []
action:
  - repeat:
      while:
        - condition: state
          entity_id: E1
          state: notifying
        - condition: or
          conditions:
            - condition: state
              entity_id: E2
              state: notifying
      sequence:
        - service: media_player.play_media
          data:
            media_content_id: >-
              https://xxx.com/yyy/alarm.wav
            media_content_type: music
          target:
            entity_id: media_player.xxxxxx-yyyyyyy
        - delay:
            hours: 0
            minutes: 0
            seconds: 10
            milliseconds: 0

Some initial data:

  • E1 and E2 can never be at “notifying” state at the same time
  • sequence is playing about 10 seconds long media file, when triggered. The structure is OK and works, let’s not concentrate on it

What I was aiming for, and what I thought this structure would do, is the following:

  • Triggered by either E1 or E2 in “notifying” state.
  • if either E1 or E2 is “notifying”, while loop plays 10 sec media file and 10 sec delay in the end gives it time to finish.
  • If E1 or E2 is still notifying, it runs the sequency again, that is, plays the audio file during 10 sec delay and so on.

Instead of that, what the structure does is:

  • If E1 is notifying, behavior is like described before. File is played in a continuous loop as long E1 is “notifying”
  • If E2 is notifying, the file is played only once.

Why is this?

Your OR conditions for the while loop aren’t quite correct. try this:

  - repeat:
      while:
        - condition: or
          conditions:
            - condition: state
              entity_id: E2
              state: notifying
            - condition: state
              entity_id: E1
              state: notifying

See: https://www.home-assistant.io/docs/scripts/conditions/#or-condition

You don’t put the OR condition between the two conditions as you would write it in a sentence. You create an OR “block” and put the conditions you want OR’ed together inside that block.

Yes, in the code view it looks better, however I made the automation with the automations graphical UI, where it shows up so, that under “while” there is first “or” and after that the conditions, which is a bit illogical compared to what I’ve ever done before, if You know what I mean… :slight_smile:

However, it works alright, thanks!