How to disable an automation for x hours by pressing a Lovelace button

Hi all,

I’d like to disable an automation temporarily by pressing a Lovelace button which would also automatically enable itself after x hours.

I have created a button to toggle the automation on/off, but would like to enhance the functionality by it automatically turning back on after x hours. Doable?

type: button
show_state: true
icon_height: 50px
tap_action:
  action: call-service
  service: automation.toggle
  target:
    entity_id: automation.auto_aircon_setting_summer
  service_data: {}
entity: automation.auto_aircon_setting_summer
name: AI Temp
icon: mdi:hvac-off

I do something similar with my aircon automations, a lovelace button that triggers a 1 hour turbo cool function. The lovelace button changes an input_boolean helper and I have an automation set, triggered by state change of that helper, which fires a 1 hour timer at the end of which the boolean is turned off (and then the aircon also turns off).

For your use case it would be simple to write an automation using the timer service call to then do something (e.g. turn an automation back on).

Nice … Thanks @Swallowtail

id: '1641430119309'
alias: Pause AI Aircon
description: Pause AI Aircon
trigger: []
condition: []
action:
  - service: automation.turn_off
    target:
      entity_id: automation.auto_aircon_setting_summer
  - delay:
      hours: 2
      minutes: 0
      seconds: 0
      milliseconds: 0
  - service: automation.turn_on
    target:
      entity_id: automation.auto_aircon_setting_summer
mode: single

and for the button to trigger it …

type: button
show_state: true
icon_height: 50px
tap_action:
  action: call-service
  service: automation.trigger
  target:
    entity_id: automation.pause_ai_aircon
  service_data: {}
name: AI Temp
icon: mdi:hvac-off
entity: automation.auto_aircon_setting_summer

That will work, doing it with a delay inside the automation.

Not how I do it though: Investigate the timer service calls along with timer helper.

Example (with some content removed for simplification):

alias: 'AC timers: Preset 2 (1hr turbo cool)'
description: []
trigger:
  - platform: state
    entity_id: input_boolean.1hr_timer_toggle_cool
    to: 'on'
action:
  - service: timer.start
    data:
      duration: '0'
    target:
      entity_id: timer.1hour_cool
  - service: input_boolean.turn_on
    target:
      entity_id: input_boolean.ac_override_auto
  - service: script.1629638189755
mode: single

…and…

alias: 'AC timers: Preset 2 ends (1hr turbo cool)'
description: ''
trigger:
  - platform: event
    event_type: timer.finished
    event_data:
      entity_id: timer.1hour_cool
condition: []
action:
  - service: input_boolean.turn_off
    target:
      entity_id:
        - input_boolean.ac_auto_on
        - input_boolean.1hr_timer_toggle_cool
  - service: script.1629638241565
mode: single
1 Like

Yeah dont wait in an automation for 2 hours. It is highly likely that this can be interrupted by a restart or reload of automations.

Timers won’t restore either (though there are ways around this).

Instead, switch your automation off with the Lovelace button and write an automation that turns the disabled automation back on if it has been off for two hours (no timer required):

trigger:
  - platform: state
    entity_id: automation.that_you_disabled
    from: 'on'
    to: 'off'
    for:
      hours: 2
action:
  - service: homeassistant.turn_on
    target:
      entity_id: automation.that_you_disabled

This can be interrupted by a restart but will begin counting again from the restart.

3 Likes

For a bonus you can have it enabled by default, so when you reload automations or restart HA it automatically enables. Or you can have it just enabled at startup:

trigger:
  - platform: state
    entity_id: automation.that_you_disabled
    from: 'on'
    to: 'off'
    for:
      hours: 2
  - platform: homeassistant
    event: start
...
4 Likes

Thank you for this! This is brilliant. And simple. I can’t remember the last time I google’d a HA how to and found a perfect solution so quick.

Cheers :beers:

I gave a try at some stuff based on the exaples above and the are not working the way intended. I have a “turn off light at a certain time” -automation:


alias: Turn off lamp at xx o'clock
description: ""
trigger:
  - platform: time
    at: "21:09:00"
condition:
  - condition: device
    type: is_on
    device_id: xxxd65
    entity_id: xxx392
    domain: light
action:
  - type: turn_off
    device_id: xxxd65
    entity_id: xxx392
    domain: light
mode: single

To delay this for 2 hours I have a second automation:

alias: >-
  Delay turn off light at xx o_clock automation for 2 hours and restart
  automation if HA reboots
description: ""
trigger:
  - platform: state
    entity_id:
      - automation.turn_off_lamp_at_xx_o_clock
    from: "on"
    to: "off"
    for:
      hours: 2
      minutes: 0
      seconds: 0
action:
  - service: homeassistant.turn_on
    target:
      entity_id: automation.turn_off_lamp_at_xx_o_clock
    data: {}
mode: single

For activation I have a button in the UI:

show_name: true
show_icon: true
type: button
hold_action:
  action: more-info
icon: mdi:alarm-light-outline
entity: >-
  automation.delay_turn_off_light_at_xx_o_clock_automation_for_2_hours_and_restart_automation_if_ha_reboots
how_state: true

What happens when I press the button is that the “delay automation for 2 hours” gets disabled. Any hints what is wrong with the code? Thanks!