How do you make an automation/script if its running and you turn off turns off the switch

so i have 2 things i wanna do…

for my sisters Home assistant. i setup a automation turns on her garage light at 9:30 till 10pm and it works great… and i linked it to a button

but if she wants to hit the automation button to turn it off leaves the light on… anyway to turn it off when you press the button?

2nd…
for my sisters Block heater… i had to do a script to a button… so when she presses it… it runs a timer for 4 hours then shuts off the switch/plug … but id also like it if you press Turn 4 Hour timer button to off… that it would turn off the plug… instead of turning off the script but leaving the plug still on

is there a way to do that?

Post the code you already have, then people can help figure out what would need to change.

i was thinking you need like 2 automations

1 for the button and 2nd one to toggle the timer and toggle the switch/plug for the light and block heater

but was hoping it to be all in 1 automation so 1 for light 1 for block heater not 4 1 to turn on off garage light automation and 1 to turn on and off the block heater automation… so then youd have 4 automations/scripts for 2 devices

so
for light
1… press timer light button automation it comes on at 9:30 but if button gets pressed again say 9:40pm it turns off the automation and turns off the Light

2… same like above
press the timer for 4 hours for block heater… within that 4 hours you press it again it shuts off the script and shuts off the block heater switch

i wasnt able to make it an automation i tried all the options but u always need a trigger… just starting the automation wasnt a good enough trigger

automation code to turn on garage light

alias: activate light for 930pm till 10pm
description: ''
trigger:
  - platform: time
    at: '21:30:00'
    id: outside_light_timer
condition: []
action:
  - type: turn_on
    device_id: e3d3c4b3282d3a6e23e97327dde1fdd3
    entity_id: switch.m_garage_light
    domain: switch
  - wait_for_trigger:
      - platform: time
        at: '22:00:00'
  - type: turn_off
    device_id: e3d3c4b3282d3a6e23e97327dde1fdd3
    entity_id: switch.m_garage_light
    domain: switch
  - service: automation.turn_off
    target:
      entity_id: automation.activate_light_for_930pm_till_10pm
mode: single

script to turn on block heater

sequence:
  - service: switch.turn_on
    target:
      entity_id: switch.m_block_heater
  - delay:
      hours: 4
      minutes: 0
      seconds: 0
      milliseconds: 0
  - service: switch.turn_off
    target:
      entity_id: switch.m_block_heater
mode: single
alias: Turn Off Mitchs Block Heater 4 hours

i tried to make the script an automation but apparently you cant create an automation without using a trigger… i figure using no trigger automaticlly causes the automation to run from the moment you enable the automation… but it doesnt

What is the button for exactly? Is it enabling the automation or is it a way to turn the light off?

so both buttons Light and the Block heater
they toggle the script and the automation

type: horizontal-stack
title: null
size: 10%
cards:
  - type: custom:button-card
    color_type: card
    icon: mdi:lightbulb
    name: Garage Light Timer
    entity: automation.activate_light_for_930pm_till_10pm
    tap_action:
      action: toggle
    styles:
      card:
        - font-size: 20px
        - height: 150px
        - width: 145px
        - color_type: card
        - color: rgb(255,255,255)
    state:
      - value: 'off'
        name: Garage Light<br>9:30-10pm<br>Timer<br>Off
        color: rgb(255, 0, 0)
      - value: 'on'
        name: Garage Light<br>9:30-10pm<br>Timer<br>On
        color: rgb(0, 128, 0)
      - value: unavailable
        name: Garage Light<br>9:30-10pm<br>Timer<br>Offline
        color: rgb(0, 0, 128)
  - type: custom:button-card
    color_type: card
    icon: mdi:car
    name: Mitch's Block<br>Heater<br>4 hr Timer<br>Offline
    entity: script.1642733925177
    tap_action:
      action: toggle
    styles:
      card:
        - font-size: 20px
        - height: 150px
        - width: 145px
        - color_type: card
        - color: rgb(255,255,255)
    state:
      - value: 'off'
        name: Mitch's Block<br>Heater<br>4hr Timer<br>Off
        color: rgb(255, 0, 0)
      - value: 'on'
        name: Mitch's Block<br>Heater<br>4hr Timer<br>On
        color: rgb(0, 128, 0)
      - value: unavailable
        name: Mitch's Block<br>Heater<br>4hr Timer<br>Offline
        color: rgb(0, 0, 128)

in the end i figured you cant do it in 2 automations/scripts
1 for the light 1 for heater… probably needs a second one for checking things and the first one all it does is toggle…

@walrus_parka so i not sure can it be done in 2 automations 1 for each… if possible?
or is you need 1 to toggle things and 2nd one runs timers?
havent figured it out but figured id ask

or can you do a group thing
so a like the 1 button be
switch_group_1:

  • automation: automation.activate_light_for_930pm_till_10pm
  • switch.m_garage_light = off

Light:
Is there a reason to have the automation toggled via UI? Why not have the button reference the light itself and run the automation in the background?

Heater:
A way to accomplish the heater is to have an input_boolean named “m_block_heater” be toggled in the UI. It would trigger the automation and you could use it to match the state of the heater.

