I need this automation to run again if the switch was turned on

I noticed that the third trigger didn’t work earlier. The wind has been above 15 for the last several hours. Someone turned on a switch with the label and the inflatable didn’t turn off. As I started this post I realized it’s because the automation did trigger this morning after the 5 minutes threshold, so there was nothing to check. So I guess I’m actually looking to brainstorm how to get it to run this check again when any switches with this label are turned on.

Right now what I’m thinking is to move all of this to a script, where the current triggers become conditions. Then make a new automation that has the same triggers to run the script. And another trigger for switch.on and then if that was the trigger, check if the trigger entity has the outdoor_inflatable label on it. Is there a better/less complex way to do this that anyone can suggest?

alias: Weather turn off inflatables
description: >-
  Turn off switches labeled as outdoor inflatables whenever a weather services
  indicates rain or when the wind has been too high for 5 minutes. Only triggers
  if it is currently a holiday season.
triggers:
  - trigger: state
    entity_id:
      - weather.forecast_home
    to:
      - pouring
      - rainy
      - lightning-rainy
  - trigger: state
    entity_id:
      - weather.pirateweather
    to:
      - pouring
      - rainy
      - lightning-rainy
  - trigger: numeric_state
    entity_id:
      - sensor.pirateweather_wind_speed
    for:
      hours: 0
      minutes: 5
      seconds: 0
    above: 15
conditions:
  - condition: or
    conditions:
      - condition: state
        entity_id: binary_sensor.halloween_season
        state: "on"
      - condition: state
        entity_id: binary_sensor.christmas_season
        state: "on"
actions:
  - action: switch.turn_off
    metadata: {}
    data: {}
    target:
      label_id: outdoor_inflatable
mode: single

Quick update. I think I got it once I remembered template sensors. I added another trigger to go whenever the count of switches with that label is above 0. I added the other 3 triggers (still left as actions) to an or condition. Theoretically, if any one of the 4 triggers activate then it should always pass my conditions, like if rain is detected then the rain trigger will run, the rain condition will pass the or condition, and things should run smoothly. I’m using a similar method in other automations.

The only thing that’s proving to be a problem is that I need to figure out how to get the numeric state condition to let me use the “for length of time” portion, cause that’s not supported.

You would need to set up a Template binary sensor (either trigger-based or with a defined delay_on) since there’s nothing in the state object of the wind speed sensor that stores how long the state has been above or below a certain value.

What makes it work for triggers?

Triggers create a special listener on the event bus that compares the state value to your defined threshold value then essentially waits to see if it stays true for the defined time.

Conditions only check the state in that instance. Since a plain State condition is checking for an exact match, it is possible to check the duration. That’s because the last_changed property of the state object can be checked against the current time, in that instant, to see if the value has been stable for the defined duration.

But for a Numeric State condition that method wouldn’t work. The last_changed property only tells us when the state changed. But the state could have changed many times and still be “above” or “below”. That comparison isn’t stored in the state object.

For some sensors , in theory (I’ve never tried it), it might be possible to use the recorder.get_statistics action, then parse the returned values with templates to see if all the values over the desired time span were in the correct range…

1 Like

That first bit feels a bit over my head, but your second paragraph makes sense. Thanks for clarifying, I’ll get that helper set up and hopefully will be good to go.

Are trigger-based and delay_on only available when adding in yaml? I don’t see anything on the gui editor that would let me put that in.