Add a condition to a script action

I use HA to run my irrigation. I have 15 yard zones and 2 drip zones. The system has a master valve to keep the pressure off the system when not being used.

My drip system script runs the drips for X time every X hours. The yard irrigation is on a different schedule. They may run at the same time and may not. In my scripts for both the yard irrigation and drip irrigation I want the script to close the master valve when the script is done, but only if the other system (drip or yard irrigation) isn’t running. Can this be done?

Yes.

Use a condition, other system not running.

I can’t find out how to add a condition to a single action within a sequence. Can it be done with the script builder? Or does it need to be done with YAML

you can’t add it to a specific action unless the action is the last one in the script. But it sounds like that might be the case in you situation.

if so then just add the condition right before the last action (the closing of the master valve).

It might be easier to tell you how if you actually post the script tho.

Or use a choose action.

Thanks, I think I understand now. Here is the code, is this correct? The reason to run the next script is to loop the drip irrigation every X hours.

drip_run_drips:
  sequence:
  - service: switch.turn_on
    target:
      entity_id: switch.master_valve
  - service: switch.turn_on
    target:
      entity_id:
      - switch.drip_front_yard
      - switch.drip_backyard
  - delay:
      minutes: '{{ states(''input_number.drip_irrigation_timer_minutes'') | int }}'
  - service: switch.turn_off
    target:
      entity_id:
      - switch.drip_backyard
      - switch.drip_front_yard
  - service: script.turn_on
    target:
      entity_id: script.drip_loop_drip_irrigation
  - condition: state
    entity_id: script.irrigation_run_two_zones_at_once
    state: 'on'
  - service: switch.turn_off
    target:
      entity_id: switch.master_valve
  mode: single
  alias: Drip - Run Drips
  icon: mdi:water

The syntax looks OK to me.

I’m wondering if I have my logic wrong. I want the script to stop and not close the master valve if the irrigation script is “on”.

While executing a script you can add a condition to stop further execution. When a condition does not return true , the script will stop executing.

- condition: state
    entity_id: script.irrigation_run_two_zones_at_once
    state: 'on'

So, if the irrigation script is running the condition test would return “true” and the script would continue and close the valve, correct? In that case, I’d want my condition to be:

- condition: state
    entity_id: script.irrigation_run_two_zones_at_once
    state: 'off'

Is that correct?

I think so.

I’m, honestly not sure what the states are for scripts but I think you are correct in your second condition.