Script for light brightness based on sunrise/sunset

I would like to create a script to set light brightness based on sunrise/sunset. Below are the scripts I presently use to change the brightness manually. How can I combine these into 1 script based on sunrise/sunset. I’ll use this as a trigger on my Stream Deck.

office_desk_lamp_on:
  alias: Office Desk Lamp On-Day
  sequence:
  - type: turn_on
    device_id: 49dc7efaec0941a1a9a0470e8424c3f7
    entity_id: light.office_desk_lamp
    domain: light
    brightness_pct: 100
  mode: single
  icon: mdi:lightbulb
office_desk_lamp_on_night:
  alias: Office Desk Lamp On-Night
  sequence:
  - type: turn_on
    device_id: 49dc7efaec0941a1a9a0470e8424c3f7
    entity_id: light.office_desk_lamp
    domain: light
    brightness_pct: 80
  mode: single
  icon: mdi:lightbulb

If you say combine based on sunrise / sunset you mean after sunrise you want to set brightness 100 and after sunset you want to set brightness to 80?

A simple choose with sunstate should do the job then - see: Script Syntax - Home Assistant

Thank you.
This is what I came up with, but the syntax is wrong. What appropriate changes do I need to make?

# Office Desk Lamp On
office desk lamp on:
  - trigger:
      - platform: state
        entity_id: sun.sun
        state: above_horizon
    mode: queued
    action:
      - alias: "Turn on lamp and set brightness"
        choose:
          # IF (Sun is above horizon)
          - alias: "Sun Above Horizon"
            conditions: "{{ trigger.to_state.state == 'on' }}"
            sequence:
              - service: script.turn_on
                target:
                  entity_id:
                    - script.office_desk_lamp_on_day
          # ELSE (Sun is Below Horizon)
          default:
            - service: script.turn_on
              target:
                entity_id:
                  - script.office_desk_lamp_on_night

what do you mean “the syntax is wrong”? What is telling you so?

I figured it out…

office_desk_lamp:
  alias: Office Desk Lamp
  sequence:
  - choose:
    - conditions:
      - condition: or
        conditions:
        - condition: sun
          before: sunrise
        - condition: sun
          after: sunset
      sequence:
      - type: turn_on
        device_id: 49dc7efaec0941a1a9a0470e8424c3f7
        entity_id: light.office_desk_lamp
        domain: light
        brightness_pct: 80
      - service: notify.notify
        data:
          message: Light on, Brightness 80%
    default:
    - type: turn_on
      device_id: 49dc7efaec0941a1a9a0470e8424c3f7
      entity_id: light.office_desk_lamp
      domain: light
      brightness_pct: 100
    - service: notify.notify
      data:
        message: Light on, Brightness 100%
  mode: single
  icon: mdi:lightbulb

I was premature on success. Although the script executes, it is not conditioning on the sunset. It always executes the Default. HA recognizes the correct time. What am I missing?

You need to go through the automation/script debug.

I’m not understanding the output. Is this saying the condition is not even being first ran?

image

I believe I found the issue. I needed to have it structured as such…

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

Rather than…

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