Assigning a templete value to a variable

Hello,

I am trying to assign a template value to a variable to be used within a script but it returns a null value. I assume I have the syntax wrong.

This is how I am trying to assign a value:

variables:
  var_wled_effect: "{{ state_attr("light.wled_living_room", "effect_list") | random }}"

When I run this section nothing seems assigned to the variable (as in nothing is said after “Variable value is”):

service: tts.google_say
data:
  entity_id: media_player.google_home_speaker
  message: Variable value is "{{ var_wled_effect }}"

This works, so I know the template is working:

service: tts.google_say
data:
  entity_id: media_player.google_home_speaker
  message: >-
    Effect selected "{{ state_attr("light.wled_living_room", "effect_list") | random }}"

Can anyone offer me any insight into how to assign a variable value from a template? Thanks in advance.

Your use of quotes appears to be incorrect.

  • If you use double-quotes inside a template, then you must use single quotes outside the template.

  • Alternatively, if you use single quotes inside a template then you must use double-quotes outside the template.

You’re using double-quotes inside and outside of the template.

Try it this way:

- variables:
    var_wled_effect: "{{ state_attr('light.wled_living_room', 'effect_list') | random }}"
- service: tts.google_say
  data:
    entity_id: media_player.google_home_speaker
    message: "Variable value is {{ var_wled_effect }}"
2 Likes

To illustrate why Taras’s advice is important, look how your template currently sees the open and closing of your quotes:

var_wled_effect: "{{ state_attr("

That’s all it sees.

2 Likes

That was indeed the problem. I thought if it worked in the last call (the last tts.google_say) it would work for the variable set as well.

Thanks for the feedback guys. Most awesome. Cheers.

1 Like

That makes sense, thanks.