Sonos TTS crashes scripting

I have made a script for Sonos speakers for general announcements that snapshots, groups everything together, calls google tts, calculates delay, then restores. It’ll get as far as doing the TTS, but then the script crashes. I can’t tell exactly where, other than it is either in the dynamic delay calculations. Something in the templating…

Script is:

alias: Group Sonos Text To Speech
  - service: media_player.sonos_snapshot
        - media_player.family_room
    - service: media_player.sonos_unjoin
        - media_player.family_room
    - service: media_player.sonos_join
        master: media_player.family_room
    - service: media_player.volume_set
        - media_player.family_room
        volume_level: 0.5
    - service: tts.google_say
        entity_id: media_player.family_room
        message: "{{ what }}"
    - delay:
        seconds: 1
    - delay: >-
        {% set duration = states.media_player[where].attributes.media_duration %}
        {% if duration > 0 %}
          {% set duration = duration - 1 %}
        {% endif %}
        {% set seconds = duration % 60 %}
        {% set minutes = (duration / 60)|int % 60 %}
        {% set hours = (duration / 3600)|int %}
        {{ [hours, minutes, seconds]|join(':') }}
    - service: media_player.sonos_restore
        entity_id: "media_player.family_room"

Dev panel tells me only this:

Error doing job: Task exception was never retrieved

Traceback (most recent call last):
  File "/usr/src/app/homeassistant/helpers/", line 131, in async_render
    return self._compiled.render(kwargs).strip()
  File "/usr/local/lib/python3.6/site-packages/jinja2/", line 76, in render
    return original_render(self, *args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/jinja2/", line 1008, in render
    return self.environment.handle_exception(exc_info, True)
  File "/usr/local/lib/python3.6/site-packages/jinja2/", line 780, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python3.6/site-packages/jinja2/", line 37, in reraise
    raise value.with_traceback(tb)
  File "<template>", line 1, in top-level template code
  File "/usr/local/lib/python3.6/site-packages/jinja2/", line 385, in getattr
    value = getattr(obj, attribute)
jinja2.exceptions.UndefinedError: homeassistant.helpers.template.DomainStates object has no element Undefined

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.6/asyncio/", line 180, in _step
    result = coro.send(None)
  File "/usr/src/app/homeassistant/helpers/", line 104, in async_run
  File "/usr/src/app/homeassistant/helpers/", line 133, in async_render
    raise TemplateError(err)
homeassistant.exceptions.TemplateError: UndefinedError: homeassistant.helpers.template.DomainStates object has no element Undefined

Maybe this is a race condition? Any help would be appreciated. Thanks.

Okay… I found errors in my template. This was my fault. I had an invalid json entry trying to be called.

But I do have an additional question now:
Since that script crashed I can’t seem to run it again without restarting HA entirely. Something about the script ‘is_on’ not being valid. Is there a way to kill the scrip so I don’t have to restart the whole thing?