Hi there,
I’ve wrote this simple if else condition code that after it check the variable coming in, it has to tell me a series of sentences based on my actual lights state.
I’ve already verified and all code seems to be running correctly in Dev tools but the current issue is that no matter what it variable state it receive (on or off) it always tell me that:
“lights are already on” or “lights are already off”
this because it seems that the Speech voice part, being processed after the action has already turned on or off the lights… So there is no way for me to get the “turning on the lights” or “turning off the lights”.
So is there a way to prioritize and force the code to process firstly the SPEECH part before the ACTION part change the state?
# values coming in variables: {{name}} = light group {{state}} = on/off
ChangeLightState:
speech:
text: >-
{% set name2 = name | replace(" ","_") %}
{% if state == 'on' and is_state(('light.' ~ name2), 'off') %} turning on lights
{% elif state == 'on' and is_state(('light.' ~ name2), 'on') %} lights are already on
{% elif state == 'off' and is_state(('light.' ~ name2), 'on') %} turning off lights
{% elif state == 'off' and is_state(('light.' ~ name2), 'off') %} lights are already off
{% else %} info debug error {{ state }}
{% endif %}
action:
- service: light.turn_{{ state }}
target:
entity_id: light.{{ name | replace(" ","_") }}
- service: notify.marco # this just for debug purpose
data:
message: >-
Debug info -- {{ name }} - {{ state }}