There is an option to turn off the python_script in a middle (let’s say that the for loop is already running),
From the automation that called to this python_script ?
You could just write wait_template in python: Have a loop in python and check the state(s) of whatever entities would be in the wait template every second (or some interval) until your condition is true, then proceed.
This exactly what I did, but I can’t change the “binary_sensor.tami_4_water_sensor” to off:
if hass.states.get('binary_sensor.tami_4_water_sensor').state == 'on':
message = 'Pay attention, there is flooding behind Tammy Bar !'
else:
message = 'Pay attention, there is flooding in the storage !'
for j in range(4):
if hass.states.get('binary_sensor.tami_4_water_sensor').state == 'on':
# Alexa tts message:
hass.services.call('notify', 'alexa_media_alex_s_echo_dot', {'message':message, 'data' : {'type':'tts'}}, False)
time.sleep(1)
Can you provide an example? I read this and believed it was not possible.
EDIT
I misread your suggestion. I thought you were suggesting to use a literalwait_template. You are suggesting to emulate its functionality (i.e. use python code to check state of a given entity).
As for you question, the “binary_sensor.tami_4_water_sensor” is a xiaomi flooding sensor (zigbee).
I tried your suggestion to create and using an input_boolean to trigger the script to exit regardless of the state of the sensor, like shown in the the following code:
But it’s working, only if the first for loop iteration didn’t start yet, ones it’s started no option to stop the script.
for j in range(4):
if hass.states.get('input_boolean.python_script_water_sensor_sirens_code').state == 'on':
# Alexa tts message:
hass.services.call('notify', 'alexa_media_alex_s_echo_dot', {'message':message, 'data' : {'type':'tts'}}, False)
time.sleep(1)
- alias: 'The house was flooded with water notification'
trigger:
platform: state
entity_id:
- binary_sensor.tami_4_water_sensor
- binary_sensor.storage_water_sensor
from: 'off'
to: 'on'
action:
- service: python_script.water_sensor_sirens
The Python script: “water_sensor_sirens.py”
if hass.states.get('binary_sensor.tami_4_water_sensor').state == 'on':
message = 'Pay attention, there is flooding behind Tammy Bar !'
else:
message = 'Pay attention, there is flooding in the storage !'
for j in range(4):
if hass.states.get('input_boolean.python_script_water_sensor_sirens_code').state == 'on':
# Alexa tts message:
hass.services.call('notify', 'alexa_media_alex_s_echo_dot', {'message':message, 'data' : {'type':'tts'}}, False)
time.sleep(1)
# Repeat for sirens/alarm:
for i in range(5):
hass.services.call('media_player', 'play_media', {'entity_id':'media_player.alex_s_echo_dot', 'media_content_type':'sound', 'media_content_id':'amzn_sfx_scifi_alarm_01'}, False)
time.sleep(1)
Yes, when I turn it off during loop #1, it still continues with loop 2 to 4.
It’s o.k. with me to stop only after the current iteration will finish.
But it’s o.k. to stop immediately as well.
I’m wondering if the state machine passed to python scripts is stuck in stone. If that’s the case checking the state may not make a difference. I’d have to write a test case to verify, I don’t use them enough to know for sure.
I just added a break statement in each loop to bail out of the loops if the input_boolean is not on.
if hass.states.get('binary_sensor.tami_4_water_sensor').state == 'on':
message = 'Pay attention, there is flooding behind Tammy Bar !'
else:
message = 'Pay attention, there is flooding in the storage !'
for j in range(4):
if hass.states.get('input_boolean.python_script_water_sensor_sirens_code').state != 'on':
break
# Alexa tts message:
hass.services.call('notify', 'alexa_media_alex_s_echo_dot', {'message':message, 'data' : {'type':'tts'}}, False)
time.sleep(1)
# Repeat for sirens/alarm:
for i in range(5):
if hass.states.get('input_boolean.python_script_water_sensor_sirens_code').state != 'on':
break
hass.services.call('media_player', 'play_media', {'entity_id':'media_player.alex_s_echo_dot', 'media_content_type':'sound', 'media_content_id':'amzn_sfx_scifi_alarm_01'}, False)
time.sleep(1)
This thought about the state machine is interesting. I would have expected that each time it was called, the state would be the most up-to-date version of that entity’s state. And I will continue to belive this until proven otherwise