Duration automation

Hello everyone, help with the config. I didn’t find an answer on the net.

This is an action tab from automation. It shows that when the humidity drops, the switch turns on.

action:
- service_template: >-
{% set low = (states('input_number.input_number_on_watering') | float) %}
{% set humid = states('sensor.test_humid') | float %}
{% if humid < low %}
switch.turn_on
{% endif %}
entity_id: 
- switch.switch_1

I need it to turn off after 10 seconds of this switch. Please tell me how to spell it correctly. For clarity, I’ll throw off a non-working example, it shows what I need.

action:
- service_template: >-
{% set low = (states('input_number.input_number_on_watering') | float) %}
{% set humid = states('sensor.test_humid') | float %}
{% if humid < low %}
switch.turn_on
{% elif states("switch.switch_1") == 'on' > 10 seconds %}
switch.turn_off
{% endif %}
entity_id: 
- switch.switch_1
1 Like
action:
  - service: >
      {% set low = (states('input_number.input_number_on_watering') | float) %}
      {% set humid = states('sensor.test_humid') | float %}
      {% if humid < low %}
        switch.turn_on
      {% endif %}
    entity_id: switch.switch_1
  - delay:
      seconds: 10
  - service: switch.turn_off
    entity_id:  switch.switch_1

Just an FYI, “service_template” is deprecated (so is “data_template”) so just use “service” (“data”) instead.

Thanks It works. But is it possible to register this delay inside the service? Or do without a delay and do as I showed in the example?

i guess you technically could do it that way but it depends on the trigger (which you didn’t provide) and how the conditions inside the service template change between those different triggers.

Since the info you provided was so limited I just based it off of that.

But to be more clear…

the way you have it (ignoring anything outside of the template) it won’t do what you want.

it seems (and I could be wrong) that you expect the “elif” to wait until the switch has been on for 10 seconds after it was turned on by the switch.turn_on service in the “if” portion but that’s not the way it works.

the template evaluates the “if” and if true executes that service, otherwise it checks the “elif” and true executes that service. If neither are true the automation aborts and does nothing further even if there are additional action steps after that.

Once the “if” is true and the switch.turn_on service is called the sequence steps move on to the next set of actions in the automation and the “elif” is never evaluated until the next trigger occurs.

- alias: auto_watering
    trigger:
    - platform: homeassistant
      event: start
    - platform: state
      entity_id: 
      - sensor.test_humid
action:
- service_template: >-
{% set low = (states('input_number.input_number_on_watering') | float) %}
{% set humid = states('sensor.test_humid') | float %}
{% if humid < low %}
switch.turn_on
{% endif %}
entity_id: 
- switch.switch_1

This is what automation looks like.

using only those triggers the way I provided is the only way it can be done. And TBH, it’s the easiest.

Understood you. Thanks for the support. Helped a lot. Maybe you know the answer to another one of my problems?

- alias: auto_watering
  trigger:
    - platform: homeassistant
      event: start
    - platform: state
      entity_id: sensor.test_humid
  condition:
    - condition: template
      value_template: >
        {% set low = states('input_number.input_number_on_watering') | float(0) %}
        {% set humid = states('sensor.test_humid') | float(0) %}
        {{ humid < low }}
  action:
    - service: switch.turn_on
      target:
        entity_id: switch.switch_1
- alias: auto_shutoff
  trigger:
    - platform: state
      entity_id: switch.switch_1
      to: 'on'
      for: '00:00:10'
  condition: []
  action:
    - service: switch.turn_off
      target:
        entity_id: switch.switch_1