Help with conditions combining the sun and a time

I need some help with automation of lights that I would like to come on at 4pm but only if the sun has set. If the sun has not set at 4pm I want it to come on at sunset.

I have been trying with this but it is not working.

- alias: Turn on light in kitchen and livingroom at sunset
  trigger:
    - platform: time
      at: '16:00:00'
    - platform: sun
      event: sunset
  condition:
    - condition: sun
      after: sunset
  action:
    - service: light.turn_on
      data:
        entity_id: 
          - light.koksfonster_vanster
          - light.koksfonster_hoger_54
          - light.vrum_fonster_4
          - light.belysning_horn_soffa_40
          - light.vrum_hornbumling_33

However this is not working. The light come on at sunset (about 3.20pm) Any help would be greatly appreciated!

So if I understand you correctly, if the sunsets earlier than 4pm you don’t want the lights to turn on?

Right now you have 2 triggers for this automation, either 4pm or sunset and one condition. So if the sun sets at 3:20 it looks for the condition that it’s set (which it would be) and executes.

If you wanted to only trigger 4pm or later, you’ll need to add another condition to check if it’s after 4pm.

This should do it

- alias: Turn on light in kitchen and livingroom at sunset
  trigger:
    - platform: sun
      event: sunset
  condition:
  - condition: time
    after: '16:00:00'
  action:
    - service: light.turn_on
      data:
        entity_id: 
          - light.koksfonster_vanster
          - light.koksfonster_hoger_54
          - light.vrum_fonster_4
          - light.belysning_horn_soffa_40
          - light.vrum_hornbumling_33
1 Like

Oh thats how it should be done. Will try it straight away and see if it works today! Thanks! :smile:

Thanks for explaining! Kind of new to this and find it some what difficult to work with the conditions. Again, thanks! :smile:

Looking at your suggestion once again. Does this condition trigger the light to go on at 4pm if the sun sets before 4pm? I can understand that it will not trigger before 4pm if the sun has not set.
Some more background to understand what I want to achieve. during the winter the sun sets before 4pm but generally no one is at home before 4pm so no need to go off before. During the sumer the sun sets later (like 9pm) and at that time i would like it to trigger at sunset. Just wonder if this can be done i one automation…?

The answer here is this:


 - alias: Turn on light in kitchen and livingroom at sunset
  trigger:
    - platform: time
      at: '16:00:00'
    - platform: sun
      event: sunset
  condition:
    condition: and
    conditions:
      - condition: sun
        after: sunset
      - condition: time 
        after: '15:59:59'
  action:
    - service: light.turn_on
      data:
        entity_id: 
          - light.koksfonster_vanster
          - light.koksfonster_hoger_54
          - light.vrum_fonster_4
          - light.belysning_horn_soffa_40
          - light.vrum_hornbumling_33

This way it triggers for either event and checks that the other is valid.

I use this method myself for my lights to come on when someone comes home after dark, but if somebody is already home it waits until it gets dark to trigger. Example…

Link removed

PS - I’ve just noticed that the condition offset in my GitHub is wrong and should be ‘-00:40:00’ to match the trigger, to avoid confusion :laughing:

Great, just what I was looking for!

1 Like

Thanks! Works fine!

1 Like

Yes, mine would trigger at sunset, but only if it was after 4pm.

So if sunset was 4:15pm, 5pm, 7pm or 9pm it would turn on the light. But if sunset was before 4pm it wouldn’t turn on the light.

1 Like