Automation condition change Problem - Float switch

HI Guys, I’m struggling with an automation I have setup for greenhouse irrigation. I have a small plastic box with a float switch in it which also has a irrigation drip per which fills it when the irrigation starts. The idea is that once the box fills the float switch will trigger the irrigation pum to switch off and as the water evaporated from the box the float switch turns on and this is a condition for turning the greenhouse irrigation on again.

I have the following automation, which will trigger the irrigation to turn on but when the float switch turns off it is not triggering the pump to turn off.


alias: Greenhouse Irrigation
description: ''
trigger:
  - platform: time
    at: '18:00:00'
condition:
  - condition: state
    entity_id: binary_sensor.float_switch
    state: 'on'
action:
  - service: switch.turn_on
    data: {}
    target:
      entity_id: switch.tasmota2
  - delay:
      hours: 0
      minutes: 0
      seconds: 20
      milliseconds: 0
  - condition: state
    state: 'on'
    entity_id: switch.tasmota2
  - service: switch.turn_on
    data: {}
    target:
      entity_id: switch.tasmota
  - service: notify.notify
    data:
      message: Greenhouse Irrigation has started
      title: Greenhouse Irrigation
  - condition: state
    entity_id: binary_sensor.float_switch
    state: 'off'
    for:
      hours: 0
      minutes: 0
      seconds: 0
  - service: switch.turn_off
    data: {}
    target:
      entity_id: switch.tasmota
  - delay:
      hours: 0
      minutes: 0
      seconds: 10
      milliseconds: 0
  - service: switch.turn_off
    data: {}
    target:
      entity_id: switch.tasmota2
  - service: notify.notify
    data:
      message: Greenhouse Irrigation has finished
      title: Greenhouse Irrigation
mode: single


I’m not quite sure why the above automation is not working as I expected?? I know that the float switch is changing state as I can see this in the history log. Today the pump ran and flooded my tomato’s :frowning: :unamused:

Any help would be much appreciated

This State Condition within the automation’s action doesn’t wait around until the switch’s state becomes off.

  - condition: state
    entity_id: binary_sensor.float_switch
    state: 'off'

When the automation reaches that State Condition and determines the switch’s state is currently on it ends the automation immediately. That’s how a condition within an action behaves.

If you want it to wait until the switch is off, consider using a wait_template.

Be advised that the way you constructed the automation makes it vulnerable to being interrupted by a restart or Reload Automations. If you are interested, I can explain how to make it more fault-tolerant.

thanks @123 this shows my limited knowledge of automations. it would be great if you could explain about the wait template and fault tolerance too.

I have now updated the automation withe a wait template

alias: Greenhouse Irrigation
description: ''
trigger:
  - platform: time
    at: '18:00:00'
condition:
  - condition: state
    entity_id: binary_sensor.float_switch
    state: 'on'
action:
  - service: switch.turn_on
    data: {}
    target:
      entity_id: switch.tasmota2
  - delay:
      hours: 0
      minutes: 0
      seconds: 20
      milliseconds: 0
  - condition: state
    state: 'on'
    entity_id: switch.tasmota2
  - service: switch.turn_on
    data: {}
    target:
      entity_id: switch.tasmota
  - service: notify.notify
    data:
      message: Greenhouse Irrigation has started
      title: Greenhouse Irrigation
  - wait_template: '  wait_template: "{{ is_state(''binary_sensor.float_switch'', ''off'') }}"'
    continue_on_timeout: false
  - service: switch.turn_off
    data: {}
    target:
      entity_id: switch.tasmota
  - delay:
      hours: 0
      minutes: 0
      seconds: 10
      milliseconds: 0
  - service: switch.turn_off
    data: {}
    target:
      entity_id: switch.tasmota2
  - service: notify.notify
    data:
      message: Greenhouse Irrigation has finished
      title: Greenhouse Irrigation
mode: single

Not sure if it’s correct??

Your wait_template contains the word wait_template twice. Remove the second one and correct the quotes like this:

  - wait_template: '{{ is_state(''binary_sensor.float_switch'', ''off'') }}'

Thanks @123 I’ve updated the wait template and added a timeout, which I assume with allow the automation to continue after the timeout even if the wait template condition is not met? is that correct?

Also if you could share how to make this automation more fault tolerant that would be great, thanks

You should specify a timeout value. The wait_template waits for the float switch to turn off for no longer than the duration of the timeout value (and then either continues or terminates, depending on the value of continue_on_timeout).