Send variable/attribute to script through IFTTT/JSON

Hello fellow “domoters”! :wink:
I’m trying to send a variable to a Home Assistant script through IFTTT: I read the topics linked at the bottom of this post, but I’m still not able to do it.
What I’m trying to do is to receive the number of the channel from Google Home, and make Home Assistant cast the channel URL to a Chromecast.
This is my code and my IFTTT configuration:

  change_kitchen_channel:
    alias: "Change kitchen channel"
    sequence:
     - service: media_player.play_media
       data_template:
         entity_id: media_player.kitchen
         media_content_id: >
          {% if is_state("{{channel}}", "1") %} http://iptv/link/1
          {% elif is_state("{{channel}}", "2") %} http://iptv/link/2
          {% endif %}
         media_content_type: video

I also tried with {{data.channel}} to indicate that’s a variable. The URLs are dummy ones, but the real ones arefully supported by HA and Chromecast (tried casting them with other scripts).

The URL is correct, because I’m using it to call other scripts. Has anybody got any suggestion?

https://www.reddit.com/r/homeassistant/comments/6f1ula/ifttt_hass_python_script_with_a_variable/

Have you checked your Jinja2 template code in the developer tools to see if it outputs the correct media_content_id?

Thanks to your help, I managed to “debug” the code! :smiley:

First of all, the URL in IFTTT was incorrect: the correct one was http://url:port/api/services/script/change_kitchen_channel?api_password=pass, and not http://url:port/api/services/script/turn_on?api_password=pass (even if, with scripts that don’t need variables, this URL works).

Now the code:

    change_kitchen_channel:
      alias: "Change kitchen channel"
      sequence:
        - service: media_player.play_media
          data_template:
            entity_id: media_player.kitchen
            media_content_id: >
              {% if (channel == "1") %}
                http://iptv/link/1
              {% elif (channel == "2") %}
                http://iptv/link/1
              {% endif %}
            media_content_type: video
1 Like