Automation Condtions

I am trying to add conditions to a automation action and am wondering whether this should work or not (obviously depending on the scripts being right etc). Thanks in advance!

  • alias: Run Cycle 2
    initial_state: ‘on’
    trigger:

    • platform: time_pattern
      minutes: 0
      seconds: 0

    condition:

    • condition: template
      value_template: >
      {{ now().strftime(’%Y-%m-%d %H:%M:00’) == states(‘input_datetime.cycle2_next_run_time’) }}
    • condition: state
      entity_id: input_boolean.cycle2_enable
      state: ‘on’
      action:
    • condition: template
      value_template: “{{ ‘states.sensor.sensor1’, ‘On’}}”
    • service: script.run_a_cycle
      data_template:
      cycle: 2
    • condition: template
      value_template: “{{ ‘states.sensor.sensor1’, ‘Off’}}”
    • service: script.update_next_runtime

Please format your code correctly. Point 11 here: How to help us help you - or How to ask a good question

Sorry, thought I had done that, my mistake!

 - alias: Run Cycle 2
    initial_state: 'on'
    trigger:
      - platform: time_pattern
        minutes: 0
        seconds: 0

    condition:
      - condition: template
        value_template: >
          {{ now().strftime('%Y-%m-%d %H:%M:00') == states('input_datetime.cycle2_next_run_time') }}
      - condition: state
        entity_id: input_boolean.cycle2_enable
        state: 'on'
    action:   
      - condition: template      
        value_template: "{{ 'states.sensor.sensor1', 'Dry'}}"
      - service: script.run_a_cycle
        data_template:
          cycle: 2
      - condition: template      
        value_template: "{{ 'states.sensor.sensor1', 'Too Wet'}}"
      - service: script.update_next_runtime

this won’t work:

Try instead
value_template: "{{ states('sensor.sensor1') == 'Dry'}}"
Also note that if your first condition under the action block is not met, your script/automation stops, it does not just skip the next action and keeps running.
On that basis your service: script.update_next_runtime will never run

Thanks for that. Ohh I see… I did have a play with using If /else statements but I couldn’t find a way to add in the data_template for cycle 2 bit.
Is there a way to skip the service if the condition is not met and move to the next one?

yes, just call 2 separate automations/scripts that would have the condition built-in and would run an action if the condition is true.
OR
have a service_template that checks the sensor.sensor1 value and runs an action accordingly:

I’d ideally like to use the service_template but I couldn’t see a way of including the below config.

data_template:
  cycle: 2

what exactly is cycle: 2 ? I’ve never seen this before.
If you’re wanting the script to run twice, just call it twice?

Its part of this script. It either runs cycle 1 or cycle 2 depending on which input_boolean is selected.

  - service: script.run_a_zone
    data_template:
      cycle: '{{ cycle }}'
      zone: 1

Thanks. I’m still not sure how that’s supposed to work.
Just add a condition / service_template to your script.
so 1 automation with service_template to select the script, and the script has a service_template to selec the cycle?

I think I’m going to have two automations, one for if the sensor state is ‘Dry’ and one for when its ‘Wet’.

Cheers for the help.

1 Like