Turn Switch on every hour with conditions

Hi!

I want to turn the switch on ever hour for 1 minute between 10:30 am and 6:30 pm, will this below automation work?

So, it’ll switch on at 10:30,11:30,12:30…5:30,6:30

alias: Watering Every hour
description: ''
trigger:
  - platform: time_pattern
    hours: '1'
    minutes: '12'
condition:
  - condition: time
    before: '18:30:00'
    after: '10:30:00'
action:
  - type: turn_on
    device_id: 
    entity_id: switch.sonoff_1000ecdac0
    domain: switch
mode: queued
max: 9

No. That will turn the switch on at 01:12 And it will not switch it off. Try this:

alias: Watering on Every hour
description: ''
trigger:
  - platform: time_pattern
    minutes: '30' # matches at 30 minutes past the hour
condition:
  - condition: time
    before: '18:31:00'
    after: '10:29:00'
action:
  - service: switch.turn_on
    entity_id: switch.sonoff_1000ecdac0
  - delay: 60
  - service: switch.turn_off
    entity_id: switch.sonoff_1000ecdac0
mode: single

How long does your home assistant server take to restart?

(not an unrelated question)

It takes around 40 seconds to a minute I’m guessing.

For the switch off, I created another automation

alias: New Automation
description: ''
trigger:
  - platform: device
    type: turned_on
    device_id: 42ea5a399e4a52010546fd34aded725d
    entity_id: switch.sonoff_1000ecdac0
    domain: switch
    for:
      hours: 0
      minutes: 
      seconds:60 
condition: []
action:
  - type: turn_off
    device_id: 
    entity_id: switch.
    domain: switch
mode: queued

I did not want to use the delay functionality as I read in this post that the delay would be reset in case the HA is restarted on automations reloaded

That is really only relevant for long delays. Yours is only a minute.

Your off automation suffers the same issue. The for time won’t work after a restart.

The only way around this is to set an input datetime +1 minute from now() when you turn the switch on. Then use a time trigger to turn off the switch at that time.

Unfortunately as your restart time may be greater than one minute (hence my leading question) this off automation may miss the off time too. So not really worth doing, unless you also trigger an automation on restart of home assistant and check if the input datetime is in the past.

1 Like

Wow!

Sounds too complex!

Thank you for all the support and answering my stupid queries!

Much gratitude :hugs:

I have 11 zones in my irrigation system and I use this. I water for short amount of time for multiple cycles which helps with water consumption and minimizes. I have input variables that control start and duration of each zone. My maximum zone time is 15 minutes and most are set to 5 minutes. This may be more than you want to do.

- id: 'xxxxxxxxxxxxx'
  alias: Water Lawn with repeat
  description: Water all zones with repeat
  trigger:
  - platform: time
    at:
    - input_datetime.cycle1
    - input_datetime.cycle2
    - input_datetime.cycle3
    - input_datetime.cycle4
    - input_datetime.cycle5
    - input_datetime.cycle6
  condition:
  - condition: state
    entity_id: sensor.watering_days
    state: '0'
  - condition: state
    entity_id: input_boolean.run_irrigation
    state: 'on'
  action:
  - repeat:
      count: '11'
      sequence:
      - variables:
          zone: switch.zone_{{ repeat.index }}
      - service: switch.turn_on
        target:
          entity_id: '{{ zone }}'
      - delay:
          minutes: '{{ states(''input_number.zone'' ~ repeat.index) | int }}'
      - service: switch.turn_off
        target:
          entity_id: '{{ zone }}'
  mode: single

Here is my dashboard:

My zones and other variables are set up like this:

sensor:
# Total cycle watering time
  - platform: template
    sensors:
      total_watering_time:
        friendly_name: "Total Cycle Time"
        unique_id: "Total Cycle Time in Minutes"
        unit_of_measurement: "min"
        value_template: >-
          {% set z1 = states('input_number.zone1') | float %}
          {% set z2 = states('input_number.zone2') | float %}
          {% set z3 = states('input_number.zone3') | float %}
          {% set z4 = states('input_number.zone4') | float %}
          {% set z5 = states('input_number.zone5') | float %}
          {% set z6 = states('input_number.zone6') | float %}
          {% set z7 = states('input_number.zone7') | float %}
          {% set z8 = states('input_number.zone8') | float %}
          {% set z9 = states('input_number.zone9') | float %}
          {% set z10 = states('input_number.zone10') | float %}
          {% set z11 = states('input_number.zone11') | float %}
          {{ (z1 + z2 + z3 + z4 + z5 + z6 + z7 +z8 + z9 + z10 + z11) }} 
#--------------------------------------------------------------------------------------------
#Input Variables
input_datetime:

