Why isn't this variable working?

I have a Sonos speaker, and the song title info in template editor is:
{{ (state_attr('media_player.kjokken', 'media_title')) }}
This works as it should. I am trying to put this into an automation to send it with a REST command to another system (which doesn’t support MQTT or anything else, so it has to be REST). This is the defined rest command:

rest_command:
  girder:
    url: http://192.168.1.100:9876/nodered.html?1={{ kommandoklasse }}&2={{ kommando }}&3={{ sone }}&4=SECRET_PASSWORD_TO_THE_SYSTEM
    method: GET
    verify_ssl: false

And this is the automation I am trying to get right:

alias: Vise hva Sonos spiller
description: ""
triggers:
  - trigger: state
    entity_id:
      - media_player.kjokken
    attribute: media_title
conditions:
  - condition: state
    entity_id: media_player.kjokken
    state: playing
actions:
  - variables:
      avspillingsinfo: |
        {{ (state_attr('media_player.kjokken', 'media_title')) }}
  - alias: Sende det som spiller
    metadata: {}
    data:
      kommandoklasse: Nettradioinfo
      kommando: |
        {avspillingsinfo}
      sone: 2
    action: rest_command.girder
mode: single

But it arrives as kommandoklasse (command class ) “Nettradioinfo” with kommando (command) empty. I tried to use > instead of | but that’s changed when I close and reopen the automation. What am I doing wrong here?

I first tried a simpler version, with only one action, but that didn’t work either:

alias: Sende det som spiller
metadata: {}
data:
  kommandoklasse: Nettradioinfo
  kommando: >
    {{ (state_attr('media_player.kjokken', 'media_title')) }}
  sone: 2
action: rest_command.girder

In your first example, if you want to get the value of the variable avspillingsinfo, you need to write {{ avspillingsinfo }}. Currently you have only a single curly bracket.

You also probably don’t want to use | to define a value that you’re going to put into a URL-encoded string. > is probably a better choice.

The simpler example should work, assuming that you’ve got the entity_id and attribute name correct. What do you see when you paste

{{ (state_attr('media_player.kjokken', 'media_title')) }}

into Developer Tools → Template Editor?

Thanks for answering! Right now it shows:

Lørdagskveld i NRK P1: Try sleeping with a broken heart, Alicia Keys

As I said in the first post, that works. Weird that it doesn’t work in the test, then. :grimacing:

Oh, sorry. Adding second pair of curly braces didn’t change anything in the complicated version.

Did you say that you tried to change the | to a >, but when you reopen the automation after changing it, it’s still |? That’s somewhat troubling.

Try this:

alias: Vise hva Sonos spiller
description: ""
triggers:
  - trigger: state
    entity_id:
      - media_player.kjokken
    attribute: media_title
conditions:
  - condition: state
    entity_id: media_player.kjokken
    state: playing
actions:
  - variables:
      avspillingsinfo: "{{ state_attr('media_player.kjokken', 'media_title') }}"
  - alias: Sende det som spiller
    metadata: {}
    data:
      kommandoklasse: Nettradioinfo
      kommando: "{{avspillingsinfo}}"
      sone: 2
    action: rest_command.girder
mode: single

Hm… It still sends an empty kommando.

What do you see in the alert that happens if you try this automation (added an action at the end):

alias: Vise hva Sonos spiller
description: ""
triggers:
  - trigger: state
    entity_id:
      - media_player.kjokken
    attribute: media_title
conditions:
  - condition: state
    entity_id: media_player.kjokken
    state: playing
actions:
  - variables:
      avspillingsinfo: "{{ state_attr('media_player.kjokken', 'media_title') }}"
  - alias: Sende det som spiller
    metadata: {}
    data:
      kommandoklasse: Nettradioinfo
      kommando: "{{avspillingsinfo}}"
      sone: 2
    action: rest_command.girder
  - action: persistent_notification.create
    data:
      title: Test Alert
      message: "'{{avspillingsinfo}}'"
mode: single

Sorry, sorry, sorry, sorry! It was an indentation error in the recieving end, I thought it was in the sending end! :grimacing: I’m an idiot. I apologize so much!

So now it arrives but it arrives a bit messed up, so:


"Lørdagskveld i NRK P1: Du e så digg, Lyse Netter"

turns into

Lørdagskveld i NRK P1: Du e så digg, Lyse Netter

But I think that is in the receiving end, I can’t format it differently from the sending , can I?

I’m making an educated stab in the dark here, but it’s probably a URL encoding problem.

Try

      kommando: "{{avspillingsinfo|urlencode}}"

How would that look in the simple version? Like this? This still has the encoding error:

alias: Sende det som spiller
metadata: {}
data:
  kommandoklasse: Nettradioinfo
  kommando: >
    {{ (state_attr('media_player.kjokken', 'media_title'|urlencode)) }}
  sone: 2
action: rest_command.girder

Close.

alias: Sende det som spiller
metadata: {}
data:
  kommandoklasse: Nettradioinfo
  kommando: >
    {{ (state_attr('media_player.kjokken', 'media_title'))|urlencode }}
  sone: 2
action: rest_command.girder

Still same problem. I’m guessing it is on the receiving end, so I’m using string.gsub on the LUA system there in a script after receipt. Thank you very much for the help!

Edit: Before the script:
Lørdagskveld i NRK P1: Uten å si et ord, Frida Amundsen
After the script:
Lørdagskveld i NRK P1: Uten å si et ord, Frida Amundsen

In your original example, this was missing the double {{ }} but that was sort of fixed by other things you tried. But it also explained why the variable wasn’t working.

Yep, that was discovered by @d921 during the process. :grin: