Really simple automation kills me

Hello guys.

I am trying to make a really simple automation to remind me to water my plants.
I already have a light for my plants (they don’t grow because its to dark) that gets switched on at a input_number defined time and turned off after a simple delay.
Thats working really well.

I added a earth moisture sensor that measures the moisture.
And i have a motion sensor next to the plants.

I now want to do the following:

if motion and moisture < target_moisture
blink plant light for as long as motion is detected (1s on 1s off)
loop

Should be easy right?

I did start with the script to flash a light.
and added my conditions.

I did have the problem that the light would start flashing if the wifi connection gets lost. So i added a template to check for “unavailable”. -> fixed
The next problem appeared:
Sometimes the Automation / blink_basilikum / flash_loop script gets stuck.
If this happens the state of the automation and scripts stays at 1 but the light does not blink.
So i added a script_turnoff in each step to reset the scripts (not knowing why it stayed on in the first place)
Next problem: I do a light.toggle to ensure that the state of the light is the same before and after the flash cycle. This should ensure that the plant light thats turned on during the day does not get switched to a different state e.g. off after watering the plant (during the day) or stays on during the night if it gets watered.
But this does not happen, sometimes the state changes. After the watering it turns off (if it was on before watering) or it stays on (if it was off beforehand)
Thinking the script_turnoff kills the script mid cycle i added a input_boolean to ensure that the script_turnoff only gets called when the “two times toggle cycle” is finnished.

It didn’t help.

Now i am stuck with a really complicated setup with two scripts and one automations and a helper but it still does not work reliably.
I am thinking i am overcomplicating stuff. Can somebody give me a hint?
Thanks a lot!

Automation:

- id: 25e4a0dab38e4539a375ea2d3d4cdd6f
  alias: Blumenlampe Feuchtigkeiswarnung Basilikum
  trigger:
  - platform: template
    value_template: '{%if is_state(''sensor.feuchtigkeit_basilikum'', ''unavailable'')
      %} false {%elif states(''sensor.feuchtigkeit_basilikum'')|float < states(''input_number.basilikum'')|float
      and is_state(''binary_sensor.kuche'', ''on'')%} true {%else%} false {%endif%}'
  condition:
  - condition: state
    entity_id: input_boolean.toggle_basilikum
    state: 'off'
  action:
  - service: script.turn_off
    data: {}
    entity_id: script.blinkbasilikum
  - service: script.turn_on
    data: {}
    entity_id: script.blinkbasilikum
  mode: single
  max: 2

Script.blinkbasilikum

blinkbasilikum:
  alias: blinkbasilikum an
  sequence:
  - condition: and
    conditions:
    - condition: state
      entity_id: automation.blumenlampe_feuchtigkeiswarnung_basilikum_2
      state: 'on'
    - condition: template
      value_template: '{{ states(''sensor.feuchtigkeit_basilikum'')|float < states(''input_number.basilikum'')|float
        }}'
    - condition: state
      entity_id: binary_sensor.kuche
      state: 'on'
  - service: input_boolean.turn_on
    data: {}
    entity_id: input_boolean.toggle_basilikum
  - service: light.toggle
    entity_id: light.blumenlampe_basilikum
    data:
      brightness: 130
      transition: 0.5
  - delay: '1'
  - service: light.toggle
    entity_id: light.blumenlampe_basilikum
    data:
      brightness: 130
      transition: 0.5
  - service: input_boolean.turn_off
    data: {}
    entity_id: input_boolean.toggle_basilikum
  - service: script.turn_off
    data: {}
    entity_id: script.flash_loop
  - service: script.turn_on
    entity_id: script.flash_loop
  mode: single

script.Flashloop

flash_loop:
  alias: Flash loop
  sequence:
  - condition: template
    value_template: '{{ states(''sensor.feuchtigkeit_basilikum'')|float < states(''input_number.basilikum'')|float
      }}'
  - delay: '1'
  - service: script.turn_on
    data: {}
    entity_id: script.blinkbasilikum
  mode: single

Try this:

- id: 25e4a0dab38e4539a375ea2d3d4cdd6f
  alias: Blumenlampe Feuchtigkeiswarnung Basilikum
  trigger:
  - platform: template
    value_template: >
      {% if is_state(''sensor.feuchtigkeit_basilikum'', ''unavailable'') %} 
        false 
      {%elif states(''sensor.feuchtigkeit_basilikum'')|float < states(''input_number.basilikum'')|float and is_state(''binary_sensor.kuche'', ''on'')%} 
        true 
      {%else%} 
        false 
      {%endif%}
  action:
    repeat:
        sequence:
          - service: light.toggle
            entity_id: light.blumenlampe_basilikum
            data:
              brightness: 130
              transition: 0.5
          - delay:
              seconds: 1
          - service: light.toggle
            entity_id: light.blumenlampe_basilikum
            data:
              brightness: 130
              transition: 0.5
          - delay:
              seconds: 1
      until:
        - condition: state
          entity_id: binary_sensor.kuche
          state: 'off'

I couldn’t test it but I think it should work.

But that trigger template looks more complicated than necessary.

1 Like

Thanks, this worked like a charm.
I did even forget that i had that problem, therefor the late reply!
thanks

1 Like