Set light brightness based on sun

Hi, I would like to set a light, and all of its effects a static brightness based on the Sun, Sunset or Sunrise.

Is this possible in ESPhome config?

Thanks

Thanks Nick, could you advise the best way to integrate this for the Light.

I want to integrate it so that no matter what light effect is called, the brightness remains the same based on the sun condition.

Right now, when the effect is called, the brightness reverts back to its default.

Thanks

Could you please show your yaml.

Sure,

esphome:
  name: playbulb-candle-1
  friendly_name: playbulb-candle-1
#  on_boot:
#    priority: 600
#    then:
#      - light.control:
#          id: led
#          state: on
#          effect: BOOT
esp32:
  board: esp32dev
  framework:
    type: arduino

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: 

ota:
  password: 

wifi:
  networks:
  - ssid: !secret wifi_ssid
    password: !secret wifi_password
    bssid: 
  fast_connect: true
  on_connect:
    - light.turn_on:
        id: led
        brightness: 100%
        red: 0%
        green: 100%
        blue: 0%
        effect: none
  on_disconnect:
      - light.control:
          id: led
          state: on
          effect: ERROR

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Playbulb-Candle-1"
    password: 

captive_portal:
    
    
light:
  - platform: fastled_clockless
    id: led
    name: "LED"
    pin: 23
    chipset: WS2812
    num_leds: 1
    rgb_order: GRB
#    restore_mode: ALWAYS_OFF
    effects:
      - pulse:
      - pulse:
          name: "Fast Pulse2"
          transition_length: 0.5s
          update_interval: 0.5s
          min_brightness: 0%
          max_brightness: 100%
      - pulse:
          name: "Slow Pulse2"
          # transition_length: 1s      # defaults to 1s
          update_interval: 2s
#      - pulse:
#          name: "Asymmetrical Pulse"
#         transition_length:
#            on_length: 1s
#            off_length: 500ms
#          update_interval: 1.5s
      # Use default parameters:
#      - random:
      # Customize parameters
#      - random:
#          name: "My Slow Random Effect"
#          transition_length: 30s
#          update_interval: 30s
#      - random:
#          name: "My Fast Random Effect"
#          transition_length: 4s
#          update_interval: 5s
#      - random:
#          name: Random Effect With Custom Values
#         transition_length: 5s
#          update_interval: 7s         
#      - pulse:
#          name: "Fast Pulse"
#          transition_length: 0.5s
#          update_interval: 0.5s
#          min_brightness: 40%
#          max_brightness: 100%
#      - pulse:
#          name: "Slow Pulse"
          # transition_length: 1s      # defaults to 1s
#          update_interval: 2s
#      - strobe:
#          name: Strobe Effect With Custom Values
#          colors:
#            - state: true
#              brightness: 100%
#              red: 100%
#              green: 90%
#              blue: 0%
#              duration: 500ms
#            - state: false
#              duration: 250ms
#            - state: true
#              brightness: 100%
#              red: 0%
#              green: 100%
#              blue: 0%
#              duration: 500ms
#      - flicker:
#          name: Flicker Effect With Custom Values
#          alpha: 95%
#          intensity: 1.5%
#      - addressable_rainbow:
#          name: Rainbow Effect With Custom Values
#          speed: 10
#          width: 50
#      - addressable_color_wipe:
#          name: Color Wipe Effect With Custom Values
#          colors:
#            - red: 100%
#              green: 100%
#              blue: 100%
#              num_leds: 1
#            - red: 0%
#              green: 0%
#              blue: 0%
#              num_leds: 1
#          add_led_interval: 100ms
#          reverse: false
#      - addressable_scan:
#          name: Scan Effect With Custom Values
#          move_interval: 100ms
#          scan_width: 1
#      - addressable_twinkle:
#          name: Twinkle Effect With Custom Values
#          twinkle_probability: 5%
#          progress_interval: 4ms

#      - addressable_random_twinkle:
#          name: Random Twinkle Effect With Custom Values
#          twinkle_probability: 5%
#          progress_interval: 32ms
#      - addressable_fireworks:
#          name: Fireworks Effect With Custom Values
#          update_interval: 32ms
#          spark_probability: 10%
#          use_random_color: false
#          fade_out_rate: 120

#      - addressable_lambda:
#          name: "Wipe In"
#          update_interval: 50ms
#          lambda: |-
#            static int x = 40;
#            static int y = 0;
#            if (initial_run) {
#              x = 40;
#              y = 0;
#              it.all() = ESPColor::BLACK;
#            }
#            if (x < it.size(), (y < it.size())) {
#              it[x, y] = current_color;
#              y += 1;
#              x += 1;
#            }

