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”
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??
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 %}
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: