I am working on a sound machine automation. This will play white noise, rain sounds, ocean, etc. It is tied in with Assist. I have an automation that when called will play a sound file and when the sound file ends it will loop again and replay it until a condtion is met.
Unfortunately it seems that it will play the entire sound file before moving to the condition check. This is not good as the user cannot cancel the playback until the whole sound file completes which could be 10-20 minutes depending on the sound file being played.
Here's the code I have now:
- repeat:
sequence:
- data:
media_content_id: "{{ soundmachine_dict[trigger.slots.soundtype] }}"
media_content_type: MUSIC
target:
entity_id: "{{ target_mediaplayer_device }}"
action: media_extractor.play_media
- delay:
seconds: 2
- wait_template: "{{ states(target_mediaplayer_device) == 'idle'}}"
continue_on_timeout: true
enabled: true
while:
- condition: template
value_template: >-
{{ (state_attr(target_satellite_device,'soundmachine')
== 'playing') }}
Is there a better way to start the sound playing and stop the sound on demand?
Here is the entire automation:
https://github.com/dinki/View-Assist/blob/viewassist-soundmachine/View_Assist_custom_sentences/Sound_Machine/automation-soundmachine.yaml
Thanks!