Change sound if someone are home (multiple conditions in automation)

Hi!

I need to add multiple conditions to one automation. If “device_tracker.Mua_iphone” and “device_tracker.Mark_iphone” is “away” (everybody are away) I want to play the sound fanfar.mp3. If Mua_iphone are “home” i want to play MUA.mp3. If device_tracker.Mark_iphone are “home” I want to play MARK.mp3.

This is my automation on the front door today:

- id: '1570641644419'
  alias: Automation_Front_Door
  trigger:
  - entity_id: binary_sensor.sensor_front_door
    from: 'off'
    platform: state
    to: 'on'
  condition: []
  action:
  - service: media_player.play_media
    data_template:
      entity_id: media_player.baa
      media_content_id: http://soundking.com/sounds/fanfar.mp3
      media_content_type: audio/mp3
  - service: media_player.volume_set
    data_template:
      entity_id: media_player.baa
      volume_level: 0.5

First, please format your YAML code properly. We can’t read it well as it is.

So, if I understand correctly, when the front door opens, you want to play a particular audio file depending on who is home. Is that correct? If so, what you want to do is to use a template for media_content_id. If you can format your code correctly, I can suggest what you might want.

Sorry. Better now?

Yes, much. Thanks!

Do you think this might do what you want?

- id: '1570641644419'
  alias: Automation_Front_Door
  trigger:
  - entity_id: binary_sensor.sensor_front_door
    from: 'off'
    platform: state
    to: 'on'
  condition: []
  action:
  - service: media_player.play_media
    data_template:
      entity_id: media_player.baa
      media_content_id: >
        {% if is_state('device_tracker.Mua_iphone', 'home') %}
          http://soundking.com/sounds/MUA.mp3
        {% elif is_state('device_tracker.Mark_iphone', 'home') %}
          http://soundking.com/sounds/MARK.mp3
        {% else %}
          http://soundking.com/sounds/fanfar.mp3
        {% endif %}
      media_content_type: audio/mp3
  - service: media_player.volume_set
    data_template:
      entity_id: media_player.baa
      volume_level: 0.5

I suspect that this might not actually be what you want, but since you didn’t answer my question…

1 Like

It’s works perfect! Thank you!

1 Like