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:
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
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.