How to trigger automation based off two states simultaneously?

Hi! I am doing my first bigger automation for a ‘goodnight’ routine.

I want to run my speaker to play some rain sounds overnight when a) both phones are in the bedroom, b) both phones are charging, and c) it’s past 10:30pm.

I more or less got this working, but right now it seems the order in which the phones are placed is crucial for the automation to begin. I don’t want the automation to start until both phones are charging, but I also want it to start no matter which phone is placed on to charge first.

Can someone please let me know if I’m just doing it wrong, or if there is a better way of doing it? Or do I just need to double up and add both phones to the trigger and both phones to the wait-for-trigger?

P.S. Bonus points for anyone who knows why the looping of the media also doesn’t seem to work? Is there a way to loop the one hour audio file?

alias: Play rain sounds overnight
description: ''

# One phone placed charging
trigger:
  - platform: state
    entity_id: sensor.iphone_ONE_battery_state
    to: Charging

# Two phones must be in the bedroom (using room-assistant)
condition:
  - condition: and
    conditions:
      - condition: state
        entity_id: sensor.ONE_iphone_room_presence
        state: bedroom-ra
      - condition: state
        entity_id: sensor.TWO_iphone_room_presence
        state: bedroom-ra

# Must be between 10:30pm and before 6am
  - condition: time
    after: '22:30:00'
    before: '06:00:00'

# Don't start automation until second phone begins to charge
action:
  - wait_for_trigger:
      - platform: state
        entity_id: sensor.iphone_TWO_battery_state
        from: Not Charging
        to: Charging
    continue_on_timeout: false

# Lower volume of speaker
  - service: media_player.volume_set
    target:
      device_id: 57d870a7d52cdc5a780091aeb1ed158f
    data:
      volume_level: 0.25

# Say goodnight
  - service: tts.google_cloud_say
    data:
      entity_id: media_player.bedroom_speaker
      message: Good night!
      cache: true

# Small delay to give enough time to wake speaker up and say goodnight
  - delay:
      hours: 0
      minutes: 0
      seconds: 6
      milliseconds: 0

# Begin playing Google's rain sounds locally
  - service: media_player.play_media
    data:
      media_content_id: https://mydomain.com/local/audio/rain.mp3
      media_content_type: music
    target:
      device_id: 57d870a7d52cdc5a780091aeb1ed158f

# Set media to repeating so rain sounds continue to loop the entire night (one hour audio file)
  - service: media_player.repeat_set
    target:
      device_id: 57d870a7d52cdc5a780091aeb1ed158f
    data:
      repeat: all
mode: single

My dumb answer, put all these inputs as both trigger and condition.

  • trigger when phone 1 is being charged or phone 2 is being charged
  • condition is phone 1 is being charged and phone 2 is bei6 charged.

You can create a group of these two sensors, life will get easier. Apply additional inputs into both triggers and conditions.

1 Like

I don’t have an answer about repeating the audio, but you should get rid of the “Wait for Trigger” and just use a template trigger that requires both phones to be charging. When the second phone’s state changes to charging, the template will return “True” and trigger the automation.

alias: Play rain sounds overnight
description: ''
trigger:
  - platform: template
    value_template: >
      {{ is_state('sensor.iphone_ONE_battery_state', 'Charging') and is_state('sensor.iphone_TWO_battery_state', 'Charging') }}
condition:
  - condition: state
    entity_id: sensor.ONE_iphone_room_presence
    state: bedroom-ra
  - condition: state
    entity_id: sensor.TWO_iphone_room_presence
    state: bedroom-ra
  - condition: time
    after: '22:30:00'
    before: '06:00:00'
action:
  - service: media_player.volume_set
    target:
      device_id: 57d870a7d52cdc5a780091aeb1ed158f
    data:
      volume_level: 0.25
  - service: tts.google_cloud_say
    data:
      entity_id: media_player.bedroom_speaker
      message: Good night!
      cache: true
  - delay:
      hours: 0
      minutes: 0
      seconds: 6
      milliseconds: 0
  - service: media_player.play_media
    data:
      media_content_id: https://mydomain.com/local/audio/rain.mp3
      media_content_type: music
    target:
      device_id: 57d870a7d52cdc5a780091aeb1ed158f
  - service: media_player.repeat_set
    target:
      device_id: 57d870a7d52cdc5a780091aeb1ed158f
    data:
      repeat: all
mode: single

Ah I see. Thank you! Hopefully this makes it into the visual editor eventually with multiple sensors/values to choose instead of manually writing that code syntax.

Yeah, this was the brute force method I had for now haha. I know there’s a cleaner way, but it seems not using the visual editor just yet. Thanks for the reply!

Just realized a way to “loop” the audio without digging into scripting or making a bloated MP3 could be to just add a Wait for time to pass (delay) with 1 hour, and then call the same exact file again to play? Obviously would have to copy this 8 times for a full 8 hours, but I guess this will do until some looping functions are introduced for media play.

My two cents: waiting for an hour inside automation is not a good idea :slight_smile: in case a restart of core (update, new custom integration, problems) you will loose automation state, it will not continue

1 Like

I didn’t even think about that, especially since I got watchtower running for updates. I guess bloated MP3 it is for now hah. Thanks for the input!

You don’t need to use a “bloated mp3”… just create another automation that listens for the state of the media player to go from playing to idle and then calls your play_media service again. If restarts are an issue overnight, you could add a second trigger for that…

alias: Repeat rain sounds overnight
description: ''
trigger:
  - platform: homeassistant
    event: start
  - platform: state
    entity_id: media_player.YOUR_PLAYER
    from: 'playing'
    to: 'idle' 
condition:
  - condition: state
    entity_id: media_player.YOUR_PLAYER
    state: 'idle' 
  - condition: time
    after: '22:30:00'
    before: '06:00:00'
action:
  - service: media_player.play_media
    data:
      media_content_id: https://mydomain.com/local/audio/rain.mp3
      media_content_type: music
    target:
      device_id: 57d870a7d52cdc5a780091aeb1ed158f
mode: single
1 Like