Automation help: time_pattern trigger for multiple hours and minutes

Hey there,

It’s me again with a weird / complex automation/trigger question :wink:

I’m trying to get one or two automations done that will control my X-Mas lights on the balcony. To not blind my neighbors and make them get epileptic attacks, I want to enable an effect every 10 minutes and let it run for just 5 minutes. So I was planning to have two automations:

  • One that sets an effect every 10 minutes
  • One that resets it to a solid color / disables the effect
  • Aaaaand both of these only between 6 and 10pm

So I found the time_pattern platform and thought that would be it. As somebody who’s used to write crontab entries in his past, that sounded like it.
If the time_pattern would work as I expected it to work (== like crontab), there are basically two solutions to my requirement:

  1. Have two automations: One running every 10 minutes that sets an effect and one every 5 minutes that disables any effect. But the issue here is, that I can’t guarantee the execution order, so the activate might run first and a second after the deactivate automation. So I thought solution two is more suitable:
  2. Again two automations: One runs at minute 0, 15, 30, 45 to activate an effect and one that runs at minutes 5, 20, 35, 50 to deactivate any effect. This would work perfectly.

Of course, both would also have a condition to run only between 6 and 10pm. Somehow.

Unfortunately, all my attempts fail to use the time_pattern platform. My first try was this:

trigger:
- platform: time_pattern
  hours: [18,19,20,21,22]
  minutes: [0,15,30,45]

This is not throwing any error and I can successfully reload the automations. But it doesn’t work. It’s doing something “random”. Maybe it’s just executing the last working state of that automation. I actually don’t know. But it’s not doing what I expected it to do.
I also tried specifying it as a comma separated string, but that even results in an error when trying to reload the automation.
So despite it being “hours” and “minutes” (plural!), it seems to only accept a single minute or hour!?

Also, is it even possible to specify both “hours” and “minutes” in the same trigger? Or do I need to specify two triggers with an “and”?

I know, that there are probably other ways to solve this with weird template triggers (found something close to that I want to do in the forums already) or maybe even combining the time_pattern trigger with one or more conditions and probably also here template comparisons…

I’m just wondering what the limits of the time_pattern trigger are and if somebody managed to do something more complex than just a single minute/hour or using the “/x” pattern. The docs are not that helpful, as they don’t give too many examples: https://www.home-assistant.io/docs/automation/trigger/#time-pattern-trigger

Or maybe somebody even has an automation that does something similar to what I want to do :wink:

Thanks and greetings,

Andy!

A time pattern set to /15 minutes will trigger every 15 minutes (00,15,30,45), and then use the condition to divine the time is between the correct hours:

trigger:
  platform: time_pattern
  minutes: '/15' 
condition:
  condition: time
  after: '17:59:59'
  before: '22:00:01'

Do what mf_social said, but combine it to a single automation with a delay.

action:
  - service: my_effects_service
  - delay: 
      minutes: 5
  - service: my_solid_color_service

No need to have 2 automations doing this

2 Likes

Thx for the fast answer guys!

The combination of your solutions I guess would do it. I will probably end up doing it that way. Thx!

But again: My goal was also to find out if the trigger is actually that limited, so it can really only accept a single value. And I actually just looked at the code and it seems that it cannot accept lists. I’m curious though why it didn’t fail when I specified one.
It does however support having hours and minutes in a single trigger.
[EDIT] It actually doesn’t: It’s nulling the smaller values if a larger is specified. So If you specify minutes and hours, the minutes are wiped / nulled…

Maybe I end up changing it and allowing multiple values to make it a bit more like cron. Maybe that’s going to be my first contribution to core :wink:

As always, leaving my full solution here for everybody to copy and use.

First: I’m using WLED on an ESP8266 (actually on a D1mini on a QuinLED-Dig-Uno!) controlling my LED strings installed on my balcony. Currently has only one segment, that makes it kinda easy.

That’s the automation I did, based on @jocnnor and @anon43302295 help:

- id: balcony_xmas_lights
  alias: '[Outside] Balcony X-Mas Lights'
  trigger:
  - platform: time_pattern
    minutes: "/15"
  condition:
  - condition: time
    after: '17:59:59'
    before: '22:00:01'
  - condition: not
    conditions:
    - condition: state
      entity_id: light.outside_wled_balcony_light
      state: unavailable
  action:
  - service: python_script.wled_xmas_random_effect
    data:
      entity_id: light.outside_wled_balcony_light
  - delay: 
      minutes: 5
  - service: wled.preset
    data:
      entity_id: light.outside_wled_balcony_light
      preset: 16

This will trigger a Python script that selects a random effect and 5 minutes after this, will reset WLED to preset 16, which has my calm effect / basically static color saved to it.

And that’s the Python script (Note: I removed a lot of effects to shorten it. You can add any effect to the list you want it to choose from):

effects = [
  {
    "effect": "Bpm",
    "intensity": 100,
    "speed": 1
  },
  {
    "effect": "Colortwinkles",
    "intensity": 255,
    "speed": 30
  }
]

entityId = data.get("entity_id")
logger.info(entityId)
effect = random.sample(effects, 1).pop()
logger.info(effect)

serviceData = {
  "entity_id": entityId,
  "effect": effect["effect"],
  "intensity": effect["intensity"],
  "speed": effect["speed"]
}

logger.info(serviceData)

hass.services.call('wled', 'effect', serviceData)

Hope somebody finds this useful :slight_smile:

1 Like

For anyone that would want to do the python equivalent in an automation or script, you can do it with variables.

I think the python script is perfect for this also, but I think there are some people that just never ssh into their machine and create the required subfolders to run python scripts.

- id: balcony_xmas_lights
  alias: '[Outside] Balcony X-Mas Lights'
  variables: 
    effects:
      bpm:
        name: "Bpm"
        intensity: 100
        speed: 1
      color:
        name: "Colortwinkles"
        intensity: 255
        speed: 30
    random_effect: "{{ effects | list | random }}"
  ...
  ...
  action:
  # Pick a random effect
  - service: wled.effect
    data:
      entity_id: light.outside_wled_balcony_light
      effect: "{{ effects[random_effect].name }}"
      intensity: "{{ effects[random_effect].intensity}}"
      speed: "{{ effects[random_effect].speed }}"
  # Loop through all effects until this condition is true
  - repeat:
      sequence:
        - variables:
           # Use the loop index and use modulo for wrap around.
           next_effect: "{{ (effects | list)[repeat.index % (effects | count)] }}"
        - service: wled.effect
          data:
            entity_id: light.outside_wled_balcony_light
            effect: "{{ effects[next_effect].name }}"
            intensity: "{{ effects[next_effect].intensity}}"
            speed: "{{ effects[next_effect].speed }}"
        - delay:
            minutes: 5
      until:
        # Do this until told to stop
        - condition: state
          entity_id: binary_sensor.light_looper
          state: 'off'

Again, I think your python script is perfect for this. This was more for example lol

1 Like

No, I actually think that is way better, as in my personal opinion, one should always try to not use python scripts but do everything with native features of HA. I may just use your solution now :smiley: I totally forgot about the new variables already!

Thanks!