Automation with two conditions

I have an automation set up as follows:

trigger:
  - platform: time
    at: "18:45:00"
  - platform: sun
    event: sunset
    offset: 0
condition:
  - condition: time
    before: "00:00:00"
    after: "18:45:00"
  - condition: sun
    after: sunset

I assume that both of these conditions need to be true for the automation to activate the action. Correct? Assuming this is correct, how would I change this the automation if I wanted to activate the action when the first condition becomes true?

Correct.

Delete the second condition.

Thanks, but I do not believe that you are correct. In the current case, when both conditions need to be true, the action is activated at the later time. Thus, if sunset happens after xx:xx, the action activates at sunset, otherwise it activates at the time xx:xx.

What I am asking is this: make the action activate at the earlier of xx:xx or sunset.

That is not what you asked.

For the revised request, delete both conditions.

It will do that without the need for any conditions.

I think what you want is for the automation to only execute once per day, not twice. For example, if sunset is at 18:50, the automation will trigger twice, the first time at 18:45 (Time Trigger) and the second time at 18:50 (Sunset Trigger).

You can restrict the automation to execute just once for any given day like this:

trigger:
  - platform: time
    at: "18:45:00"
  - platform: sun
    event: sunset
    offset: 0
condition:
  - condition: template 
    value_template: "{{ this.attributes.last_triggered.date() | default(as_datetime(0)) < now().date() }}"
action:
   .. etc .. 
1 Like

Makes sense - obvious answer, i.e., dumb me…

Just turning on lights and the fact that the action gets executed twice does not matter. Thanks for the template though.

If it doesn’t matter that it’s executed twice then your automation already does what you requested:

  • If today’s sunset time is at 18:35 then the Sun Trigger will occur before the Time Trigger (which will execute the automation a second time at 18:45).

  • If today’s sunset time is 18:50 then the Time Trigger will occur (at 18:45) before the Sun Trigger (which will execute the automation a second time at 18:50).

It executes at the earliest time first, whether it’s the Time or the Sun Trigger.