Automation works until I try to make it only work at night

So I’ve been trying to get it to turn on the lights in the laundry/back path when you unlock the back door.

This works like a charm, but as you might imagine, isn’t needed for over half the day.
- id: ‘1546953267899’
alias: Lights on when it’s dark
trigger:
platform: state
entity_id: lock.back_door
from: locked
to: unlocked
action:
- service: light.turn_on
entity_id: light.laundry
- service: light.turn_on
entity_id: light.back

As soon as I add conditions around the sun it seems to stop working - below is my latest non-working rendition.

- id: '1546953267899'
  alias: Lights on when it's dark
  trigger:
    platform: state
    entity_id: lock.back_door
    from: locked
    to: unlocked
  condition:
    condition: and
    conditions:
    - condition: state
      entity_id: sun.sun
      state: 'below_horizon'
    - condition: sun
      before: sunrise
  action:
  - service: light.turn_on
    entity_id: light.laundry
  - service: light.turn_on
    entity_id: light.back

You have a lot of redundancy in your conditions. They are AND by default, so that’s not needed. Also the sun will always be below the horizon before sunrise.

Try this:

  condition:
    - condition: state
      entity_id: sun.sun
      state: 'below_horizon'

before sunrise will only work after midnight…

That’s true for ‘after sunset’ too isn’t is?

It is only true from sunset to midnight.

In which case ‘below_horizon’ is the test to use.

yeah they all relate to before midnight or after midnight and they don’t cross over.

I guess @Bartem has just been lucky he never needed to use his condition after midnight.

1 Like

Ok that makes sense…

I probably could have figured that out if I’d read the documentation a little better, sorry.

- id: '1546953267899'
  alias: Lights on when it's dark
  trigger:
    platform: state
    entity_id: lock.back_door
    from: locked
    to: unlocked
  condition:
    condition: state
    entity_id: sun.sun
    state: 'below_horizon'
  action:
  - service: light.turn_on
    entity_id: light.laundry
  - service: light.turn_on
    entity_id: light.back

will probably do me then by the sounds of it, I can get onto my next automation, turning on the mirror light when you use the bathroom light… and turning off the lights if there’s no motion for 15 minutes because someone keeps leaving them on…

I have struggled with the “before sunrise” thing for quite a while and finally worked out a solution. As many have pointed out, the reason the before sunrise only works after midnight is due to the date “rollover”. Before midnight you are in a different day and your time in hours:minutes is greater than the trigger, so the results are triggered. The way around this is to go back to a unix timestamp. What this looks like in the value template is:

{{ as_timestamp(state_attr(‘sun.sun’,‘next_rising’)) - as_timestamp(now()) }}

When doing it this way your current time (now) will always be less than the next sunrise. The results of the above are in seconds. That means if you want to trigger at 2 hours before sunrise, you would compare the above value to 7200 (2 hrs * 60 min/hr * 60 sec/mi).