#      - addressable_lambda:
#          name: "Expo Light"
#          update_interval: 16ms
#          lambda: |-
#            static int x = -400;
#            float y = 0.35+0.65*exp(-pow(x, 2)/49000);
#            int8_t r = ceil(current_color.r * y);
#           int8_t g = ceil(current_color.g * y);
#            int8_t b = ceil(current_color.b * y);
#            it.all() = ESPColor(r,g,b);
#            x += 1;
#            if (x == 400)
#              x = -400;
#
#      - addressable_lambda:
#          name: "Wipe Out"
#          update_interval: 12ms
#          lambda: |-
#            static int x = 0;
#            if (initial_run) {
#             x = it.size();
#           }
#           if (x > 0) {
#              x -= 1; 
#              it[x] = ESPColor::BLACK;
#            }

#      - lambda:
#          name: "ERROR"
#          update_interval: 1s
#          lambda: |-
#            static bool state = false;
#            auto call = id(led).turn_on();
#            call.set_transition_length(500);
#            call.set_rgb(1, 0, 0);
#            if (!state) {
#              call.set_brightness(1);
#            } else {
#              // If using 0, it freaks Home Assistant UI.
#              call.set_brightness(0.01);
#            }
#            call.perform();
#            state = !state;
#      - lambda:
#          name: "BOOT"
#          update_interval: 1s
#          lambda: |-
#            static bool state = false;
#            auto call = id(led).turn_on();
#            call.set_transition_length(500);
#            call.set_rgb(0, 1, 0);
#            if (!state) {
#              call.set_brightness(1);
#            } else {
#              // If using 0, it freaks Home Assistant UI.
#              call.set_brightness(0.01);
#            }
#            call.perform();
#            state = !state;

I am confused. The only code I see changing the effect is in on_disconnect. And I see nothing about sun.

I am trying like this.

light:
  - platform: fastled_clockless
    id: led
    name: "LED"
    pin: 23
    chipset: WS2812
    num_leds: 1
    rgb_order: GRB
      on_sunrise:
      - then:
        id: light_1
        brightness: 100%

Came up with this, will test.

sun:
  latitude: 
  longitude: 

  on_sunset:
    - then:
      - light.turn_on:
          id: led
          brightness: 20%
          effect: none
  on_sunrise:
    - then:
      - light.turn_on:
          id: led
          brightness: 100%
          effect: none

The problem with this is it does not check to see what state the sun is in after booting,

If I reboot the unit, its full brightness until the next sun cycle.

Any ideas on this one?

What I am trying to achieve is, preventing the light going above 20% brightness when the sun is below horizon, and then allowing the light to go to 100% when the sun is above the horizon, not matter what. I dont want anything to be able to override this.

Appreciate any input, been struggling now for a while with this.

Right now, it appears that the light does change static brightness based on the sun condition, however when an effect is called, like Fast Pulse, it seems to overwrite the brightness and just defaults to 100%, this is fine if the sun is above horizon, but it does it when the sun is below horizon too.

also, any idea why the led doesnt turn on on boot?

  on_boot:
    priority: 800
    then: 
      - if:
          condition:
            - sun.is_above_horizon:
          then:
              light.turn_on:
                id: led
                brightness: 100%
                green: 100%

          else:
             - light.turn_on:
                id: led
                brightness: 20%
                green: 100%
light:
  - platform: fastled_clockless
    id: "led"
    name: "LED"
    pin: 23
    chipset: WS2812
    num_leds: 1
    rgb_order: GRB

priority: 800 is maybe a little early. Try later.

Thanks, I solved it with -1000

But, something weird is happening. I cannot get the color GREEN to show on boot.

It shows like a light blue?

  on_boot:
    priority: -1000
    then: 
      - if:
          condition:
            - sun.is_above_horizon:
          then:
              light.turn_on:
                id: led
                brightness: 100%
                green: 100%

          else:
             - light.turn_on:
                id: led
                brightness: 20%
                green: 100%

Does green work otherwise ?

Yes, green seems to work when instructed from HA

Damn, I fixed this by adding

color_correct: [60%, 60%, 60%]

to the fastled_clockless integration.

now, all thats outstanding is the effects overriding the brightness,

Could you help me with this. In my effects I am not really specifying a brightness. Is there a way to specify a brightness based on the sun condition for the effects?

    effects:
      - pulse:
      - pulse:
          name: "Fast Pulse"
          transition_length: 0.5s
          update_interval: 0.5s
          min_brightness: 0%
          max_brightness: 100%
      - pulse:
          name: "Slow Pulse"
          # transition_length: 1s      # defaults to 1s
          update_interval: 2s

Thanks

I think you need to use lambda effects

Thanks,

You mean actually write the Lamda code to check the state of the sun?

Yes, worth a try