How to stop execution of a python script on state change?

Hi All,

Sorry for cross-posting, but I hope here I can get answer to this question I asked on another sub-forum.

In essence, I need to stop a python script I ran earlier from HA (via service: python_script.script_name) if one of entities changes state.
I tried calling service: python_script.turn_off and service: homeassistant.turn_off, but it does not work.

I’m not sure that’s possible, and it woudn’t be very reliable (not sure where the script would stop).

What is you script doing?
Maybe change it so that home-assistant run it every x minutes/seconds via an automation, and when you don’t need the script to run, disable the automation on state change.

Here is the code that runs in a while loop:
hass.services.call(‘homeassistant’, ‘turn_on’, {‘entity_id’: entity_id})
time.sleep(on_time)
hass.services.call(‘homeassistant’, ‘turn_off’, {‘entity_id’: entity_id})

If I knew how to check state of an entity from the python script, I would be able to end it from within…

You could do that with an automation entirely in hass. Any reason you went with a python script?

Well, I don’t know how to do the following in hass (did not see any loop statements):
If there is time T and and an entity that cannot be ON for longer than L and requires D seconds to rest.
Can you show me how to do that?

Perfect use case for AppDaemon!

ok, will give it a go.
Have no idea about it apart from name though :slight_smile:

If your comfortable with Python, I think you’ll really (really!) like it. For me, so much more intuitive than yaml!

Well, Python is not my language and I’m still learning, but being able to write a proper function is easier for me than trying to create something out of a limited set of in-built commands.
Yaml is good for its goals too (most of the time) if one uses a proper editor, otherwise it’s a nightmare… :
Anyway, thanks for the tip :wink:
Now I have a reason to learn one more concept.

You’ll thank me once you get past the learning curve!

FWIW you can get the state of an entity in a Python script with hass.states.get(entity_id).state. Also I think you loop in a YAML script by having it call itself. But I’m new, so…

Not sure what you meant by “if there is time T” ?

This will turn off light1 if it was on for 10 minutes, and then turn it back on after 2 minutes.

automation:
  - alias: Rest light after 10 minutes
    trigger:
      - platform: state
        entity_id: light.light1
        to: on
        for:
          minutes: 10
    action:
      - service: timer.start
        entity_id: timer.timer_rest
      - service: light.turn_off
        entity_id: light.light1
  - alias: Turn on light after 2 minutes rest
    trigger:
      - platform: event
        event_type: timer.finished
        event_data:
          entity_id: timer.timer_rest
    action:
      - service: light.turn_on
        entity_id: light.light1

timer:
  timer_rest
    duration: '00:02:00'

In case you need to check the state of another entity before executing an automation, you can add a state condition in the automation.

thanks for the tip, it does the trick!

According to this pots, it’s not possible, but one can overcome that restriction by having 2 scripts :wink:

1 Like

sorry, it means “T is the total amount of time I need the entity to be turned on”

I need it during execution as an indicator that it’s time to stop the script.
Already did that by passing appropriate entity_id and desired state to the script.

Thanks for the example.
Will have a thought about it as the concept is slightly different from straightforward scripting :wink:

That’s interesting. I’ll have to read through that post. Honestly I never tried it. I just remembered seeing it done on this page:

https://www.home-assistant.io/cookbook/automation_flashing_lights/

See the last script shown named flash_room1.

LOL, that post starts by quoting the same example from the docs. Wow, what does it take to get that fixed? How long has it been leading people astray I wonder.

It should take exactly one pull request :wink:

~Cheers

Edit: I just went ahead and made a PR to get this changed…

1 Like

Thanks! I would have been happy to do that myself, but I’m not set up to do that yet, although I hope to get there at some point.

Sorry guys, what change are you talking about?

The “Examples for flashing lights” docs page that I (and others) referenced shows a script turning itself on at the end in order to implement a looping technique, but that doesn’t work (or at least, doesn’t work anymore since that was written.) @PhyberApex was nice enough to submit a fix to that doc page.