#while loop that immediatly stops

1 messages · Page 1 of 1 (latest)

rugged nest
#

What building block do i need to use for a while loop that doesnt wait for it to end when the condition is false

basically:

while (some_bool) {
if (!some_bool) break;
// action1
if (!some_bool) break;
// action2
if (!some_bool) break;
etc.
}
// do this immediatly when some_bool is false

ideally immediatly even aborting a running delay, but if thats impossible atleast do it on the next action/step

rugged nest
wild phoenix
ebon shard
#

Maybe if you explain WHAT you need and not HOW you got it, is easier to help you 🙂

rugged nest
ebon shard
#

And to be kind with people trying to help 😉

rugged nest
rugged nest
ebon shard
#

Long explanation for those interested is that a proposed solution may be totally wrong. Because of that is necessary to know the original problem, not the proposed solution, that, as I said, can be totally wrong for that problem

rugged nest
rugged nest
ebon shard
#

Well, I'm a programmer and see everyday that the final user does have no idea about what really wants. In my work, as a programmer, the ability to convert a problem into a coded solution is very valuable. That is what a programmer is. Problem -> coded solution 🤷‍♂️

#

Not debugging. That can be made with Copilot 😉

rugged nest
#

then i have to assume that im not going to get constructive help from you. maybe someone else asks for the same thing so ill get it as always ^^

ebon shard
#

I'm not assuming anything 🙂 . I'm just asking for the original problem, just in case there would be a better solution than the proposed.
You can read again my messages, if you want

wild phoenix
pure socket
#

☮️ please

subtle zealot
#

You could put all the actions into their own script (e.g. script.step_actions), and then your main automation (or script) would have these steps:

  1. run script.step_actions using the non-blocking script.turn_on command.
  2. “wait for trigger” using two triggers:
  • some_bool
  • the state of script.step_actions changes to off
  1. Call script.turn_off to stop the script.step_actions script if it’s still running.

There might be better ways to achieve your ultimate goal but you’d need to share more.

granite crag
rugged nest
rugged nest
# subtle zealot You could put all the actions into their own script (e.g. `script.step_actions`)...

There might be better ways to achieve your ultimate goal but you’d need to share more.
thanks for being reasonable and not snarky about it: well, if you really think the goal would help, i have a space heater that i want to run in intervals (30m on, 60m off) so it doesnt constantly heat but gives managable bursts, and i use a zigbee remote to toggle that loop (input helper bool) and i want to break out of the loop as soon as the helper bool becomes false by the remote button (therefor ending the heating cycle instantly and not waiting for it to end)

rugged nest
# granite crag Then simply use a condition action.

where and which condition would you suggest?

alias: Space heater timer
description: ""
triggers:
  - entity_id:
      - input_boolean.space_heater_timer
    from: "off"
    to: "on"
    for:
      hours: 0
      minutes: 0
      seconds: 1
    trigger: state
  - event: start
    trigger: homeassistant
    enabled: false
conditions: []
actions:
  - action: script.buzz
    metadata: {}
    data: {}
    enabled: false
  - type: turn_off
    device_id: 12243719fd70df5398bd1f13862b829e
    entity_id: 4eaadaef9d436d4ce6bdd83cfcf1f9f1
    domain: switch
  - repeat:
      while:
        - condition: state
          entity_id: input_boolean.space_heater_timer
          state: "on"
      sequence:
        - device_id: 12243719fd70df5398bd1f13862b829e
          domain: climate
          entity_id: 98d8b15d7cfacaf13586889b61d98ee0
          type: set_preset_mode
          preset_mode: H1
        - if:
            - condition: state
              entity_id: input_boolean.space_heater_timer
              state: "off"
          then:
            - stop: Space Heater timer is not on
          enabled: false
        - delay:
            hours: 0
            minutes: 1
            seconds: 30
            milliseconds: 0
        - if:
            - condition: state
              entity_id: input_boolean.space_heater_timer
              state: "off"
          then:
            - stop: Space Heater timer is not on
          enabled: false
        - device_id: 12243719fd70df5398bd1f13862b829e
          domain: climate
          entity_id: 98d8b15d7cfacaf13586889b61d98ee0
          type: set_hvac_mode
          hvac_mode: fan_only
        - if:
            - condition: state
              entity_id: input_boolean.space_heater_timer
              state: "off"
          then:
            - stop: Space Heater timer is not on
          enabled: false
        - delay:
            hours: 0
            minutes: 3
            seconds: 0
            milliseconds: 0
        - if:
            - condition: state
              entity_id: input_boolean.space_heater_timer
              state: "off"
          then:
            - stop: Space Heater timer is not on
          enabled: false
  - type: turn_on
    device_id: 12243719fd70df5398bd1f13862b829e
    entity_id: 4eaadaef9d436d4ce6bdd83cfcf1f9f1
    domain: switch
  - device_id: 12243719fd70df5398bd1f13862b829e
    domain: climate
    entity_id: 98d8b15d7cfacaf13586889b61d98ee0
    type: set_hvac_mode
    hvac_mode: fan_only
  - action: script.buzz
    metadata: {}
    data: {}
    enabled: false
  - action: script.buzz
    metadata: {}
    data: {}
    enabled: true
mode: restart
granite crag
subtle zealot
# rugged nest > There might be better ways to achieve your ultimate goal but you’d need to sha...

I would suggest the following:
triggers:

  • state of input_boolean from off to on, with trigger_id: turn_on
  • state of input_boolean from on to off, with trigger_id: turn_off
  • state of space heater to on, for: 30 minutes, with trigger_id: turn_off
  • state of space heater to off, for: 60 minutes, with trigger_id: turn_on
    conditions:
  • nothing here
    actions:
  • choose:
    • option 1 conditions:
      • triggered by turn_on
      • state of input_boolean is on
    • option 1 actions:
      • turn on space heater
    • option 2 conditions:
      • triggered by turn_off
    • option 2 actions:
      • turn off space heater
#

in general, I advise against any delays longer than about a minute. Long-running automations mean they are stateful, and those states will get lost upon HA restarts and when automations are reloaded. In just about every case there is no need to have an automation running and doing nothing.

rugged nest
rugged nest
rugged nest
subtle zealot
rugged nest
rugged nest
#

the heater even has a set_temp function but it doesnt quite do what i want, instead of switching to fan only mode when it reaches the temp, it shuts off and doesnt come back on when its too cold again, otherwise that would have been a compromise i couldve accepted

#

if were ignoring the "dont use long delays in automations" thing for a second a break from loop action would be ideal for me even if i manually have to add it after each step (which i already did with the stop action for testing) tho but i guess since im not using the loop as "intended" its probably not gonna be accepted as a feature request

subtle zealot
#

There is a “repeat until” that will run the actions and break out of the loop before starting the loop again, but it doesn’t check after each step inside the loop

rugged nest
#

yeah that will just move the choke point to a different part of the loop. i guess ill just accept that i have to manually turn the heater off while its still in its last loop iteration

subtle zealot
#