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:

Sonos_group_tts:
alias: Group Sonos Text To Speech
sequence:
  - service: media_player.sonos_snapshot
      entity_id:
        - media_player.family_room
        - media_player.kitchen
    - service: media_player.sonos_unjoin
      entity_id:
        - media_player.family_room
        - media_player.kitchen
    - service: media_player.sonos_join
      data:
        master: media_player.family_room
      entity_id:
        - media_player.kitchen
    - service: media_player.volume_set
      entity_id:
        - media_player.family_room
        - media_player.kitchen  
      data:
        volume_level: 0.5
    - service: tts.google_say
      data_template:
        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
      data_template:
        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/template.py", line 131, in async_render
    return self._compiled.render(kwargs).strip()
  File "/usr/local/lib/python3.6/site-packages/jinja2/asyncsupport.py", line 76, in render
    return original_render(self, *args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/jinja2/environment.py", line 1008, in render
    return self.environment.handle_exception(exc_info, True)
  File "/usr/local/lib/python3.6/site-packages/jinja2/environment.py", line 780, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python3.6/site-packages/jinja2/_compat.py", 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/sandbox.py", 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/tasks.py", line 180, in _step
    result = coro.send(None)
  File "/usr/src/app/homeassistant/helpers/script.py", line 104, in async_run
    delay.async_render(variables))
  File "/usr/src/app/homeassistant/helpers/template.py", 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?