Trouble with if then else and multi line variables

Having trouble with the following request. If then else works for command: but does not work for the parameters: value which is a list of values []. Any ideas how to make this work.

- service:  media_player.squeezebox_call_method
  data_template: 
    entity_id: "media_player.{{states.variable.var_lms_player.state}}"
    command: >
        {% if (states.sensor.alb_id.state == "0") %} 
            playlist
        {% else %}
            playlistcontrol
        {% endif %}
    parameters: >
        {% if (states.sensor.alb_id.state == "0") %} 
            ["loadtracks","album.titlesearch={{states.variable.var_lms_album.state}}"]           
        {% else %}
            ["loadtracks","album.titlesearch={{states.variable.var_lms_album.state}}"]
        {% endif %}

The squeezebox call works fine with parameters: [“loadtracks”,“album.titlesearch={{states.variable.var_lms_album.state}}”] all in one line but not using the if then else construct.

Any advice much appreciated. Ynot.

Why do you even have an if-then-else for the parameters if you’re using the same value in both cases? Why not just:

    parameters: ["loadtracks","album.titlesearch={{states.variable.var_lms_album.state}}"]

They will not be the same I just didn’t type it in but for what its worth, the if statement will return:

parameters: [“cmd:load”,“album_id:{{states.sensor.alb_id.state}}”]
or
parameters: [“loadtracks”,“album.titlesearch={{states.variable.var_lms_album.state}}”]

both of these statements work properly as written but using an if then else, results in failure .

Sorry for not having writtent it out completely. Ynot

Solved,

Created 2 variable P1 and P2 and did the call the way I normally would have:

- service: variable.set_variable
  data_template:
    variable: var_lms_p1
    value: >
        {% if (states.sensor.alb_id.state == "0") %} loadtracks           
        {% else %}  cmd:load
        {% endif %}
- service: variable.set_variable
  data_template:
    variable: var_lms_p2
    value: >
        {% if (states.sensor.alb_id.state == "0") %}  album.titlesearch={{states.variable.var_lms_album.state}}           
        {% else %}  album_id:{{states.sensor.alb_id.state}}
        {% endif %}
- service:  media_player.squeezebox_call_method
  data_template: 
    entity_id: "media_player.{{states.variable.var_lms_player.state}}"
    command: >
        {% if (states.sensor.alb_id.state == "0") %}  playlist
        {% else %}  playlistcontrol
        {% endif %}
    parameters: ["{{states.variable.var_lms_p1.state}}","{{states.variable.var_lms_p2.state}}"]

Wordy but it works. Sorry for calling out the dogs a little too early. Ynot.

No problem. For future reference, I think the issue with the way you originally did it was the result was a string, not a list. If you do:

  data_template:
    anything: >
       ["blah", "blah", "blah={{states.some.thing.state}}"]

you don’t get:

["blah", "blah", "blah=blah"]

you (effectively) get:

'["blah", "blah", "blah=blah"]'

If you want to use templating to create a list, you can’t use >, you have to do something like this:

  data_template:
    anything: ["blah", "blah", "blah={% if xxx %}{{ states.some.thing.state }}{% else %}{{ states.some.other_thing.state }}{% endif %}"]

I.e., the quoted template has to be inside the (non-quoted) list brackets. At least, that’s what I’ve seen discussed.

Does that make sense?

I think I get it. That was definitely the problem, the result turned out to be a string when a list was expected. The syntax learning curve for this app is pretty steep. Thanks for the pointers. Ynot,

1 Like