Need help with template - Sonos Favourite selection

Hello,

Need some help here, as I cannot seem to get the variable out to Sonos Media Player script.

I set up a list with favourites, and based on day of week, I get different music favourites to play to Sonos.

Whilst in template UI, the variable comes out correctly, but when I put it into scripts for automation to stat playing at certain times I get an error “Missing favorite for media_id: FV:2/29:”, yet template display this correctly, but wont work in script.

I provide the script that I have created:-

When I hardcode media content_id to ‘FV:2/29:’ it plays fine.

I have also change the line ‘data:’ to ‘data_template:’ but it makes no differences.

Any ideas as to what I am doing wrong?

The following is what is entered in configuration yaml, and this is correct, as it does display the correct format.

Please note, that I am learning to use template, just so anyone ask why I’m doing it this way, if there is a better way, at least I am trying to learn.

Thanks

Did you try

media_content_id: "{{ states('sensor.arraypointer') }}"

?

Remove the spurious colon at the end of each item in the musiclist variable.

  - platform: template
    sensors:
      arraypointer:
        friendly_name: 'musicarray'
        value_template: >
          {% set musiclist = ["FV:2/29", "FV:2/37", "FV:2/36", "FV:2/31", "FV:2/26", "FV:2/30"] %}
          {% set dayofweek = musiclist[now().weekday()] %}
          {{ dayofweek }}

If you wish, you can reduce the template to this:

        value_template: >
          {{ ["FV:2/29", "FV:2/37", "FV:2/36", "FV:2/31", "FV:2/26", "FV:2/30"][now().weekday()] }}

If you copy-paste the following template into the Template Editor, it will list the keys of the dictionary contained by the sensor’s items attribute (I am assuming you have a sensor.sonos_favorites entity). You’ll notice that each key’s name doesn’t end with a colon.

{{ state_attr('sensor.sonos_favorites', 'items').keys() | list }}

EDIT

For future reference, avoid posting screenshots of YAML; post the YAML as text. It spares the people replying from having to re-type the contents of your screenshot.

For more information, refer to FAQ Guideline 14 - Screenshots

Thanks to Pedolsky and Taras - I did try Pedolsky, but it still gave me the same error as before, so I tried Tara’s solution and hey presto, just removing “:” worked, not sure why, as when I put this in hard code as well it still worked - is there a reason for this Tara?

Now I can resume with this, and will try out shorter version that Tara’s suggested. Noted the screenshot, I was having trouble submitting my issues when I did copy the code, even using the preformatted text, hence the screen shot, but found that I should have just put a tick in the do not show again :frowning: next time I will just do that again.

Many Thanks for the solution.

1 Like