# Set up the input variables for irrigation cycle start times
  cycle1:
    name: Cycle 1
    has_date: false
    has_time: true
  cycle2:
    name: Cycle 2
    has_date: false
    has_time: true
  cycle3:
    name: Cycle 3
    has_date: false
    has_time: true
  cycle4:
    name: Cycle 4
    has_date: false
    has_time: true
  cycle5:
    name: Cycle 5
    has_date: false
    has_time: true
  cycle6:
    name: Cycle 6
    has_date: false
    has_time: true
  irrigation_valve_on_morning:
    name: Irrigation Valve on Morning
    has_date: false
    has_time: true
  irrigation_valve_on_evening:
    name: Irrigation Valve on Evening
    has_date: false
    has_time: true
  irrigation_valve_off_morning:
    name: Irrigation Valve off Morning
    has_date: false
    has_time: true
  irrigation_valve_off_evening:
    name: Irrigation Valve off Evening
    has_date: false
    has_time: true
input_number:
# Irrigation system zone run time
  zone1:
    name: Zone1
    icon: mdi:clock-start
#   initial: 4
    min: 1
    max: 15
    step: 1
    mode: slider
    unit_of_measurement: min
  zone2:
    name: Zone2
    icon: mdi:clock-start
#   initial: 4
    min: 1
    max: 15
    step: 1
    mode: slider
    unit_of_measurement: min
  zone3:
    name: Zone3
    icon: mdi:clock-start
#   initial: 5
    min: 1
    max: 15
    step: 1
    mode: slider
    unit_of_measurement: min
  zone4:
    name: Zone4
    icon: mdi:clock-start
#   initial: 6
    min: 1
    max: 15
    step: 1
    mode: slider
    unit_of_measurement: min
  zone5:
    name: Zone5
    icon: mdi:clock-start
#   initial: 5
    min: 1
    max: 15
    step: 1
    mode: slider
    unit_of_measurement: min
  zone6:
    name: Zone6
    icon: mdi:clock-start
#   initial: 5
    min: 1
    max: 15
    step: 1
    mode: slider
    unit_of_measurement: min
  zone7:
    name: Zone7
    icon: mdi:clock-start
#   initial: 8
    min: 1
    max: 15
    step: 1
    mode: slider
    unit_of_measurement: min
  zone8:
    name: Zone8
    icon: mdi:clock-start
#   initial: 8
    min: 1
    max: 15
    step: 1
    mode: slider
    unit_of_measurement: min
  zone9:
    name: Zone9
    icon: mdi:clock-start
#   initial: 6
    min: 1
    max: 15
    step: 1
    mode: slider
    unit_of_measurement: min
  zone10:
    name: Zone10
    icon: mdi:clock-start
#   initial: 5
    min: 1
    max: 15
    step: 1
    mode: slider
    unit_of_measurement: min
  zone11:
    name: Zone11
    icon: mdi:clock-start
#   initial: 10
    min: 1
    max: 15
    step: 1
    mode: slider
    unit_of_measurement: min
# Boolean to control irrigation
input_boolean:
  run_irrigation:
   name: irrigation control
#  initial: true  - keep value through reboot so that is why commented out
    icon: mdi:water-pump 

I have a rainmachine that is controlled locally and the switches are named zone[1…11].

I use garbage collection to determine watering days S, T, Th every week between April and October. I also have a shutoff valve that I control with a separate automation.

- id: 'yyyyyyyyyyyyyy'
  alias: 'Water Lawn - Turn on/off main water '
  trigger:
  - platform: time
    at: input_datetime.irrigation_valve_on_morning
  - platform: time
    at: input_datetime.irrigation_valve_on_evening
  - platform: time
    at: input_datetime.irrigation_valve_off_morning
  - platform: time
    at: input_datetime.irrigation_valve_off_evening
  condition:
  - condition: state
    entity_id: sensor.watering_days
    state: '0'
  - condition: state
    entity_id: input_boolean.run_irrigation
    state: 'on'
  action:
  - choose:
    - conditions:
      - condition: time
        after: input_datetime.irrigation_valve_on_morning
        before: input_datetime.irrigation_valve_off_morning
      sequence:
      - service: switch.turn_on
        target:
          entity_id: switch.irrigation_current_value
    - conditions:
      - condition: time
        after: input_datetime.irrigation_valve_on_evening
        before: input_datetime.irrigation_valve_off_evening
      sequence:
      - service: switch.turn_on
        target:
          entity_id: switch.irrigation_current_value
    - conditions:
      - condition: time
        after: input_datetime.irrigation_valve_off_morning
        before: input_datetime.irrigation_valve_on_evening
      sequence:
      - service: switch.turn_off
        target:
          entity_id: switch.irrigation_current_value
    - conditions:
      - condition: time
        after: input_datetime.irrigation_valve_off_evening
      sequence:
      - service: switch.turn_off
        target:
          entity_id: switch.irrigation_current_value
    default:
    - delay:
        hours: 0
        minutes: 0
        seconds: 0
        milliseconds: 10
  mode: single

I do not have much trouble keeping my system alive so the reboots are minimized.

1 Like