Help to create an automation to turn on a device for 5 mins every hour

Completely new. I’m trying to set up an automation For a pump for my Greenhouse, which works fine in Home Assistant. Numerous failed attempts to automate my first automation, I’m hoping someone can help.

I want the pump to run five minutes every hour if over 10°C.

10 minutes if over 20°C

15 minutes is over 30°C

20 minutes over 40°C

(I have a temp and humidity sensor fully working in HA in the greenhouse)

I’d like this automation to only be available during daylight hours

And it definitely has to turn off ideally with some extra failsafes in place otherwise i’ll flood the greenhouse.

Any kind helpers? If it can be done in the normal UI way rather than code which, I have not done yet.

These automations should do it:

On automation:

triggers:
  - trigger: time_pattern # at 5 seconds past the hour, every hour
    hours: "*"
    minutes: 0
    seconds: 5
conditions:
  - condition: sun # only during daylight, you can add offsets if needed.
    after: sunrise
    before: sunset
  - condition: numeric_state # only if above 10°
    entity_id: sensor.greenhouse_temperature
    above: 10
actions:
  - action: input_number.set_value # store the minutes the timer should be on
    target:
      entity_id: input_number.watering_on_time # you have to create this helper first
    data:
      value: >
        {% set temp = states('sensor.greenhouse_temperature')|float(0) %}
        {% if temp > 40 %}
          20
        {% elif temp > 30 %}
          15
        {% elif temp > 20 %}
          10
        {% else %}
          5
        {% endif %}
  - action: switch.turn_on
    target:
      entity_id: switch.greenhouse_irrigartion

Off automation:

triggers:
  - trigger: state
    entity_id: switch.greenhouse_irrigartion
    to: 'on' 
    for:
      minutes: "{{ states('input_number.watering_on_time') }}"
actions:
  - action: switch.turn_off
    target:
      entity_id: switch.greenhouse_irrigartion 

And a failsafe automation:

triggers:
  - trigger: state
    entity_id: switch.greenhouse_irrigartion
    to: 'on' 
    for:
      minutes: 21
actions:
  - action: switch.turn_off
    target:
      entity_id: switch.greenhouse_irrigartion 
  - action: notify.your_phone
    data:
      message: "The irrigation ran over time. Please check the system."
1 Like

Thank you so much. When I posted the question yesterday, I had no idea I would get any answer but to receive an answer the same evening is great and I am really appreciative

I haven’t quite got it to work yet, but I’m working on it now and I will get back to you.

Seems not to like endif? Almost had it. It wouldn’t let me save it. :frowning:

There should only be 1 if, any clauses afterward should be elif

      value: >
        {% set temp = states('sensor.greenhouse_temperature')|float(0) %}
        {% if temp > 40 %}
          20
        {% elif temp > 30 %}
          15
        {% elif temp > 20 %}
          10
        {% else %}
          5
        {% endif %}
1 Like

Yes, it can be.

Not ideal if your HA restarts often, as this gets terminated while waiting in that case.

An alternative solution is to have one automation that turns the pump on (if the temp is above 10), then an automation that runs at 10 minutes after the hour and turns the pump off if the temp is below 30, a third at 15 minutes that turns it off if the temp is below 40, and so on. That’s more resilient to restarts, especially because all the later ones for higher temps will also turn the pump off, so even if the right one is missed, 5 minutes later the next one will do the job. (Hint: Add an extra unconditional one 5 minutes after the last.)

Those also can be combined into one automation with 5 timed triggers. For that, you need to assign names to the triggers (“…”-menu on the trigger), then you can have a CHOOSE block based on which of the triggers fired. However, the only advantage is to have it all in one automation, which also is a negative as it becomes harder to read. I usually just put mine into separate automations and then group them in their own category.

As well as my if/elif mistake (now fixed above) make sure you indent it as shown. This is important in YAML.

Could you please help me again? I’m not doing great. I have created everything you laid out but seem I have a glitch.

Thank you very much for taking the time to help me. I really appreciate it. I am still working on the first solution provided. As I’m still working on that I tried your solution but, when I select ‘fan’ and try to select something there is nothing there to select. lol. I’m still learning I guess.

