One automation to run at various points in time

I am now setting up my first automations in HA, and I see it requires slightly different way of thinking than Domoticz (which I used previously).

I had one script to drive the LED in my Xiaomi air purifier:

  • at sunrise turn on LED (on dim setting)
  • at 9:00 switch to full brightness
  • at sunset dim it again
  • and at 23:00 switch it altogether.

In Domoticz, I could have one automation covering this, is there a way to handle this with one automation in HA?

You can but why make it overly complex?

You can use templating to determine which entity triggered the automation and what the old and new state are.

yes, you can do it in one automation by using “wait_template:” as steps in the automation.

create the automation with sunrise as a trigger then in the action the first one will be to turn on the LED, then wait for each milestone to switch to the next step with the last step being to turn the led off.

But I wouldn’t do it like that since the automation will be running continuously all day and if one part fails then the rest also fail.

just make a few automations that trigger at the milestones above and perform just one action each. It will be way more reliable.

2 Likes

You can but why make it overly complex?

My reasoning was that having one automation would give me 1 switch to activate/deactivate it.

You can create a group of automations and switch that on/off. Or create a script that can toggle the state of all automations.

Ah, ok, thanks. Grouping devices and changing the lovelace layout are still on my ‘to-do’ list.

But according to your requirements above you don’t “activate it” (I assume you mean “trigger” or “to cause the automation to run”) by a switch. It runs at sunrise to turn on the LED. So the “trigger” would be “at sunrise”. then the next automation would have “at 9am” as it’s trigger. then “at sunset”, then “at 11pm”.

Yes, it will ‘trigger’ automatically based on the time. But I’d probably also want to turn off the script altogether (so the LED does not cycle throughout the day - e.g. switch it off completely).

you could create an input boolean (virtual switch) and put it in the conditions for each of the automations to prevent the action if the boolean is turned off.

You can do this in one automation using data_template in the action:

trigger:
- platform: sun
  event: sunrise
- platform: time
  at: '09:00'
- platform: sun
  event: sunset
- platform: time
  at: '23:00'
action:
- service: light.turn_on
  entity_id: light.LED
  data_template:
    brightness: >
      {% if trigger.platform == 'sun' %}
        50
      {% else %}
        {{ 255 if trigger.now.hour == 9 else 0 }}
      {% endif %}