Buttons to start and stop a set of looping scripts

Hello, just installed Home Assistant to a Pi I had laying around and have a quick question.

I have a standing desk that I have integrated with HomeKit succesfully. It is able to control my desk and it has several device action built that I can utlitize.

I am trying to learn how to write a script(s) that will do that following.

Ultimately, I want the desk to go to height preset 1 by starting a script, timer down for 30 minutes, then start the next script, which goes to a different precept for 10 minutes, and then call the original script…I’m sure there’s an elegant way to create a loop or some sort of for statement, but I’m not familiar with the syntax.

So I have created both scripts as follows:
Script 1:

  1. Device action - Press preset button 1
  2. Timer start on for 30 mintutes
  3. Run script 2

Script 2: Device Action - Press preset button 2
2. Timer start on for 10 minutes
3. Call script 1

Both scripts run in Single mode.

I would like a button that starts one of the scripts. I would also like a button that either pauses this loop, or probably better on resources, ends the loop.

Can I do that with the provided script GUI, or do I need to do it with manual code?

Thank you in advance for your time and help!

Sounds as if you’d be better using three automations for most of this.

  1. Trigger when button 1 is pressed
  2. Trigger when desk as been at preset 1 for 30 minutes
  3. Trigger when desk has been at preset 2 for 10 minutes

To stop the automations running, use an input_boolean with a condition in each one so that it doesn’t run if the input_boolean is “on”.

If you’re just starting, use the UI. If you’re interested in yaml you can look at the code afterwards to see what it looks like.

Loops are not a good idea.

Automations #1: trigger only fires once.
Automations #2: the condition block.

2 Likes

You can do this in 1 automation with input_datetimes.

Create 2 input datetimes for each script. This will survive restarts and no timer is needed.

trigger:
- id: button1
  platform: state
  entity: button.abc
- id: button2
  platform: state
  entity: button.xyz
- id: 30_minutes
  platform: time
  at: input_datetime.a
- id: 10_minutes
  at: input_datetime.b
action:
- choose:
  - conditions:
    - condition: trigger
      id: button1
    sequence:
    - service: input_datetime.set_datetime
      target:
        entity_id: button.abc
      data:
       datetime: "{{ now() + timedelta(minutes=30) }}"
  - conditions:
    - condition: trigger
      id: button2
    sequence:
    - service: input_datetime.set_datetime
      target:
        entity_id: button.abc
      data:
       datetime: "{{ now() + timedelta(minutes=10) }}"
  - conditions:
    - condition: trigger
      id: 30_minutes
    sequence:
    - service: script.a
  - conditions:
    - condition: trigger
      id: 10_minutes
    sequence:
    - service: script.b

You can add another trigger with an id to end the loop. Set both the input_datetimes to something in the past… doesn’t matter what.

"{{ now() - timedelta(30) }}"

1 Like

Thank you everyone for this.

I’m still learning. I’ve been reading tutorials and watching videos, but I still don’t understand a lot of the basics. For instance, many of the videos I see show the three-dot menu button on the Home-Overview screen, top right on the blue bar, I don’t have those.

Others show their scripts showing up automatically on the dashboard as well, but that hasn’t happened for me either.

I’m fine with writing code (copy/paste, adapt templates, etc), but I don’t know any languages per se.

So that I understand, items like “button 1” and button.abc, those are things I will need to create, and then get the name/ID for an put into this automation, correct?

You said you had buttons, I just went off what you said. If you don’t have buttons, you have to describe what you mean.

Sorry if it came off that way, I would like buttons, I have been trying to figure out how to create/add them. But the tutorials and help/how-to’s I’ve been referencing seem to be outdated, with menu items and options I don’t have, or have been moved elsewhere (I’m guessing).

Happy to learn, thank you for your help.

Create an input_button helper in the UI and use those. Settings → Devices & Services → Helpers tab

Just found it in helpers right as you replied, thank you.

I’m getting the following error when I try to save the automation:
"Message malformed: extra keys not allowed @ data[‘0’]

- alias: StandingDeskCycle 
  trigger:
    - platform: state 
      entity_id: input_boolean.desk_automation
      to: 'on'  
    - platform: time
      at: input_datetime.raise_delay
    - platform: time
      at: input_datetime.lower_delay
  mode: restart 
  action:
    - choose:
        - conditions: "{{ trigger.platform == 'state' and trigger.entity_id == 'input_boolean.desk_automation' }}" 
          sequence:
            - service: input_datetime.set_datetime
              target:
                entity_id: input_datetime.raise_delay
              data:
                datetime: "{{ now() + timedelta(minutes=10) }}" 
            - service: script.raise_desk
        - conditions: "{{ trigger.platform == 'time' and trigger.entity_id == 'input_datetime.raise_delay' }}"
          sequence:
            - service: input_datetime.set_datetime
              target:
                entity_id: input_datetime.lower_delay
              data:
                datetime: "{{ now() + timedelta(minutes=30) }}"
            - service: script.lower_desk
        - default: []

Tried validating here and it says it’s fine: Best YAML Validator Online

This is the most helpful response and one that I was able to accomplish after struggling with the YAML. I kept getting errors that were incredible vague, and that no amount of reading forum/reddit responses could help me resolve.

Also tried having Gemini write me some code, same types of errors.

I made 3 Automations, and the solution is quite elagant, when the height of my desk is above or below a defined threshhold, after time it will press a preset button, as long as my booelan is set to true.