Light automation with dawn and dusk and timers

Hi
use case 1: at night I want turn lights on at dusk and turn them off at certain time, but before turn lights on, check if it is not later than “time to turn lights off”.
use case 2: in the morning, I want to turn lights on at certain time and turn them off at dawn, but before turn lights on, check if it is not later than dawn

for use case 1: automation looks like:

alias: lights on/off night
description: ""
trigger:
  - platform: numeric_state
    entity_id: sun.sun
    value_template: "{{ state.attributes.elevation }}"
    below: -5.9
    alias: lights on night
    id: lights.on.nig
  - platform: time
    at: input_datetime.outdoor_lights_off
    alias: Lights off night
    id: lights.off.nig
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: lights.on.nig
            alias: at dawn
          - condition: time
            before: input_datetime.outdoor_lights_off
            alias: and before lights off time
            after: "00:00:00"
        sequence:
          - service: light.turn_on
            data: {}
            target:
              device_id: xxx
      - conditions:
          - condition: trigger
            id: lights.off.nig
            alias: when lights off time
        sequence:
          - service: light.turn_off
            data: {}
            target:
              device_id: xxx
mode: single

for use case 2: I know it’s wrong but…

alias: lights on/off morning
description: ""
trigger:
  - platform: time
    at: input_datetime.outdoor_lights_on
    alias: lights.on.morning
    id: lights.on.mor
  - platform: numeric_state
    entity_id: sun.sun
    value_template: "{{ state.attributes.elevation }}"
    above: -5.9
    id: lights.off.mor
    alias: lights.off.morning
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: lights.on.mor
          - condition: sun
            before: sunrise --> this is not right. it should be before dawn or before sun.sun elevation below - 6
        sequence:
          - service: light.turn_on
            data: {}
            target:
              device_id: xxx
      - conditions:
          - condition: trigger
            id: lights.off.mor
        sequence:
          - service: light.turn_off
            data: {}
            target:
              device_id: xxx
mode: single

Any idea how to add condition only turn the lights on if before dawn?

Thanks

Here is how I did it. You want it earlier … before dawn though

alias: Morning
description: 5:30 am morning before sunrise
trigger:

  • platform: time
    at: “05:30:00”
    condition:
  • condition: sun
    before: sunrise
    action:
  • service: switch.turn_on
    data: {}
    target:
    entity_id: switch.lampshade_sckt_3a
    mode: single

The other way is to use 2 automations

  1. runs every midnight and sets some sensor value for next dawn after reading it from the Sun integration.
  2. Automation that is triggered on the value of the sensor value Next dawn