Automation Trigger At Most Once Per Hour, Skip if Manual

I have been working on this one for several days and am stumped at the moment…

What I am trying to do:
Turn on the pond filter for 20 minutes after the weather changes from rain to anything else, if the sun is above the horizon, if it is above 45F, and if the filter’s switch has been off for the last hour. If it rained and stopped multiple times after not having been on over the previous 60 minutes, it should only trigger once. This keeps the filter from running nonstop if popup storms rapidly come and go.

What it currently does:
Turn on the pond filter for 20 minutes after the weather changes from rain to anything else, if the sun is above the horizon, if it is above 45F. It will run multiple times per hour though if there are popup storms. It also seems to turn on once an hour ALL day until the sun goes down. This is happening because there are multiple triggers. If I try to move the 1 hour time trigger to the condition using

- condition: state
  entity_id:
    - switch.waterfallpump
  to: 'off'
  for:
    hours: 1

HASS will complain saying that the dictionary of null is not valid for the state condition. The current running YAML is below.

##########################################################
#  Turn On Pond Filter After Rain Stops
##########################################################
- alias: "Clean pond after rain"
  trigger:
    - platform: state
      entity_id:
        - sensor.dark_sky_icon
      from: 'rain'
    - platform: state
      entity_id:
        - switch.waterfallpump
      to: 'off'
      for:
        hours: 1
  condition:
    condition: and
    conditions:
      - condition: state
        entity_id: sun.sun
        state: 'above_horizon'
      - condition: numeric_state
        entity_id: sensor.dark_sky_temperature
        above: 45
  action:
    - service: homeassistant.turn_on
      entity_id:
        - input_boolean.automation_triggered_pump
        - switch.waterfallpump
 
##########################################################
#  Turn Off Pond Filter After Cleaning Cycle
##########################################################
- alias: 'Pond cleaning complete'
  trigger:
    - platform: state
      entity_id:
        - input_boolean.automation_triggered_pump
      to: 'on'
      for:
        minutes: 20
  action:
    - service: homeassistant.turn_off
      entity_id:
        - input_boolean.automation_triggered_pump
        - switch.waterfallpump

I have also been trying to figure out how to get automations to not run if something was triggered by a user and not by an automation. IE: If I turn on the pond filter by hand so I can enjoy the waterfall and it begins raining, it will automatically turn off 20 minutes after rain stops. I would like the automation to “pause” until the state returns to the default state, if that is even possible. This is relevant for living room lamps too as they kick on during storms and off after the fact. If I want it on though that environment triggered automation is a pain.

Add this to your conditions:

    - condition: template
      value_template: '{{ (as_timestamp(now()) - as_timestamp(states.automation.clean_pond_after_rain.attributes.last_triggered | default(0)) | int > 3600)}}'

This will ensure it does not trigger again if triggered in the last hour (3600 sec)

5 Likes

Alternatively you could create a history statistics sensor that measures how many times the pump has been switched on in the last 60 minutes. Add a condition to the automation that tests (number of times switched on = 0).

1 Like

Thank you so much! That was exactly what I needed. I need to work on my template making skills, they are still lagging behind the rest of them rather significantly.

Sometimes is literally faster to google it, find the great guy who basically served it to you on a silver dish and copy&paste.
Thanks, next one’s on me.

1 Like

The only thing I would change from that is to use state_attr('automation.clean_pond_after_rain', 'last_triggered')
Instead of states…attributes…
They do the same thing but state_attr should not cause errors in the logs.

Thanks, though if you really want to copy paste, use this as it requires no modiffications :wink:

    - condition: template
      value_template: '{{ (as_timestamp(now()) - as_timestamp(this.last_triggered) | int > 30)}}'

(You’ll need HA 2021.8 or higher)

3 Likes

Witch is indeed what I’ve done haha. Great minds think alike! :rofl:

{{ (as_timestamp(now()) - as_timestamp(state_attr('automation.warning_road_frozen','last_triggered') | default(0)) | int > 3600*3) }}

1 Like

I realize this is an old (but super helpful) thread, as_timestamp behavior seems to have changed meanwhile. It expects a default value now so the updated version would be:

'{{ (as_timestamp(now()) - as_timestamp(state_attr('automation.warning_road_frozen','last_triggered'), 0) > 3600)}}'

Also, the | int is redundant (?)
And
as_timestamp(this.last_triggered, 0) didn’t work for me for some reason

1 Like