Turn off switch after X minutes

Hello once again!

I have set up mqtt to an arduino, works great. Now I would like to turn off the switch after x amounts of minutes. I am testing first with a constant amount of seconds but it ain’t happening…

Also, when I try to make the time a slider, my config won’t compile. Anyone?

automation:
  - alias: Sprinker Time
    trigger:
      platform: state
      entity_id: switch.back_sprinker
      to: 'on'
      for:
        seconds: '10'
    action:
      service: homeassistant.turn_off
      entity_id: switch.back_sprinker

switch:
  - platform: mqtt
    name: "Back Sprinkler"
    state_topic: "home/outside/control1"
    command_topic: "home/outside/control/set1"
    payload_on: "1"
    payload_off: "0"
    optimistic: false
    qos: 0
    retain: true

Code for slide:

'00:{{ states.input_slider.timer_options_slider.state | int }}:00'

Could the issue be that you want switch.back_sprinkler, not switch.back_sprinker?

I’m pretty sure that you can’t use templates as a value for for: at this time. You can probably get around this by creating a template sensor that compares the timestamp to your slider value. Then trigger your automation from that sensor.

Take a look at the alarm clock thread if you need some examples of time calculations with sliders.

1 Like

Very much so… Thank you!

Your example pointed me elsewhere, got it to work, seconds for now, but easily turns into minutes:

input_slider:
  slider1:
    name: Sprinkler Time
    initial: 10
    min: 1
    max: 30
    step: 1

# Example configuration.yaml entry
automation:
  - alias: Sprinker Time
    trigger:
      platform: state
      entity_id: switch.back_sprinkler
      to: 'on'
    action:
      service: homeassistant.turn_on
      entity_id: script.timed_sprinkler

script:
  timed_sprinkler:
    alias: "Set Sprinkler Timer"
    sequence:
      # Cancel ev. old timers
      - service: script.turn_off
        data:
           entity_id: script.timer_off
      # Set new timer
      - service: script.turn_on
        data:
          entity_id: script.timer_off
  timer_off:
    alias: "Turn Off Sprinkler"
    sequence:
      - delay: '00:00:{{ states.input_slider.slider1.state | int }}'
      - service: homeassistant.turn_off
        entity_id: switch.back_sprinkler

switch:
  - platform: mqtt
    name: "Back Sprinkler"
    state_topic: "home/outside/control1"
    command_topic: "home/outside/control/set1"
    payload_on: "1"
    payload_off: "0"
    optimistic: false
    qos: 0
    retain: true
1 Like

I can never keep straight where templates are allowed! I think being able to use them in delay is fairly recent.

1 Like

I am pretty new to the game, have some experience coding, I agree, rules some to be all over the place :slight_smile:

this is my solution Example: Timed Switches

this monitors for the switch turning on, then initiates a script which will turn it off, after a certain number of minutes, as defined by a slider.

I use this for my pool pump and my reticulation bore, and works really well!

1 Like

Seems we came to a similar outcome! Thanks for the code!

Hey I recently installed home assistant, where do I put the code? I tried pasting the code in configuration.yaml, automations.yaml and script.yaml but I’m getting this error message:

General Errors:
- Setup failed for script: Invalid config.

Successful config (partial)
script:

I would appreciate any help, this looks kinda confusing.

You can either put it all in configuration.yaml (which is basically what the example above is doing), or split it up.
The reason for splitting it up is that configuration.yaml would become very big very quickly, so it makes sense to have a separate file (or files) for automations, another one for scripts etc.
Given that you have a separate automations.yaml file it looks like you’re set up that way.
Basically what happens is that in configuration.yaml you create an automations setting that refers to the file where the automations are kept. Same for scripts etcetera.
So if you do that, then you need to split the code mentioned above over those various files. The automations part goes in the automations file, etcetera.

1 Like

Thank you for the clarification, so it means that I can put the whole code in to test it and then I can split it up.

I’ve tried to test the code, my sonoff outlet works but the automation doesn’t do anything, I may be missing something…

Im planning to turn on a pump via homebridge and let it turn off automatically after a few minutes. I would appreciate any help.

switch:
  • platform: mqtt
    name: “pump”
    state_topic: “stat/sonoff/POWER”
    command_topic: “cmnd/sonoff/power”
    payload_on: “ON”
    payload_off: “OFF”
    qos: 1
    retain: true

input_slider:
slider1:
name: pump time
initial: 10
min: 1
max: 30
step: 1

automation 1:

  • alias: pump
    trigger:
    platform: state
    entity_id: switch.pump
    to: ‘on’
    action:
    service: homeassistant.turn_on
    entity_id: script.timed_pump

script 1:
timed_pump:
alias: “pump Timer”
sequence:
# Cancel ev. old timers
- service: script.turn_off
data:
entity_id: script.timer_off
# Set new timer
- service: script.turn_on
data:
entity_id: script.timer_off
timer_off:
alias: “Turn Off pump”
sequence:
- delay: ‘00:00:{{ states.input_slider.slider1.state | int }}’
- service: homeassistant.turn_off
entity_id: switch.pump