Are script variables now required to be set?

Hello,

In v 2021.5.1 I’m no longer able to use variables that aren’t defined in if statements.

For example, I have a script that accepts a playlist variable from automations:

{% if playlist -%}
  {% set selectedPlaylist = playlist | random -%}
{% elif genre == 'pop' -%}
  ...
{% else -%}
  ...
{% endif -%}
{{ selectedPlaylist }}

This works fine in v 2021.3.4. In the current version I get an error that playlist isn’t defined (which in many cases it isn’t).

Does anyone know of changes that might have occurred between March and May (nothing is standing out to me in the release notes)?

Do you actually mean error or do you mean warning? I don’t know how you would suddenly start getting an error but as of 2021.4.0 you should start getting a warning since you are using using something undefined in a template. There’s a whole section on that in last releases release notes: warnings for undefined variables in templates.

Assuming that is what you are seeing then the warning doesn’t break anything its just encouraging you to change it before it does. Have you tried changing that if statement to this:

{% if playlist is defined -%}
1 Like

Thanks, you are right it was a warning, not an error. I had mixed up the warning/error related to Sonos. I was writing this from memory because I had already restored/downgraded to resolve the issue.

This is the error I’m getting:

Traceback (most recent call last):
  File "/usr/local/lib/python3.8/concurrent/futures/thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/home/ha/homeassistant/lib/python3.8/site-packages/homeassistant/components/sonos/media_player.py", line 501, in update_media
    self._play_mode = event.current_play_mode if event else self.soco.play_mode
  File "/home/ha/homeassistant/lib/python3.8/site-packages/pysonos/events_base.py", line 183, in __getattr__
    raise AttributeError("No such attribute: %s" % name)
AttributeError: No such attribute: current_play_mode

Not sure what is causing that error, I’ll have to do some trial and error to troubleshoot.