Light automation doesn't work after midnight

Hello,

I’ve made a simple automation to turn on lights when I come back home, it’s only triggered if it’s 30 minutes before sunset and if the lights are turned off, it works well before midnight, if I come back later the lights wont’ turn on. I’m sure that system is working because I set up a “welcome back” notification to my phone and that works every time, do you have any idea why this happens? Is it because of the sunset condition?

Thank you all, here is the script:

- alias: back home
  trigger:
    platform: state
    entity_id: device_tracker.me_phone_bt
    from: not_home
    to: home
  condition:
    condition: and
    conditions:
      - condition: sun
        after: sunset
        after_offset: "-00:30:00"
      - condition: state
        entity_id: light.entrance
        state: "off"
  action:
    - service: light.turn_on
      data:
        entity_id: light.entrance
        color_name: green
    - delay: 00:03:00
    - service: light.turn_off
      data:
        entity_id: light.entrance
        transition: "30"

Yes, it looks like it’s the sunset condition. Since you’ve only specified one time condition, it’ll only work from that condition until midnight.

Put in a sunrise condition as well, and that should fix it up.

@icaman004 is right. And since this can be a bit tricky:

- alias: back home
  trigger:
    platform: state
    entity_id: device_tracker.me_phone_bt
    from: not_home
    to: home
  condition:
    - condition: state
      entity_id: light.entrance
      state: "off"
    - condition: or
      conditions:
        - condition: sun
          after: sunset
          after_offset: "-00:30:00"
        - condition: sun
          before: sunrise
          before_offset: "00:30:00"
  action:
    - service: light.turn_on
      data:
        entity_id: light.entrance
        color_name: green
    - delay: 00:03:00
    - service: light.turn_off
      data:
        entity_id: light.entrance
        transition: "30"

If you list multiple conditions under condition: you don’t have to specify and - that’s the default. So the above condition will be true if the light is off and it’s either between 30 minutes before sunset and midnight or between midnight and 30 minutes after sunrise. Adjust the offsets if you’d like a different time period.

Thank you all!

@pnbruckner I didn’t know that the default condition is an and, glad to know and thanks for the automation.

1 Like