Automate between sunset and sunrise

I have a door sensor and a light which I added automation for as below:

- id: '1594533764936'
  alias: Frontyard lights ON
  description: ''
  trigger:
  - device_id: db29c69701184f4b9b62569335839aa3
    domain: binary_sensor
    entity_id: binary_sensor.front_door_contact
    platform: device
    type: opened
  condition:
  - after: sunset
    condition: sun
  - condition: or
    conditions:
    - before: sunrise
      condition: sun
  action:
  - device_id: e3475abbcb0345398bcadb378af95d81
    domain: switch
    entity_id: switch.front_lights_front_lights
    type: turn_on
- id: '1594533811440'
  alias: Frontyard lights OFF
  description: ''
  trigger:
  - device_id: db29c69701184f4b9b62569335839aa3
    domain: binary_sensor
    entity_id: binary_sensor.front_door_contact
    platform: device
    type: not_opened
  condition: []
  action:
  - device_id: e3475abbcb0345398bcadb378af95d81
    domain: switch
    entity_id: switch.front_lights_front_lights
    type: turn_off

And the “ON” part (the one with the condition) does not work right now at 1am. Without the condition it works just fine so the sensor and the light are correct. What do I miss? Is it a bug or https://www.home-assistant.io/docs/scripts/conditions/ needs an update? Right now it says " to cover time between sunset and sunrise one need to use after: sunset and before: sunrise as 2 separate conditions and combine them using or".

Update:

condition: not
conditions:
  - condition: sun
    before: sunrise
    after: sunset

this works too. So I am really curious now why the recommended setup does not work.

You have made a classic mistake, we all made them
You have said, before sunrise AND after sunset
That can’t happen
Try states(‘sun.sun’) == ‘below_horizon’

So you tried to do the right thing, but this is another classic mistake – misunderstanding how the and and or conditions work. Try this:

  condition:
  - condition: or
    conditions:
    - before: sunrise
      condition: sun
    - after: sunset
      condition: sun

Or @Mutt’s suggestion is another way to effectively do the same thing, although it requires including the sun integration in your configuration, which the above does not.

EDIT: And, actually, now that we have a not condition, your updated solution is probably just as good, or better, since it’s slightly more compact. :smile:

EDIT 2: Actually, your updated solution shouldn’t work. The “inside” condition is “before sunrise and after sunset”, which, again, is always False. Then the “outside” condition inverts that, which makes it always True. What you really want is:

condition: not
conditions:
  - condition: sun
    after: sunrise
    before: sunset

The main point is: “before sunrise” means the period of time from today’s midnight (i.e., the start of today) to sunrise, and “after sunset” means from sunset to tomorrow’s midnight (i.e., the end of today.) As pointed out, it can’t be both the start of the day and the end of the day at the same time.

@pnbruckner I noticed my mistake in the second variant this morning :slight_smile:

Regarding the “or” - I misunderstood how the UI works around “or”, I think I get it now (this is how I got the first variant and I was looking how to do it right from the UI as I was doing it on the phone). Thanks!

1 Like