#simplifying 5m on/10m off time based automation

1 messages · Page 1 of 1 (latest)

bronze rover
#

if i wanted a single automation that is triggered by time(5m on 10off) but isnt susceptible to issues that the delay action has like rebooting ha, would it look something like this? and then you would just filter by on/off trigger ids?

#
trigger:
  - platform: time_pattern
    minutes: "0"
    id: "on"
  - platform: time_pattern
    minutes: "15"
    id: "on"
  - platform: time_pattern
    minutes: "30"
    id: "on"
  - platform: time_pattern
    minutes: "45"
    id: "on"
  - platform: time_pattern
    minutes: "5"
    id: "off"
  - platform: time_pattern
    minutes: "20"
    id: "off"
  - platform: time_pattern
    minutes: "35"
    id: "off"
  - platform: time_pattern
    minutes: "50"
    id: "off"
condition: []
action:
  - if:
      - condition: trigger
        id:
          - "on"
    then:
      - service: homeassistant.turn_on
        data: {}
        target:
          entity_id: switch.aquarium_lights_plug_aquarium_lights_relay
  - if:
      - condition: trigger
        id:
          - "off"
    then:
      - service: homeassistant.turn_off
        data: {}
        target:
          entity_id: switch.aquarium_lights_plug_aquarium_lights_relay
mode: single
hoary hazel
#
action:
  - service: "switch.turn_{{ trigger.id }}"
    data: {}
    target:
      entity_id: switch.aquarium_lights_plug_aquarium_lights_relay
boreal kernel
#

I think all the on triggers could be replaced by:

  - platform: time_pattern
    minutes: "/15"
    id: "on"

Guess can't do the same with off though.

hoary hazel
#

Could probably just do it with:

  - platform: state
    entity_id: switch.aquarium_lights_plug_aquarium_lights_relay
    to: "on"
    for: "00:05:00"
    id: "off"
hoary hazel
#

That's just a trigger

bronze rover
#

oh ok, so its saying once the entity has been on for 5m, trigger with the id of off.

hoary hazel
#

Yes

#
trigger:
  - platform: time_pattern
    minutes: "/15"
    id: "on"
  - platform: state
    entity_id: switch.aquarium_lights_plug_aquarium_lights_relay
    to: "on"
    for: "00:05:00"
    id: "off"
action:
  - service: "switch.turn_{{ trigger.id }}"
    data: {}
    target:
      entity_id: switch.aquarium_lights_plug_aquarium_lights_relay
bronze rover
#

so none of my automations have an importance resolution of < 1m, is there a way to harden them by checking that they are in a proper state every 1m to get around unavailability of entities or ha downtime? like once everyone is back online, check the proper state and set to what they should be if not type of thing.

hoary hazel
#

You'd need a startup trigger, and #templates-archived logic to handle whether it should be on or off

whole moon
#

There is a difference between triggering at minutes: "0", minutes: "15" etc and minutes: "/15"
I think the last one will be based on the startup time of HA

hoary hazel
#

Nah

#

You can prefix the value with a / to match whenever the value is divisible by that number

bronze rover
#

does the state trigger youre using have the same downside as a time delay being interrupted 🤔

hoary hazel
whole moon
#

Well that's what I thought first as well, but according to Petro it is based on the number of minutes from a certain base point (so HA start, or 1970-1-1 00:00)

#

So minutes: "/37" should trigger every 37 minutes then, and not at XX:00 and XX:37

boreal kernel
#

at XX:00 and XX:37

I'm pretty sure that's what you get

#
trigger:
  - platform: time_pattern
    seconds: /42
condition: []
action:
  - service: system_log.write
    data:
      message: The trigger fired
      level: warning
2023-09-13 09:55:00.503 WARNING (MainThread) [homeassistant.components.system_log.external] The trigger fired  
2023-09-13 09:55:42.502 WARNING (MainThread) [homeassistant.components.system_log.external] The trigger fired  
2023-09-13 09:56:00.502 WARNING (MainThread) [homeassistant.components.system_log.external] The trigger fired 
whole moon
#

Then it was what I thought it did and @drifting swallow misinformed me

drifting swallow
#

what

whole moon
#

Hehe, the time pattern trigger doesn't work as you explained, but it works as I templated it 😅

drifting swallow
#

not sure what you mean

#

it should trigger every x minutes from startup unless someone changed it

#

or x seconds

bronze rover
#

it is sort of odd, you would think it would only fire at 09:55:42

whole moon
#

The example above from @boreal kernel shows otherwise

drifting swallow
#

that would be just seconds: 42

whole moon
#

No it does 0 as well, as that's devisable by 42 as well

drifting swallow
#

seconds: 42 will just fire on the 42nd second

#

/42 should be every 42 seconds from startup

boreal kernel
#

does not do that

drifting swallow
#

if it's not doing that, it's a bug

boreal kernel
#

what do you think about this then:

minutes: /3
seconds: /42
drifting swallow
#

I would assume that wouldn't work

#

it's possible this all changed

#

but when I first started using HA, it was based on startup

#

I do vaguely remember something changing to use modulo

#

there are threads to get every hour and a half that was a breaking change

#

so it's possible it changed

boreal kernel
#

I guess so

drifting swallow
#

I personally avoid time_patterns as they are not useful (to me)

boreal kernel
#

and just for the record

minutes: /3
seconds: /42

Triggers at 00:00, 00:42, 03:00, 03:42, ...

bronze rover
#

the visual editor says: "Trigger every 42 seconds of every 3 minutes of every hour"

boreal kernel
#

Yes I wrote that code that emits that 😛

#

It's not easy to verbalize this hehe

bronze rover
#

i totally understand

boreal kernel
#

good luck to all you translators out there hehe

drifting swallow
#

took me a bit to track down the code

#

but it's all been unified under the event helper

#

it was completely different code back in the day. They must have redone it when it went full async

#

it's not even in the change history, meaning it was prior to the great migration

#

circa 2018

whole moon
#

The Great Migration

drifting swallow
#

Yes

#

it was a folder structure change

#

but I dont' remember what the old structure looked like

#

but it affected the way integrations were loaded

#

it basically kills the history prior to that change too

#

specifically a folder structure change for integrations

whole moon
#

Ah okay

drifting swallow
#

either wya, I can't find the original time_pattern trigger

#

I 100% remember it used modulo and didn't schedule the next trigger. Now it schedules the next trigger and mimics modulo

#

Ah I remember now

#

This was the old structure

#

so, if you wanted to look for an blink light, you'd go to the light folder and look for blink.py

#

now you go to the blink folder and look for light.py

#

e.g.

light
- hue
- zwave
- zigbee
sensor
- hue
- zwave
- zigbee
switch
- hue
- zwave
- zigbee

vs

hue:
- light
- sensor
- switch
zwave:
- light
- sensor
- switch
zigbee:
- light
- sensor
- switch
#

new way is 100% better

whole moon
#

It's similar to the move away from platform in the configuration

drifting swallow
#

yes

#

well, that's what it was built around initially

#

as those were the platforms

#

now there's a function that registers the platform

#

instead of assuming platforms based on py files

#

it was a big todo

#

as you can imagine

#

Anyways, back to the time_pattern crap, it probably changed, disregard what I said before. It definitely mimics modulo

#

IMO it makes it good but also bad. Seems like we should add back the old functionality. Or make a new trigger type.

#

or just use templates, but you won't get second granularity

whole moon
#

Trigger on every second using the time pattern trigger and check on the seconds in a condition

drifting swallow
#

Yea, but that clutters the traces

#

it would be nice to have it straight in the trigger