Help changing a script

Hi,

I’m using a script that calls xiaomi aqara and I want to switch to another media player.

this is the original one:

script:
  alarm_script_play_soud_gateway:
    sequence:
      - service: xiaomi_aqara.play_ringtone
        data_template: 
          gw_mac: !secret xiaomi_gateway1_mac
          ringtone_id: "{{ ringtone_id }}"
          ringtone_vol: "{{ ringtone_vol }}"

that is called in many other scripts like this one

  alarm_gateway1_play_pending_sound_day:
    sequence:
      - condition: numeric_state
        entity_id: input_number.gateway1_pending_volume_day
        above: 0
      - service: script.alarm_script_play_soud_gateway
        data_template:
          ringtone_id: "{{ states('input_select.gateway1_pending_sound_day').split('-')[0] | int }}"
          ringtone_vol: "{{ states('input_number.gateway1_pending_volume_day') | int }}"

where those input_select are defined in an array:

sound_gateway1: [
"1 - Allarme Armato",
"2 - Allarme Disinserito",
"3 - Allarme in Attivazione",
"4 - Allarme Innescato",
"5 - Allarme Ritardato"
]

This is what I made so far but is full of error and obviously doesn’t work:

script:
  alarm_script_play_soud_gateway:
    sequence:
      - service: media_player.play_media
        entity_id: media_player.broadcast
        data_template:
          ringtone_id: >
              {% if is_state("1 - Allarme Armato") %}
                http://192.168.2.101:8123/local/allarmearmato.mp3”
              {%-elif is_state("2 - Allarme Disinserito") %}
                http://192.168.2.101:8123/local/allarmedisinserito.mp3”
              {%-elif is_state("3 - Allarme in Attivazione") %}
                http://192.168.2.101:8123/local/allarmeinattivazione.mp3”
              {%-elif is_state("4 - Allarme Innescato") %}
                http://192.168.2.101:8123/local/allarmeinnescato.mp3”
              {%-elif is_state("5 - Allarme Ritardato") %}
                http://192.168.2.101:8123/local/allarmeritardato.mp3”
              {% else %}
                none
              {% endif %}
          media_content_type: 'music'

Can someone please help me?

Thank you in advance!

You’re close. You need an entity_id:

script:
  alarm_script_play_soud_gateway:
    sequence:
      - service: media_player.play_media
        entity_id: media_player.broadcast
        data_template:
          ringtone_id: >
              {% if is_state("input_select.change_this_id", "1 - Allarme Armato") %}
                http://192.168.2.101:8123/local/allarmearmato.mp3”
              {%-elif is_state("input_select.change_this_id", "2 - Allarme Disinserito") %}
                http://192.168.2.101:8123/local/allarmedisinserito.mp3”
              {%-elif is_state("input_select.change_this_id", "3 - Allarme in Attivazione") %}
                http://192.168.2.101:8123/local/allarmeinattivazione.mp3”
              {%-elif is_state("input_select.change_this_id", "4 - Allarme Innescato") %}
                http://192.168.2.101:8123/local/allarmeinnescato.mp3”
              {%-elif is_state("input_select.change_this_id", "5 - Allarme Ritardato") %}
                http://192.168.2.101:8123/local/allarmeritardato.mp3”
              {% else %}
                none
              {% endif %}
          media_content_type: 'music'

OK but this config is quite complex and there are different input_select ( six).

How can I adapt the code?

with a sequence of others elif?

You only need one input select, with multiple choices. And there are only 5 options.

If all you want is to reduce the template’s size, you can do it like this:

script:
  alarm_script_play_soud_gateway:
    sequence:
      - service: media_player.play_media
        data_template:
          entity_id: media_player.broadcast
          media_content_type: 'music'
          ringtone_id: >
            {% set d = {'1':'allarmearmato', '2':'allarmedisinserito', '3':'allarmeinattivazione', '4':'allarmeinnescato', '5':'allarmeritardato'} %}
            {% set s = states('input_select.change_this_id')[:1] %}
            {{ 'http://192.168.2.101:8123/local/' ~ d[s] ~ '.mp3' if s in d.keys() else 'none' }}
1 Like