alias: Greenhouse Pump Automation
description: ""
triggers:
  - hours: "*"
    minutes: "01"
    trigger: time_pattern
conditions:
  - condition: sun
    after: sunrise
    before: sunset
actions:
  - choose:
      - conditions:
          - condition: numeric_state
            entity_id: sensor.greenhouse_temperature
            above: 40
        sequence:
          - target:
              entity_id: switch.pump
            action: switch.turn_on
            data: {}
          - delay:
              minutes: 20
          - target:
              entity_id: switch.pump
            action: switch.turn_off
            data: {}
      - conditions:
          - condition: numeric_state
            entity_id: sensor.greenhouse_temperature
            above: 30
        sequence:
          - target:
              entity_id: switch.pump
            action: switch.turn_on
            data: {}
          - delay:
              minutes: 15
          - target:
              entity_id: switch.pump
            action: switch.turn_off
            data: {}
      - conditions:
          - condition: numeric_state
            entity_id: sensor.greenhouse_temperature
            above: 20
        sequence:
          - target:
              entity_id: switch.pump
            action: switch.turn_on
            data: {}
          - delay:
              minutes: 10
          - target:
              entity_id: switch.pump
            action: switch.turn_off
            data: {}
      - conditions:
          - condition: numeric_state
            entity_id: sensor.greenhouse_temperature
            above: 10
        sequence:
          - target:
              entity_id: switch.pump
            action: switch.turn_on
            data: {}
          - delay:
              minutes: 5
          - target:
              entity_id: switch.pump
            action: switch.turn_off
            data: {}
  - target:
      entity_id: switch.pump
    action: switch.turn_off
    data: {}

Try it like this. I tried it and it worked! You’d just have to replace it with the appropriate switch and sensor

Do not try it like that.

Delaying for lengthy periods in an automation is a sure fire way to flood your greenhouse when the automation is interrupted by a reload or restart.

I have:

    data:
      value: >

You have:

    data: >-  # remoove the ">-"
      value:> # needs a space between : and >

Please use copy and paste to prevent these sorts of transcription issues.

Many thanks again for helping me. I really appreciate it. I took on board what you said and made the necessary corrections. But it seems to be something about a visual editor and YAML? At this days stage of my journey, I have no idea what it’s all about. lol.

metadata:{} is an issue. Delete it

As shown in multiple posts, when using a multi-line quote, the template needs to start on the next line and be indented…

The following is invalid:

value: > {% ....

It should be:

value: >
  {% ....

Hi Tom. Just wondering if you could possibly help me again. If you look at my screenshots I think the problem is where I’m putting the code? For some reason, the on routine isn’t working. Definitely something small I’m not doing right But my complete lack of experience is preventing me from knowing what’s wrong. Many thanks in advance of any help.

Copy paste the full yaml instead of posting images

I’m putting the ‘code’ in the wrong place I’m sure.

It says visual editor not supported or, something.

I have no idea what I’m doing really. Although I have managed to do automations for lights, which were much more simple. Any help from anybody would be so much appreciated, but please treat me like an idiot because I didn’t seem to have the same basic understanding as everyone else. I am older guy in my 50’s :frowning:

triggers:

  • trigger: time_pattern # at 5 seconds past the hour, every hour
    hours: “*”
    minutes: 0
    seconds: 5
    conditions:
  • condition: sun # only during daylight, you can add offsets if needed.
    after: sunrise
    before: sunset
  • condition: numeric_state # only if above 10°
    entity_id: sensor.greenhouse_temperature
    above: 10
    actions:
  • action: input_number.set_value # store the minutes the timer should be on
    target:
    entity_id: input_number.watering_on_time # you have to create this helper first
    data:
    value: >
    {% set temp = states(‘sensor.greenhouse_temperature’)|float(0) %}
    {% if temp > 40 %}
    20
    {% elif temp > 30 %}
    15
    {% elif temp > 20 %}
    10
    {% else %}
    5
    {% endif %}
  • action: switch.turn_on
    target:
    entity_id: switch.greenhouse_irrigartion