Templated Automation not working... but no error

I have put together my first automation - an event which gets triggered through the API and tunes my media player to a preset radio station. It works fine when there is no templating involved. However, when I introduce a conditional template, it just fails. Nothing in the log.

The code below looks for a value of “radio national” in the event data variable (“station”) and sends the relevant URL to my player if found. Otherwise it sends an alternative URL. According to the conditional logic, at least something should play… but zilch. Once again, if I remove the templated conditional code and hard code in a single URL, it works fine.

Anyone see what might be up with the code below?

alias: "Tune to ... on Squeezebox"
trigger:
  platform: event
  event_type: squeeze_tuneto
action:
  - service: media_player.volume_set
    data:
      entity_id: media_player.lil_black_box
      volume_level: 0
  - service: logbook.log
    data_template:
      name: "Station"
      message: "{{ trigger.event.data['station'] }}"
  - service: media_player.play_media
    data_template:
      entity_id: media_player.lil_black_box
      media_content_type: "audio/mp3"
      media_content_id: >-
        {%- if trigger.event.data['station'] == "radio national" %}
          "http://opml.radiotime.com/Tune.ashx?id=s20269&formats=ogg,mp3,wma,wmvoice&partnerId=16&serial=a2918f74f5ea42bd33dec6436e682687"
        {%- else %}
          "http://opml.radiotime.com/Tune.ashx?id=s7018&formats=aac,ogg,mp3,wma,wmvoice&partnerId=16&serial=a2918f74f5ea42bd33dec6436e682687"
        {%- endif %}

Anyone got any ideas? I spent a good day or so trying to get the syntax for the data_template correct but it just does not seem to work. No errors in the log, nothing!

try {% YOUR COND %} and not {%- YOUR COND %}

media_content_id: >-
  {% if trigger.event.data['station'] == "radio national" %}
  "http://opml.radiotime.com/Tune.ashx?id=s20269&formats=ogg,mp3,wma,wmvoice&partnerId=16&serial=a2918f74f5ea42bd33dec6436e682687"
  {% else %}
  "http://opml.radiotime.com/Tune.ashx?id=s7018&formats=aac,ogg,mp3,wma,wmvoice&partnerId=16&serial=a2918f74f5ea42bd33dec6436e682687"
  {% endif %}

you can use the dev-template tool to check if the template gives you something (just change the trigger.event part of the template by sensor something of instance)

Your syntax is wrong: check here for a similair example: https://community.home-assistant.io/t/chromecast-radio-with-station-and-player-selection/12732

Thanks lambtho - this didn’t seem to make any difference unfortunately. I just don’t think it’s possible to use this variable in a conditional statement in this way - I have tried every permutation.

Wow - some great examples using a different approach. Thanks so much. I will give this a go!

2 Likes

Thank you so much Bob_NL! I have just set everything up so I can now tell Google Assistant on my phone to tune in the kitchen radio to my preferred station. This is brilliant - really appreciate your help.

1 Like

Glad you sorted it out!