Script turn itself off based on a variable?

I’m trying to get a script to turn itself off based on an input select state and a variable.
What am I doing wrong? I tested the variable in the script by a notify, and its “no”

What am I doing wrong?

Script snippet:

announce:
  alias: "Announce on Sonos"
  sequence:
    - service: script.turn_off
      data_template:
        entity_id: >
            {% if is_state('input_select.house_mode', 'Night') and "{{ at_night }}" == 'no' %}
              script.announce
            {% else  %}
              script.donothing
            {% endif %}

Are you expecting this to disable the script?

All script.turn_off does is stop a running script. There is no way to disable scripts like you can automations.

I’m trying to stop the running script with script.turn_off, not disable it from being run.

Ok that should work, though there appears to be an extra double quote here:

"{{ at_night }}""
                ^

Thanks tom for checking out my yaml - I’m afraid the rouge quote is not it - that’s a mistake left over from my latest edit (of very many!).

I’ve edited the code above to correct - the script still is not stopped from running even though “{{ at_night }}” == no and input_select.house_mode is Night

Further tests reveal if I remove the and "{{ at_night }}" == 'no' part of the condition the script works ok.
So it’s fair to say that this condition ie never met?
This script works, so what’s wrong with the original formatting??

announce:
  alias: "Announce on Sonos"
  sequence:
    - service: notify.mobile_app_iphone
      data_template:
        message: "{{ at_night }}"
    - service: script.turn_off
      data_template:
        entity_id: >
            {% if is_state('input_select.house_mode', 'Night') %}
              script.announce
            {% else  %}
              script.donothing
            {% endif %}


I am unsure what the “{{ at_night }}” is? Maybe you are missing {{ sensor.at_night }} == ‘no’

Also you can’t put a template in a template. Instead add.

entity_id: >
            {% set night = states('sensor.at_night') %}
            {% if is_state('input_select.house_mode', 'Night') and night == 'no' %}
              script.announce
            {% else  %}
              script.donothing
            {% endif %}

It’s a variable that has been passed to the script, it is not a sensor.

That’s the problem with just supplying snippets of the code.

We are left just guessing at what the rest is and what the intended result is.

Hmm, I think it’s pretty clear, I have shown 2 versions of the entire start of the script.

Can we please stick to trying to resolve the issue with the script variable please.

It’s documented here if you’re not familiar with it.

The problem is the double-quote and curly brace characters in:

"{{ at_night }}" == 'no'

That’s comparing the string {{ at_night }} to no, which of course are not the same. You need to remove those characters so that it can evaluate the expression at_night which will result in a string that it will then compare to the string no. Like this:

announce:
  alias: "Announce on Sonos"
  sequence:
    - service: script.turn_off
      data_template:
        entity_id: >
            {% if is_state('input_select.house_mode', 'Night') and at_night == 'no' %}
              script.announce
            {% else  %}
              script.donothing
            {% endif %}

Also, a better way to do this is with a choose action:

announce:
  alias: "Announce on Sonos"
  sequence:
    - choose:
        - conditions:
            - condition: template
              value_template: >
                {{ is_state('input_select.house_mode', 'Night') and at_night == 'no' }}
          sequence:
            - service: script.turn_off
              entity_id: script.announce