Trigger a sequence of automations

Hi All,

I have 2 integrations up and running (a Tuya automated watering/irrigation system with multiple zones, and a Ecowitt system to measure soli moisture for each of those zones). I’m hoping to get these systems running together to control the automated irrigation of 6 separate zones on my property. So, if the soil moisture is below a target level, the irrigation system will be triggered.

I have a series of automations created to check the moisture level against a helper value, and if the moisture is lower, to run the irrigation for 15 minutes and turn it off. Those automations are working well.

Next challenge is to get those automations to run in sequence. So we check each zone and if it needs water we give it water, and then move on to the next zone.

Lastly, we want to get this cycle to happen once a day, just a bit before sunrise.

So - current idea was to set up an automation > to trigger a script > to trigger the zone automations in sequence.

But when I manually run the script I get the following error - “Watering cycle turned off triggered by state of Watering cycle turned on” which to me says that the script turned itself off. Which is weird because there aren’t any conditions in the script itself.

Example zone automation and the script are below.

Automation for Zone 1

alias: Water Zone 1
description: ""
trigger: []
condition:
  - condition: state
    entity_id: sensor.sensor_zone_one_below_threshold
    state: "true"
action:
  - service: notify.notify
    data:
      title: Water Zone 1 Turned On
  - type: turn_on
    device_id: 2b389bd614de6507c00b8b1190b4c27e
    entity_id: switch.wx8_102_Zone1
    domain: switch
  - delay:
      hours: 0
      minutes: 15
      seconds: 0
      milliseconds: 0
  - type: turn_off
    device_id: 2b389bd614de6507c00b8b1190b4c27e
    entity_id: switch.wx8_102_Zone1
    domain: switch
mode: single

Script to loop through a series of zone automations

alias: Watering cycle
sequence:
  - service: automation.trigger
    data: {}
    target:
      entity_id: automation.water_zone_1
  - service: automation.trigger
    data: {}
    target:
      entity_id: automation.water_zone_2
  - service: automation.trigger
    data: {}
    target:
      entity_id: automation.water_zone_3
  - service: automation.trigger
    data: {}
    target:
      entity_id: automation.water_zone_4
  - service: automation.trigger
    data: {}
    target:
      entity_id: automation.water_zone_5
  - service: automation.trigger
    data: {}
    target:
      entity_id: automation.water_zone_6
mode: single
icon: mdi:refresh

An automation without a trigger is no longer an automation, it’s a bastardized script.

I suggest you convert them to proper scripts.

That triggers an automation without executing the automation’s trigger. In this case it’s a moot point because your ‘automations’ have no trigger. However, the automation.trigger service call you created also won’t execute the automation’s condition (there’s a separate option for indicating if the condition should be executed as well).

tl;dr
Change everything to scripts and it will not only work properly but adhere to Home Assistant’s common practices (triggerless automations are not).


FWIW, it’s possible to consolidate it into a single script that employs a repeat to iterate through all irrigation zones (choosing to water them or not based on their respective sensor value).

2 Likes

The doc states indeed to trigger on anything so you have to get the payload and use that further.
Here is an example
Trigger an automation using type MQTT - Configuration - Home Assistant Community (home-assistant.io)

I ended up combining all the conditions into one script that just goes through the zones in sequence. Triggered via an automation an hour before sun rise. Worked perfectly. FWIW here is the script and automation.

Script

alias: Watering cycle (v2)
sequence:
  - if:
      - condition: template
        value_template: "{{ states.sensor.zone_1_below_threshold.state }}"
    then:
      - service: switch.turn_on
        data: {}
        target:
          entity_id: switch.wx8_102_zone_1_
      - delay:
          hours: 0
          minutes: 15
          seconds: 0
          milliseconds: 0
      - service: switch.turn_off
        data: {}
        target:
          entity_id: switch.wx8_102_zone_1_
    alias: Zone 1
  - if:
      - condition: template
        value_template: "{{ states.sensor.sensor_zone_2_below_threshold.state }}"
    then:
      - service: switch.turn_on
        data: {}
        target:
          entity_id: switch.wx8_103_zone_2
      - delay:
          hours: 0
          minutes: 15
          seconds: 0
          milliseconds: 0
      - service: switch.turn_off
        data: {}
        target:
          entity_id: switch.wx8_103_zone_2
    alias: Zone 2
  - if:
      - condition: template
        value_template: "{{ states.sensor.sensor_zone_3_below_threshold.state }}"
    then:
      - service: switch.turn_on
        data: {}
        target:
          entity_id: switch.wx8_106_zone_3
      - delay:
          hours: 0
          minutes: 15
          seconds: 0
          milliseconds: 0
      - service: switch.turn_off
        data: {}
        target:
          entity_id: switch.wx8_106_zone_3
    alias: Zone 3
  - if:
      - condition: template
        value_template: "{{ states.sensor.sensor_zone_4_below_threshold.state }}"
    then:
      - service: switch.turn_on
        data: {}
        target:
          entity_id:
            - switch.wx8_105_zone_4
      - delay:
          hours: 0
          minutes: 15
          seconds: 0
          milliseconds: 0
      - service: switch.turn_off
        data: {}
        target:
          entity_id:
            - switch.wx8_105_zone_4
    alias: Zone 4
  - if:
      - condition: template
        value_template: "{{ states.sensor.sensor_zone_5_below_threshold.state }}"
    then:
      - service: switch.turn_on
        data: {}
        target:
          entity_id:
            - switch.wx8_107_zone_5
      - delay:
          hours: 0
          minutes: 15
          seconds: 0
          milliseconds: 0
      - service: switch.turn_off
        data: {}
        target:
          entity_id: switch.wx8_107_zone_5
    alias: Zone 5
  - if:
      - condition: template
        value_template: "{{ states.sensor.sensor_zone_6_below_threshold.state }}"
    then:
      - service: switch.turn_on
        data: {}
        target:
          entity_id: switch.wx8_104_zone_6
      - delay:
          hours: 0
          minutes: 15
          seconds: 0
          milliseconds: 0
      - service: switch.turn_off
        data: {}
        target:
          entity_id: switch.wx8_104_zone_6
    alias: Zone 6
mode: single
icon: mdi:refresh

Automation

alias: Watering trigger
description: ""
trigger:
  - platform: sun
    event: sunrise
    offset: "-1"
condition: []
action:
  - service: script.turn_on
    data: {}
    target:
      entity_id: script.watering_cycle_v2
mode: single

Here’s a more concise version employing repeat for_each.

alias: Watering cycle (v3)
sequence:
  - repeat:
      for_each:
        - snsr: sensor.zone_1_below_threshold
          swi: switch.wx8_102_zone_1_
        - snsr: sensor.zone_2_below_threshold
          swi: switch.wx8_103_zone_2
        - snsr: sensor.zone_3_below_threshold
          swi: switch.wx8_106_zone_3
        - snsr: sensor.zone_4_below_threshold
          swi: switch.wx8_105_zone_4
        - snsr: sensor.zone_5_below_threshold
          swi: switch.wx8_107_zone_5
        - snsr: sensor.zone_6_below_threshold
          swi: switch.wx8_104_zone_6
      sequence:
        - condition: "{{ states(repeat.item.snsr) }}"
        - service: switch.turn_on
          target:
            entity_id: "{{ repeat.item.swi }}"
        - delay: '00:15:00'
        - service: switch.turn_off
          target:
            entity_id: "{{ repeat.item.swi }}"
mode: single
icon: mdi:refresh

I used the switch names exactly as shown in your example. I don’t know if the last underscore character in switch.wx8_102_zone_1_ is correct or a typo. Let me know either way.

1 Like