EDIT: Example worked on my config. Input boolean will track the switch if it is turned off manually as well.

alias: Heater Example
mode: restart
description: ''
trigger:
  - platform: state
    entity_id: input_boolean.m_block_heater
    id: input_boolean
  - platform: state
    entity_id: switch.m_block_heater
    id: switch
  - platform: state
    entity_id: switch.m_block_heater
    id: heater_on
    to: 'on'
    for:
      hours: 4
      minutes: 0
      seconds: 0
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: input_boolean
        sequence:
          - service: "switch.turn_o{{ 'n' if trigger.to_state.state == 'on' else 'ff' }}"
            data:
              entity_id: switch.m_block_heater
      - conditions:
          - condition: trigger
            id: switch
          - condition: template
            value_template: "{{ trigger.to_state.state != states('input_boolean.m_block_heater') }}"
        sequence:
          - service: "input_boolean.turn_o{{ 'n' if trigger.to_state.state == 'on' else 'ff' }}"
            data:
              entity_id: input_boolean.m_block_heater
      - conditions:
          - condition: trigger
            id: heater_on
        sequence:
          - service: input_boolean.turn_off
            target:
              entity_id: input_boolean.m_block_heater
    default: []

ill try it later… not at home to test it…

what does the input boolean do or mean?

as for the light i made it simple for my sister…
so i use simple scheduler and i set the timer to turn on on 9:30pm till 10pm on monday and tuesdays as she gets home from work…
but sometimes she works other nights and gets home same time

so my simple way was press a button on her cell or tablet… before she leaves for work so then automation turns on turns light on at 9:30 shuts off at 10pm and then disables the automation as it a once and a while… it works but if i were to turn it off during the 9:30-10 then the automation isnt able to turn it off when you turn off automation… so i wanted click button turn on automation if you click it off before 9:30pm no big deal… but if u click it within the 930 10 window i want it to turn off the automation and turn off the light… for now i only get it to turn off the automation

and same for the block heater it also doubles for my mother…
so they can both Just press Button on the tablet or phone to Activate it for 4 hours and after 4 hours it shuts off so at anytime…
and works well for that

but if they were to turn it off the timer it leaves the heater on… but i also want if you press the heater timer to also turn off the heater plug

and i do have 2nd buttons for each one to turn on the garage light and 1 for heater plug
i added these timer options as my sister kept asking me to adjust her scheduler each day for her work… so instead i wanted it to work it on her own just press button and it just goes

they both work well if you let the timer run its course but not if you wanna turn off the automation when its running

Technically speaking, your script doesn’t employ a timer entity, it uses a delay statement. In addition, it’s a long delay of 4 hours. If you execute Reload Scripts or restart Home Assistant during that 4-hour period, the delay will be cancelled (and the block heater will not be turned off). You don’t even have to explicitly execute Reload Scripts because it’s done in the background whenever you use the Script Editor to save a new/modified script.

To avoid the risk of failing to turn off the block heater, the script needs to be re-designed to be immune to reloads/restarts. Let me know if you want help with that.

It can serve as a “Go/No Go” flag for the automation.

  • If the input_boolean is on then the automation will execute its actions at the scheduled times.
  • If the input_boolean is off then the automation will not execute its actions at the scheduled times.

This is a preferable way of controlling an automation’s behavior (as opposed to enabling/disabling the automation).


EDIT

Here’s an example employing an input_boolean to control the behavior of when the light should be turned on (or not).

It always turns off the light at 22:00 but only turns it on at 21:30 if the input_boolean is on.

alias: activate light for 930pm till 10pm
description: ''
trigger:
- id: 'on'
  platform: time
  at: '21:30:00'
- id: 'off'
  platform: time
  at: '22:00:00'
condition: []
action:
- choose:
  - conditions:
    - "{{ trigger.id == 'on' }}"
    - "{{ is_state('input_boolean.garage_light', 'on') }}"
    sequence:
    - service: switch.turn_on
      target:
        entity_id: switch.m_garage_light
  - conditions: "{{ trigger.id == 'off' }}"
    sequence:
    - service: switch.turn_off
      target:
        entity_id: switch.m_garage_light
  default: []
mode: single

@walrus_parka ok ya id like to learn.
i posted last year in the Features request… for when home assistant reboots to remember the timers… as i use one for my hot water tank… after 5 hours shuts off… and i had another automation that when reboots 5 hours from time HA reboots to shut off hot water tank as a safety… i think only 5 votes on that feature request i posted…

but ya id learn

@123 ok so basiclly its a If Statement then If On go this route if Off do nothing

so reason i turn on and off the automation… my sister likes it i have Red for Off Devices and Green for On

so reason i did the automation so when she presses the button the she sees the Automation is On and it will come on for 9:30 till 10pm

as can you display the input boolean as a Button so On is green and Off is Red?

and then does it turn off when you press the button… is that the condition…
so either run out the time… or turn off when the Button is pressed

ill try your both codes above when i get home later today…
and then the option for the block heater…

i seen there is blueprint stuff but no option for what i wanted … be nice for anyone else if they wanted a blue print …

but ill defiantly try later today

An input_boolean is not an if statement.