Time to operate office light - below horizon problem?

I have an automation to turn on an office light when i walk in to the office after sunset but before 2am (I don’t want it to come on early in the morning)
It was my belief that the automation system for time knows the spanning of midnight, however, I ended up with this trace…

Result:
after:
  __type: <class 'datetime.time'>
  isoformat: '00:00:00'
now_time:
  __type: <class 'datetime.time'>
  isoformat: '16:28:47.378672'
before:
  __type: <class 'datetime.time'>
  isoformat: '02:00:00'
result: false```

...from this code...


alias: Office Light On/Off
description: “”
triggers:

  • trigger: state
    entity_id:
    • binary_sensor.office_presence_occupancy
      from: “off”
      to: “on”
      id: detected
  • trigger: state
    entity_id:
    • binary_sensor.office_presence_occupancy
      from: “on”
      to: “off”
      id: clear
      conditions:
  • condition: state
    entity_id: sun.sun
    state: below_horizon
  • condition: time
    before: “02:00:00”
    actions:
  • choose:
    • conditions:
      • condition: trigger
        id:
        • detected
          sequence:
      • action: light.turn_on
        metadata: {}
        data:
        brightness_pct: 16
        target:
        device_id: debdfcb9f64847d605aac81b91441be0
    • conditions:
      • condition: trigger
        id:
        • clear
          sequence:
      • action: light.turn_off
        metadata: {}
        data: {}
        target:
        device_id: debdfcb9f64847d605aac81b91441be0
        mode: single
It seems that my definition for below horizon is somehow returning midnight?

I think I’ve got it. I had to set up a discrete time for the “after midnight” part to work, and then set up a separate and for the after sunset.

alias: Office Light On/Off
description: ""
triggers:
  - trigger: state
    entity_id:
      - binary_sensor.office_presence_occupancy
    from: "off"
    to: "on"
    id: detected
  - trigger: state
    entity_id:
      - binary_sensor.office_presence_occupancy
    from: "on"
    to: "off"
    id: clear
conditions:
  - condition: and
    conditions:
      - condition: sun
        after: sunset
      - condition: time
        before: "02:00:00"
        after: "14:00:00"
actions:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - detected
        sequence:
          - action: light.turn_on
            metadata: {}
            data:
              brightness_pct: 16
            target:
              device_id: debdfcb9f64847d605aac81b91441be0
      - conditions:
          - condition: trigger
            id:
              - clear
        sequence:
          - action: light.turn_off
            metadata: {}
            data: {}
            target:
              device_id: debdfcb9f64847d605aac81b91441be0
mode: single