Wait for inside main trigger or condition?

I have a few automations where I want to check the state of a device, and depending on the state it should continue or wait for the state to change. is there an easy way to add a “wait for” inside a trigger and/or condition?

To give an example, I have a passive listener when a calendar post starts, it announces that post on a group of speakers with a condition that I’m actually home at the moment. Now, sometimes I’m home but I´m wearing headphones, in that case I would like to wait until any media is stopped before announcing the calendar post.

Same thing when I get home, open my front door, I’m greeted with information like temperature and any upcoming calendar events for the day etc, but if my headphones are still on (walking home from work) I can’t hear a thing.

Since my headphones, even if they are bluetooth, is not a device in HA I can’t pause the media and announce the post and then continue the media; that would have been my preferred method of course.

So my next best thought was to check if any media is currently playing on either my computer or phone, it should sort of pause/queue the announcement until it stopped.

Any idea how to accomplish this in a simple manner?

I hope i understanded you right, but maybe a wait_template would be a solution:

https://www.home-assistant.io/docs/scripts/#wait-for-a-template

  action:
    - wait_template: "{{ states('media_player.your_computer') != 'playing' and states('media_player.your_phone') != 'playing'}}"

Adding this at first in the action part will delay the rest of the action until both media_players are not playing anymore.
You can also set a timeout that skips the rest of the action (i.e. if not needed anymore if a certain time has passed).

2 Likes

That is exactly what I was looking for, didn’t even realize there was such a thing as a wait template, should have looked more closely, really appreciate it Mike!

Glad i could help. Home Assistant is such a complex thing that it is really not easy to overview all of the possibilities there are. But for that you have the community :slightly_smiling_face: