Press a button and wait until a specified time before executing an action

I thought this would be straightforward but I can’t find it. Apologies in advance if it is simple.

I would like to press a button, then at a later specified time, execute an action.

Specifically, I commonly think of turning the water heating on during peak electricity rate so I don’t do it - then by the time it’s off peak, I’ve forgotten - go to have a shower some hours later and the water is cold.

Below is as far as I’ve got, do I need to programmatically calculate how long it is until 7pm and then delay for that long?

alias: Schedule Immersion
description: Schedule Immersion
trigger:
  - platform: state
    entity_id:
      - input_button.schedule_immersion_after_peak
    from: "off"
    to: "on"
condition: []
action:
  - delay:
      hours: 0
      minutes: 1
      seconds: 0
      milliseconds: 0
  - type: turn_on
    device_id: 
    entity_id: 
    domain: switch
mode: single

You could use this before your actual action (instead of the delay) :

- wait_for_trigger:
      - platform: time
        at: "07:00:00"
    continue_on_timeout: false

Which just waits until it’s 7:00, then let’s the automation continue.

1 Like

Thanks! Wait for Trigger was the simpler answer I was expecting to find, avoiding mucking about programmatically with delay.

For some reason I don’t see a checkbox to mark your answer as correct though!

EDIT: the solution checkbox appeared today!

Does your automation actually trigger? An input_button doesn’t have a state, so it can’t go from “off” to “on”.

1 Like

Use an input boolean to signal that you want it to run (or if it must be a button, set the input boolean from the button press).

Then write the automation to trigger at 7pm, check the input boolean is on in the condition, and turn it off again as the last step of the action.

1 Like

An input_button (or any button) changes state to the timestamp of the last trigger when pressed, so a state change trigger (without a from/to state) could be used directly

Thanks people - I fixed the button once I had the automation working. I’ve gone with any (null) state change for now, I have another button that uses input_boolean which I’d copied this automation from, but I’d set up the button helper as an experiment. Basically, ‘Button’ was top of the list in the GUI, I didn’t find it very intuitive that ‘Toggle’=input_boolean, and I didn’t bother changing it! Will see which one I like best - the difference is the other button shows a different colour to reassure the user it’s been pressed and then goes back to original colour at the end of the automation, which is a few more steps.

alias: Schedule Immersion after peak
description: Schedule Immersion
trigger:
  - platform: state
    entity_id:
      - input_button.schedule_immersion_after_peak
    to: null
condition: []
action:
  - wait_for_trigger:
      - platform: time
        at: "19:00:00"
    continue_on_timeout: false
  - type: turn_on
    device_id: 
    entity_id: 
    domain: switch
  - service: notify.whatsapp
    metadata: {}
    data:
      message: "♨️ Delayed action: Immersion switched on"
mode: single

Here you go: I stopped being lazy - full version with both types of helper below. At some point I’ll delete the one I find myself not using.

alias: Schedule Immersion after peak
description: Schedule Immersion
trigger:
  - platform: state
    entity_id:
      - input_button.schedule_immersion_after_peak
    to: null
  - platform: state
    entity_id:
      - input_boolean.schedule_immersion_after_peak
    from: "off"
    to: "on"
condition: []
action:
  - wait_for_trigger:
      - platform: time
        at: "19:00:00"
    continue_on_timeout: false
  - type: turn_on
    device_id: 683e04948c4d2745c756fddf22795070
    entity_id: 39d1018d0c3c845a9a1b5a11df9af110
    domain: switch
  - service: notify.whatsapp
    metadata: {}
    data:
      message: "♨️ Delayed action: Immersion switched on"
  - service: input_boolean.turn_off
    metadata: {}
    data: {}
    target:
      entity_id: input_boolean.schedule_immersion_after_peak
mode: single

If you’re going to use a boolean anyway, you would be better served to follow @Troon’s advice and trigger on the time. You can use a Choose action so your button switches the boolean and the time turns on the switch:

alias: Schedule Immersion after peak
description: Schedule Immersion
trigger:
  - platform: state
    entity_id:
      - input_button.schedule_immersion_after_peak
    to: null
    id: button
  - platform: time
    at: "19:00:00"
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: button
        sequence: 
          - service: input_boolean.turn_on
            metadata: {}
            data: {}
            target:
              entity_id: input_boolean.schedule_immersion_after_peak 
    default:
      - type: turn_on
        device_id: 683e04948c4d2745c756fddf22795070
        entity_id: 39d1018d0c3c845a9a1b5a11df9af110
        domain: switch
      - service: notify.whatsapp
        metadata: {}
        data:
          message: "♨️ Delayed action: Immersion switched on"
      - service: input_boolean.turn_off
        metadata: {}
        data: {}
        target:
          entity_id: input_boolean.schedule_immersion_after_peak
mode: single

Using the input_button entity is probably just adding an unnecessary step unless you have other uses for it…