Stop running script after set period of time

I have several climate entities I control with Home Assistant. I have all of these entities broken down into 5 different groups (buildings). I control temperatures with the local calendar and a toggle. Both point to either an on (occupied) or off (away) script that sets temperature to a temp in the script.

My scripts work as expected without any problem. I am trying to be proactive and add a way to stop the script in the event a condition would cause the script to continue indefinitely. The devices are Filtrite 3M Radio thermostats. They are known for having poor wifi modules. They regularly will drop out. But the time is usually only a few seconds.

My script is set up to set a temperature depending on the state, heat or cool. In the event the entity is not reporting the state the option is to repeat until the entity comes back online.

I cannot figure a way to add a timer that would stop the running script in the event the entity didn’t return online for whatever reason. My thought was to let the script run for 10 - 15 minutes and then stop. Below is one of many scripts I use for this.

alias: "Script: Set Away Mode Vaughn Hall OFF (Occupied)"
sequence:
  - choose:
      - conditions:
          - condition: device
            device_id: 15da707ba9851ec60fb823aeb98d1db3
            domain: climate
            entity_id: 6964fed31739d10d76555519d092d40c
            type: is_hvac_mode
            hvac_mode: heat
          - condition: and
            conditions:
              - condition: device
                device_id: 69539ba123367307271982fd2496d101
                domain: climate
                entity_id: ef2af9463d538cba73857d2b706af2ef
                type: is_hvac_mode
                hvac_mode: heat
              - condition: and
                conditions:
                  - condition: device
                    device_id: b65139bcf2ba51e6e4ec8ffe91c76c69
                    domain: climate
                    entity_id: a869384e01a22013e855a692f31d91c1
                    type: is_hvac_mode
                    hvac_mode: heat
        sequence:
          - service: climate.set_temperature
            metadata: {}
            data:
              temperature: 68
            target:
              entity_id:
                - climate.thermostat_1a_08_51
                - climate.vaughn_hall_s_e
                - climate.thermostat_45_a8_c3
          - service: switch.turn_on
            metadata: {}
            data: {}
            target:
              entity_id:
                - switch.thermostat_1a_08_51_hold
                - switch.vaughn_hall_s_e_hold
                - switch.thermostat_45_a8_c3_hold
      - conditions:
          - condition: device
            device_id: 15da707ba9851ec60fb823aeb98d1db3
            domain: climate
            entity_id: 6964fed31739d10d76555519d092d40c
            type: is_hvac_mode
            hvac_mode: cool
          - condition: and
            conditions:
              - condition: device
                device_id: 69539ba123367307271982fd2496d101
                domain: climate
                entity_id: ef2af9463d538cba73857d2b706af2ef
                type: is_hvac_mode
                hvac_mode: cool
              - condition: and
                conditions:
                  - condition: device
                    device_id: b65139bcf2ba51e6e4ec8ffe91c76c69
                    domain: climate
                    entity_id: a869384e01a22013e855a692f31d91c1
                    type: is_hvac_mode
                    hvac_mode: cool
        sequence:
          - service: climate.set_temperature
            metadata: {}
            data:
              temperature: 68
            target:
              entity_id:
                - climate.thermostat_1a_08_51
                - climate.vaughn_hall_s_e
                - climate.thermostat_45_a8_c3
          - service: switch.turn_on
            metadata: {}
            data: {}
            target:
              entity_id:
                - switch.thermostat_1a_08_51_hold
                - switch.vaughn_hall_s_e_hold
                - switch.thermostat_45_a8_c3_hold
      - conditions:
          - condition: template
            value_template: "{{ not is_state('climate.thermostat_1a_08_51', ['cool','heat']) }}"
        sequence:
          - repeat:
              sequence:
                - choose:
                    - conditions:
                        - condition: device
                          device_id: 15da707ba9851ec60fb823aeb98d1db3
                          domain: climate
                          entity_id: 6964fed31739d10d76555519d092d40c
                          type: is_hvac_mode
                          hvac_mode: heat
                      sequence:
                        - service: climate.set_temperature
                          metadata: {}
                          data:
                            temperature: 68
                          target:
                            entity_id:
                              - climate.thermostat_1a_08_51
                              - climate.vaughn_hall_s_e
                              - climate.thermostat_45_a8_c3
                        - service: switch.turn_on
                          metadata: {}
                          data: {}
                          target:
                            entity_id:
                              - switch.thermostat_1a_08_51_hold
                              - switch.vaughn_hall_s_e_hold
                              - switch.thermostat_45_a8_c3_hold
                    - conditions:
                        - condition: device
                          device_id: 15da707ba9851ec60fb823aeb98d1db3
                          domain: climate
                          entity_id: 6964fed31739d10d76555519d092d40c
                          type: is_hvac_mode
                          hvac_mode: cool
                      sequence:
                        - service: climate.set_temperature
                          metadata: {}
                          data:
                            temperature: 68
                          target:
                            entity_id:
                              - climate.thermostat_1a_08_51
                              - climate.vaughn_hall_s_e
                              - climate.thermostat_45_a8_c3
                        - service: switch.turn_on
                          metadata: {}
                          data: {}
                          target:
                            entity_id:
                              - switch.thermostat_1a_08_51_hold
                              - switch.vaughn_hall_s_e_hold
                              - switch.thermostat_45_a8_c3_hold
              until:
                - condition: or
                  conditions:
                    - condition: state
                      entity_id: climate.thermostat_1a_08_51
                      state: heat
                      for:
                        hours: 0
                        minutes: 0
                        seconds: 10
                    - condition: and
                      conditions:
                        - condition: state
                          entity_id: climate.thermostat_1a_08_51
                          state: cool
                          for:
                            hours: 0
                            minutes: 0
                            seconds: 10
      - conditions:
          - condition: template
            value_template: "{{ not is_state('climate.vaughn_hall_s_e', ['cool','heat']) }}"
        sequence:
          - repeat:
              sequence:
                - choose:
                    - conditions:
                        - condition: device
                          device_id: 69539ba123367307271982fd2496d101
                          domain: climate
                          entity_id: ef2af9463d538cba73857d2b706af2ef
                          type: is_hvac_mode
                          hvac_mode: heat
                      sequence:
                        - service: climate.set_temperature
                          metadata: {}
                          data:
                            temperature: 68
                          target:
                            entity_id:
                              - climate.thermostat_1a_08_51
                              - climate.vaughn_hall_s_e
                              - climate.thermostat_45_a8_c3
                        - service: switch.turn_on
                          metadata: {}
                          data: {}
                          target:
                            entity_id:
                              - switch.thermostat_1a_08_51_hold
                              - switch.vaughn_hall_s_e_hold
                              - switch.thermostat_45_a8_c3_hold
                    - conditions:
                        - condition: device
                          device_id: 69539ba123367307271982fd2496d101
                          domain: climate
                          entity_id: ef2af9463d538cba73857d2b706af2ef
                          type: is_hvac_mode
                          hvac_mode: cool
                      sequence:
                        - service: climate.set_temperature
                          metadata: {}
                          data:
                            temperature: 68
                          target:
                            entity_id:
                              - climate.thermostat_1a_08_51
                              - climate.vaughn_hall_s_e
                              - climate.thermostat_45_a8_c3
                        - service: switch.turn_on
                          metadata: {}
                          data: {}
                          target:
                            entity_id:
                              - switch.thermostat_1a_08_51_hold
                              - switch.vaughn_hall_s_e_hold
                              - switch.thermostat_45_a8_c3_hold
              until:
                - condition: or
                  conditions:
                    - condition: state
                      entity_id: climate.vaughn_hall_s_e
                      state: heat
                      for:
                        hours: 0
                        minutes: 0
                        seconds: 10
                    - condition: and
                      conditions:
                        - condition: state
                          entity_id: climate.vaughn_hall_s_e
                          state: cool
                          for:
                            hours: 0
                            minutes: 0
                            seconds: 10
      - conditions:
          - condition: template
            value_template: "{{ not is_state('climate.thermostat_45_a8_c3', ['cool','heat']) }}"
        sequence:
          - repeat:
              sequence:
                - choose:
                    - conditions:
                        - condition: device
                          device_id: b65139bcf2ba51e6e4ec8ffe91c76c69
                          domain: climate
                          entity_id: a869384e01a22013e855a692f31d91c1
                          type: is_hvac_mode
                          hvac_mode: heat
                      sequence:
                        - service: climate.set_temperature
                          metadata: {}
                          data:
                            temperature: 68
                          target:
                            entity_id:
                              - climate.thermostat_1a_08_51
                              - climate.vaughn_hall_s_e
                              - climate.thermostat_45_a8_c3
                        - service: switch.turn_on
                          metadata: {}
                          data: {}
                          target:
                            entity_id:
                              - switch.thermostat_1a_08_51_hold
                              - switch.vaughn_hall_s_e_hold
                              - switch.thermostat_45_a8_c3_hold
                    - conditions:
                        - condition: device
                          device_id: b65139bcf2ba51e6e4ec8ffe91c76c69
                          domain: climate
                          entity_id: a869384e01a22013e855a692f31d91c1
                          type: is_hvac_mode
                          hvac_mode: cool
                      sequence:
                        - service: climate.set_temperature
                          metadata: {}
                          data:
                            temperature: 68
                          target:
                            entity_id:
                              - climate.thermostat_1a_08_51
                              - climate.vaughn_hall_s_e
                              - climate.thermostat_45_a8_c3
                        - service: switch.turn_on
                          metadata: {}
                          data: {}
                          target:
                            entity_id:
                              - switch.thermostat_1a_08_51_hold
                              - switch.vaughn_hall_s_e_hold
                              - switch.thermostat_45_a8_c3_hold
              until:
                - condition: or
                  conditions:
                    - condition: state
                      entity_id: climate.thermostat_45_a8_c3
                      state: heat
                      for:
                        hours: 0
                        minutes: 0
                        seconds: 10
                    - condition: and
                      conditions:
                        - condition: state
                          entity_id: climate.thermostat_45_a8_c3
                          state: cool
                          for:
                            hours: 0
                            minutes: 0
                            seconds: 10
mode: single
icon: mdi:script-outline

Could you create an input_boolean and have the script turn it on when it starts running? Then an automation to stop the script when the input_boolean has been on for 10 minutes?

Yes, I think that would be a good possibility. I was also thinking about an automation with a timer that would start when the script fired. Then have the automation stop the script if the script state was still on at the end of the timer. Not sure which way would be best.

it always feels weird to me to force a stop of a script. however if you must, here’s a way to do it that may be simpler. use, parallel. one parallel thread will do the delay then kill of the script. the other thread does your normal execution.

this prevents having a separate automation and triggering it, or creating a helper and a separate automation, etc.

alias: New Script
sequence:
  - parallel:
      - sequence:
          - delay:
              hours: 0
              minutes: 15
              seconds: 0
              milliseconds: 0
          - service: script.turn_off
            metadata: {}
            data: {}
            target:
              entity_id: script.name_of_this_script
      - sequence:
          - service: <put your sequence of commands>
            data: {}

Thank you. Yes I agree, I just thought that would good housekeeping in the event that the entity would go offline not to return. The script would continue to run indefinitely. I will try your code.

1 Like