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' }}
2 Likes

great. But I don’t need to use input select anymore.

I prefer to pass to this script a value in another script.

For example:

script:
  play_trigger_sound:
    sequence:
      - service: script.alarm_script_play_sound_gateway
        data_template:
          ringtone_id:   {{ "1" }}

and so on other scripts with different Ids based on different sounds.

How I have to change the script (my delayed response is because I tried to figure it out my self in these 4 days but I’m losing my mind)

Please support ! :slight_smile:

You can use this script:

  play_trigger_sound:
    sequence:
      - service: script.alarm_script_play_sound_gateway
        data:
          ringtone_id: 1

to call this one:

  alarm_script_play_sound_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'} %}
            {{ 'http://192.168.2.101:8123/local/' ~ d[ringtone_id] ~ '.mp3' if ringtone_id in d.keys() else 'none' }}
1 Like

Using this:

  play_trigger_sound:
    sequence:
      - service: script.alarm_script_play_sound_gateway
        data:
          ringtone_id: 1

  alarm_script_play_sound_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'} %}
            {{ 'http://192.168.2.101:8123/local/' ~ d[ringtone_id] ~ '.mp3' if ringtone_id in d.keys() else 'none' }}


I get some errors :

2020-08-06 17:40:24 ERROR (MainThread) [homeassistant.components.script.alarm_script_play_sound_gateway] alarm_script_play_sound_gateway: Error executing script. Invalid data for call_service at pos 1: extra keys not allowed @ data[‘ringtone_id’]
2020-08-06 17:40:24 ERROR (MainThread) [homeassistant.components.script.play_trigger_sound] play_trigger_sound: Error executing script. Invalid data for call_service at pos 1: extra keys not allowed @ data[‘ringtone_id’]
2020-08-06 17:40:24 WARNING (MainThread) [homeassistant.components.automation] Error Counting Automation: Already running
2020-08-06 17:40:24 ERROR (MainThread) [homeassistant.components.websocket_api.http.connection.140654367622768] extra keys not allowed @ data[‘ringtone_id’]

Sorry, I’m unfamiliar with the media_player’s services. According to the documentation for media_player.play_media, it indicates to use media_content_id and its value depends on the capabilities of the device you are using for media_player.broadcast.

Try this:


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


1 Like

It worked!!!

Thank you very much!

1 Like