Template sanity check please

I’m trying to play a specific station based on the input_select, I think the parameters are broken.

Any advice is appreciated!

- alias: Play Radio
  trigger:
    platform: state
    entity_id: input_select.radio_station
  action:
    service: media_player.squeezebox_call_method
    data_template:
      entity_id: media_player.picoreplayer
      command: playlist
      parameters: > 
          {% if is_state("input_select.radio_station", "Jewish Music Stream") %}
            - "play"
            - "https://stream.jewishmusicstream.com:8000"
          {%-elif is_state("input_select.radio_station", "Jewish Hits") %}
            - "play"
            - "http://s3.voscast.com:7246"
          {% else %}
            none
          {% endif %}  

What you are trying to do is not possible. You cannot have jinja span multiple items in a list. Jinja will only return 1 string. It will not return complex objects (like a list of strings, which you are trying to do).

You’ll have to come up with another method to handle this. Possibly scripts with each hardcoded parameter section.

Thank you!

What you have could possibly work with some modifications. However, if that is just a simple example and you want to do something more complex, then :man_shrugging:

- alias: Play Radio
  trigger:
    platform: state
    entity_id: input_select.radio_station
  condition:
    condition: template
    value_template: >
      {{ trigger.to_state.state in ('Jewish Music Stream',
                                    'Jewish Hits') }}
  action:
    service: media_player.squeezebox_call_method
    data_template:
      entity_id: media_player.picoreplayer
      command: playlist
      parameters:
        - play
        - "{{ {'Jewish Music Stream': 'https://stream.jewishmusicstream.com:8000',
               'Jewish Hits': 'http://s3.voscast.com:7246'}
              [trigger.to_state.state] }}"

If you do actually need to send “none” for the value of parameters when the input_select is not one of the two, then you could do that in a second automation (with appropriate condition.)

That worked perfectly! Thanks!!

1 Like