Example: Timed Switches

I have a couple of switches that i wanted to only allow to run a maximum amount of time, which is configurable by an input_slider. There have been a few times that i have forgotten to turn of my retic when triggering it manually.

This solves all of those problems. It is based around the example automation for turning a light on for 10 minutes.

It performs two functions:

1.Automation Trigger: Switch Turns On
2. Script On: Delay for x minutes

1.Automation Trigger: Switch Turns Off
2. Script Off: turn off delay script (essentially resets the timer if you manually turn off the switch)

Automations:

automation:
- alias: "Pool: Automatically turn off"
  trigger:
    - platform: state
      entity_id: switch.pool
      to: 'on'
  action:
    - service: script.turn_on
      entity_id: script.pool_timer
- alias: "Pool: reset timer"
  trigger:
    - platform: state
      entity_id: switch.pool
      to: 'off'
  action:
    - service: script.turn_off
      entity_id: script.pool_timer

Scripts:

script:
  pool_timer:
    alias: "Pool Timer"
    sequence:
      - delay: '00:{{ states.input_slider.pool_run_time.state | int }}:00'
      - service: homeassistant.turn_off
        data:
          entity_id: switch.pool

Input Sliders:

input_slider:
  pool_run_time:
    name: Pool Run Time
    initial: 10
    min: 1
    max: 60
    step: 1

Groups

group:
  backyard:
    - switch.pool
    - input_slider.pool_run_time
16 Likes

That’s awesome. I’m setting that up to turn off bedroom lights when they’re left on as well as for the pool pump.

1 Like

how’d it go with your lights/bore?

It works great. I’ve got a Sonoff POW in a weatherproof enclosure and have automations setup to turn on the pump when if it rains or if it’s windy and then turn off based on what I have the slider set to. Another automation set to turn all of the weather automations off it reaches a daily 9.5 KWh threshhold. I might switch that to a historic statistic sensor and just base it on time rather than power usage. So far it’s been working out very well. Thanks for the inspiration.

4 Likes

Would you share your code?

Automation

    trigger:
      - platform: state
        entity_id: switch.pool_pump
        to: 'on'
    action:
      - service: script.turn_on
        entity_id: script.pool_timer
  - id: pool_timer_reset
    alias: "Pool: Timer Reset"
    trigger:
      - platform: state
        entity_id: switch.pool_pump
        to: 'off'
    action:
      - service: script.turn_off
        entity_id: script.pool_timer
  - id: pool_rain_trigger
    alias: "Pool: Rain Trigger"
    trigger:
      - platform: state
        entity_id: 'sensor.dark_sky_precip'
        to: 'rain'
        for:
          hours: 0
          minutes: 15
          seconds: 0
    action:
      - service: switch.turn_on
        entity_id: switch.pool_pump
  - id: pool_wind_trigger
    alias: "Pool: Wind Trigger"
    trigger:
      - platform: numeric_state
        entity_id: 'sensor.dark_sky_wind_speed'
        above: 10
    action:
      - service: switch.turn_on
        entity_id: switch.pool_pump
  - id: backwash_alert
    alias: "Backwash Alert"
    trigger:
      - platform: numeric_state
        entity_id: 'sensor.pool_pump_current'
        above: 9.5
    action:
      - service: notify.home_assistant
        data:
          title: "Pool Pump needs Backwashed"
          message: "Pool Pump needs Backwashed"
  - id: pool_pump_limit
    alias: "Pool Pump Quota"
    trigger:
      - platform: numeric_state
        entity_id: 'sensor.pool_energy_today'
        above: 9
    action:
      - service: switch.turn_off
        entity_id: switch.pool_pump
################################################

Script

  pool_timer:
    alias: "Pool Pump Timer"
    sequence:
      - delay: '00:{{ states.input_slider.pool_pump_timer.state | int }}:00'
      - service: homeassistant.turn_off
        data:
          entity_id: switch.pool_pump

Input Slider

  pool_pump_timer:
    name: Pool Pump Timer
    initial: 60
    min: 5
    max: 240
    step: 5
2 Likes

Hi Beef-well,

In your example you show a delay in the HH:MM:SS format and the input slider goes to a max of 240. How does HA handle when the MM part is 60 or more. Normally that would need to equate to 01:00:00 which would the expected way. Any idea, does your setup simply work?

Thanks
Erwin

Hi @palitu @Beef-well

I’m trying to base on your example to use slider input for garden watering.
Configuration:

switch:
  - platform: rpi_gpio
    name: "Watering"
    ports:
      5: Water trees
      6: Water lawn
input_number:
  water_slider:
    name: Watering Run Time
    initial: 20
    min: 1
    max: 90
    step: 1

Automation:

- id: turn_off_lawn_water
  alias: Turn off garden watering
  trigger:
  - entity_id: switch.water_lawn
    platform: state
    to: 'on'
  action:
  - service: script.turn_on
    entity_id: script.water_timer

- id: reset_water_timer
  alias: Reset water timer
  trigger:
  - platform: state
    entity_id: switch.water_lawn
    to: 'off'
  action:
  - service: script.turn_off
    entity_id: script.water_timer

