Trouble with triggering automation

Hi, I am struggling to get my automation work. Added yaml should be self-explanatory.
Mode is restart so it should - how I understand it run. Obviously it does not. The triggers seem to OK because when I run it manually it does what it should but then stops. So it starts watering but never stops until I run it manually.
thanks

alias: watering
description: Controls watering based on soil moisture level and sunlight.
triggers:
  - entity_id:
      - sun.sun
    to: above_horizon
    trigger: state
  - value_template: >-
      {{ states('sensor.esphome_web_80b8d4_humid')|float <
      states('input_number.humid')|float }}
    for:
      minutes: 15
    trigger: template
  - trigger: homeassistant
    event: start
conditions:
  - condition: state
    entity_id: sun.sun
    state: above_horizon
actions:
  - choose:
      - conditions:
          - condition: template
            value_template: >-
              {{ states('sensor.esphome_web_80b8d4_humid')|float <
              states('input_number.humid')|float }}
        sequence:
          - target:
              entity_id: switch.watering
            action: switch.turn_on
            data: {}
      - conditions:
          - condition: template
            value_template: >-
              {{ states('sensor.esphome_web_80b8d4_humid')|float >
              states('input_number.humid')|float + 5 }}
        sequence:
          - action: switch.turn_off
            target:
              entity_id: switch.watering
            data: {}
mode: restart

When you “run it manually” it bypasses the triggers and conditions blocks… it just runs the actions.

Troubleshooting Automations

Hello catdogmaus,

The only times that can possibly shut off is after the sunrise trigger or the home assistant restart trigger. Those are the only 2 that the humidity might be = or > than the input number setting so that your conditions could possibly trigger the off.

You need another trigger like this possibly…

You need to add a trigger indicating when the switch should be turned off.

Try this version.

alias: watering
description: Controls watering based on soil moisture level and sunlight.
triggers:
  - trigger: state
    entity_id: sun.sun
    to: above_horizon
    variables:
      mode: >
        {{ iif(states('sensor.esphome_web_80b8d4_humid')|float(0) >
               states('input_number.humid')|float(0), 'on', 'off')
  - trigger: homeassistant
    event: start
    variables:
      mode: >
        {{ iif(states('sensor.esphome_web_80b8d4_humid')|float(0) >
               states('input_number.humid')|float(0), 'on', 'off')
  - trigger: numeric_state
    entity_id: sensor.esphome_web_80b8d4_humid
    below: input_number.humid
    for:
      minutes: 15
    variables:
      mode: 'on'
  - trigger: template
    value_template: >-
      {{ states('sensor.esphome_web_80b8d4_humid')|float(0) >
         states('input_number.humid')|float(0) + 5 }}
    variables:
      mode: 'off'
conditions:
  - condition: state
    entity_id: sun.sun
    state: above_horizon
actions:
  - action: switch.turn_{{ mode }}
    target:
      entity_id: switch.watering
mode: single