Script:

  water_timer:
    alias: "Watering Timer"
    sequence:
      - delay: '00:{{ states.input_slider.water_slider.state | int }}:00'
      - service: homeassistant.turn_off
        data:
          entity_id: switch.water_lawn

My problem is: HomeAss throws error on template parsing {{ states.input_slider.water_slider.state | int }}:

2018-03-17 22:19:54 ERROR (MainThread) [homeassistant.core] Error executing service <ServiceCall script.water_timer>
Traceback (most recent call last):
  File "/srv/homeassistant/lib/python3.4/site-packages/homeassistant/helpers/template.py", line 131, in async_render
    return self._compiled.render(kwargs).strip()
  File "/srv/homeassistant/lib/python3.4/site-packages/jinja2/environment.py", line 1008, in render
    return self.environment.handle_exception(exc_info, True)
  File "/srv/homeassistant/lib/python3.4/site-packages/jinja2/environment.py", line 780, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/srv/homeassistant/lib/python3.4/site-packages/jinja2/_compat.py", line 37, in reraise
    raise value.with_traceback(tb)
  File "<template>", line 1, in top-level template code
  File "/srv/homeassistant/lib/python3.4/site-packages/jinja2/filters.py", line 653, in do_int
    return int(value)
jinja2.exceptions.UndefinedError: 'None' has no attribute 'state'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/srv/homeassistant/lib/python3.4/site-packages/homeassistant/core.py", line 1010, in _event_to_service_call
    yield from service_handler.func(service_call)
  File "/srv/homeassistant/lib/python3.4/site-packages/homeassistant/components/script.py", line 163, in service_handler
    yield from script.async_turn_on(variables=service.data)
  File "/srv/homeassistant/lib/python3.4/site-packages/homeassistant/components/script.py", line 215, in async_turn_on
    yield from self.script.async_run(kwargs.get(ATTR_VARIABLES))
  File "/srv/homeassistant/lib/python3.4/site-packages/homeassistant/helpers/script.py", line 105, in async_run
    delay.async_render(variables))
  File "/srv/homeassistant/lib/python3.4/site-packages/homeassistant/helpers/template.py", line 133, in async_render
    raise TemplateError(err)
homeassistant.exceptions.TemplateError: UndefinedError: 'None' has no attribute 'state'

While i run the script with static number of minutes it works OK.
Is there workaround? Any other way to read a value from slider?

Hi @elonden,

In this thread there is sophisticated script which derives correct time.
If templates are working for you, this should be good solution.

For me templates don’t work (HA version 0.63.2) :frowning:

@Beef-well How many watts/amps does that pump pull? Are you on a 15a or 20a breaker? And what is the rating of the Sonoff POW?

The pump draws 11 amps max. It’s on a 15 amp circuit along with a couple of receptacles and a light or 2. The sOnOff POW is rated for 15 amps.

ah ok thanks. I’m in search for a 20amp switch and tough to come by. I def don’t want to go higher in the event of overdraw.

You could make your own. I’ve been playing with ESP8266s and runningTasmota on it you could get a more robust relay and do what you’re wanting to do. This is a WS2812 LED Strip controller I put together in a little waterproof box running Tasmota.
Google Photos
It could pretty easily be set up to control a 20 or 25 amp relay.

1 Like

Looks like your script is still using input_slider. input_slider has been changed to input_number.
See if that helps. I haven’t converted mine to the new version yet. I’ll share my config once I get it updated.

Here is the updated config for my bedroom light timer

input_numbers.yaml

  bedroom_lights:
    name: Bedroom Light Timer
    initial: 15
    min: 5
    max: 60
    step: 5

scripts.yaml

  bedroom_timer:
    alias: "Bedroom Light Timer"
    sequence:
      - delay: '00:{{ states.input_number.bedroom_lights.state | int }}:00'
      - service: homeassistant.turn_off
        data:
          entity_id: switch.bed_room

automations.yaml (Created using the Automation editor)

- action:
  - data:
      entity_id: script.bedroom_timer
    service: script.turn_on
  alias: Bedroom Light Timer
  condition: []
  id: '1521549825873'
  trigger:
  - entity_id: switch.bed_room
    from: 'off'
    platform: state
    to: 'on'
- action:
  - data:
      entity_id: script.bedroom_timer
    service: script.turn_off
  alias: Bedroom Light Timer Reset
  condition: []
  id: '1521550329679'
  trigger:
  - entity_id: switch.bed_room
    from: 'on'
    platform: state
    to: 'off'

Hi, would you mind to post a screenshot of the automation editor you configured?
I am messing with the syntax and home assitant is complaining about the code itself, when I copy/paste it.

Thx
Andreas

1 Like

i got it to work , thanks to you @palitu , i had these water pumps that needed to work on timers as per my input…this sorted it nicely.

Cool!

i use them a bit :slight_smile:

Thanks @Beef-well! Used it for a snooze function to turn off my bedroom tv. Works flawlessly! :smiley: