Set light brightness based on sun

Can you help me with the lambda, I dont know Lambda :frowning:

Right now, it is obeying the Sun,

its going to 20% brightness on sunset and 100% brightness on sunrise, only thing now is the effects that are overriding this and going 100 brightness no matter what.

Looking at the docs, this is what I have been able to come up with. Give the sun sensor an id id: sun Brightness may be 0-255 instead of 0-100. I also assume the sun sensor state is a string "above_horizon" if it doesnā€™t work try without the quotes above_horizon

light:
  - platform: ...
    # ...
    effects:
      - lambda:
          name: My Custom Effect
          update_interval: 0.5s
          lambda: |-
            auto call = id(led).turn_on();
            // Transition of 1000ms = 1s
            call.set_transition_length(500);
            if ( id(sun).state == "above_horizon" ) {
              call.set_min_brightness(0);
              call.set_max_brightness(20);
            } else {
              call.set_min_brightness(0);
              call.set_max_brightness(100);
           }
            call.perform();

Thanks @Mikefila

Will this effect also be a Fast Pulse effect?

Something like

      - 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_transition_length(500);
            if ( id(sun).state == "above_horizon" ) {
              call.set_min_brightness(0);
              call.set_max_brightness(20);
            } else {
              call.set_min_brightness(0);
              call.set_max_brightness(100);
           }
            call.perform();
            state = !state;

Im getting the following errors and cannot seem to get passed them.

/config/esphome/playbulb-candle-1.yaml:65:22: error: invalid operands of types 'float' and 'const char [14]' to binary 'operator=='
             if (id(sun).state == "above_horizon") {
             ~~~~~~~~~^~~~~~~~~~~~~~~~~~
/config/esphome/playbulb-candle-1.yaml:66:14: error: 'class esphome::light::LightCall' has no member named 'set_min_brightness'; did you mean 'set_brightness'?
               call.set_min_brightness(0);
              ^~~~~~~~~~~~~~~~~~
              set_brightness
/config/esphome/playbulb-candle-1.yaml:67:14: error: 'class esphome::light::LightCall' has no member named 'set_max_brightness'; did you mean 'set_brightness'?
               call.set_max_brightness(20);
              ^~~~~~~~~~~~~~~~~~
              set_brightness
/config/esphome/playbulb-candle-1.yaml:69:14: error: 'class esphome::light::LightCall' has no member named 'set_min_brightness'; did you mean 'set_brightness'?
               call.set_min_brightness(0);
              ^~~~~~~~~~~~~~~~~~
              set_brightness
/config/esphome/playbulb-candle-1.yaml:70:14: error: 'class esphome::light::LightCall' has no member named 'set_max_brightness'; did you mean 'set_brightness'?
               call.set_max_brightness(100);
              ^~~~~~~~~~~~~~~~~~
              set_brightness
light:
  - platform: fastled_clockless
    id: "led"
    name: "LED"
    pin: 23
    chipset: WS2812
    num_leds: 1
    rgb_order: GRB
    effects:
      - lambda:
          name: My Custom Effect
          update_interval: 0.5s
          lambda: |-
            auto call = id(led).turn_on();
            call.set_transition_length(500);
            if (id(sun).state == "above_horizon") {
              call.set_min_brightness(0);
              call.set_max_brightness(20);
            } else {
              call.set_min_brightness(0);
              call.set_max_brightness(100);
            }
            call.perform();

I havenā€™t worked with the sun component in ESPHome, but given the first error the state might be the sunā€™s angle/elevation, so compare with 0. Or use the sun conditions (in the docs).

Ok this project is beginning to drive me f&^%&ing mad, im 2 seconds from throwing this thing out the window.

Again now, on boot

green: 100& shows white.

WTF.

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


          else:
             - light.turn_on:
                id: led
                brightness: 20%
                color_mode: RGB
                green: 100%
light:
  - platform: fastled_clockless
    id: "led"
    name: "LED"
    pin: 23
    chipset: WS2812B
    num_leds: 1
    rgb_order: GRB
    color_correct: [60%, 60%, 60%]

Iā€™ve even tried tried with

- platform: esp32_rmt_led_strip

and still the same issue.

green: 100% should = green, not white.

What am I doing wrong here.

Ok, so I continue to fight with it,

Turns out that this

type: GRB

and this

      brightness: 20%
      red: 0%
      green: 100%
      blue: 0%
      effect: none

All matter, You have to specify the blue and red 0&, and they have to be in that order,

I think the GRB order somehow links to the order of them above too. Any way that solves the issues with booting green.

I think I have it booting, checked the sun position and then adapting brightness based on the sun

  • 20% green for below horizon
  • 100% green for above horizon

I also think I have it so it changes state when the sun state changes, the same thing just not on_boot

  • When sun changes to below horizon, adjusts to 20% brightness and remains green
  • When sun changes to above horizon, adjusts to 100% brightness and remains green.

Only thing outsrtanding now is the Lambda effect and setting the brightness.

So attempting this Lambda, I get the following error.

Would really appreciate some help with this as im pretty keen to move on with this project.

    effects:
      - lambda:
          name: Fast Pulse
          update_interval: 1s
          lambda: |-
            static unsigned long last_update = 0;
            static bool state = false;
            if (millis() - last_update >= 500) {
              state = !state;
              if (state) {
                if (id(sun_state).state) {
                  id(led).set_effect_color(0.0f, 1.0f, 0.0f, 1.0f);
                  id(led).set_brightness(1.0);
                } else {
                  id(led).set_effect_color(0.0f, 1.0f, 0.0f, 0.2f);
                  id(led).set_brightness(0.2);
                }
              } else {
                id(led).set_brightness(0);
              }
              last_update = millis();
            }

Results in this error

/config/esphome/playbulb-candle-1.yaml: In lambda function:
/config/esphome/playbulb-candle-1.yaml:106:26: error: 'class esphome::sun::Sun' has no member named 'state'
                 if (id(sun_state).state) {
                          ^~~~~
/config/esphome/playbulb-candle-1.yaml:107:18: error: 'class esphome::light::AddressableLightState' has no member named 'set_effect_color'; did you mean 'get_effect_name'?
                   id(led).set_effect_color(0.0f, 1.0f, 0.0f, 1.0f);
                  ^~~~~~~~~~~~~~~~
                  get_effect_name
/config/esphome/playbulb-candle-1.yaml:108:18: error: 'class esphome::light::AddressableLightState' has no member named 'set_brightness'
                   id(led).set_brightness(1.0);
                  ^~~~~~~~~~~~~~
/config/esphome/playbulb-candle-1.yaml:110:18: error: 'class esphome::light::AddressableLightState' has no member named 'set_effect_color'; did you mean 'get_effect_name'?
                   id(led).set_effect_color(0.0f, 1.0f, 0.0f, 0.2f);
                  ^~~~~~~~~~~~~~~~
                  get_effect_name
/config/esphome/playbulb-candle-1.yaml:111:18: error: 'class esphome::light::AddressableLightState' has no member named 'set_brightness'
                   id(led).set_brightness(0.2);
                  ^~~~~~~~~~~~~~
/config/esphome/playbulb-candle-1.yaml:114:16: error: 'class esphome::light::AddressableLightState' has no member named 'set_brightness'
                 id(led).set_brightness(0);
                ^~~~~~~~~~~~~~
*** [.pioenvs/playbulb-candle-1/src/main.cpp.o] Error 1

I donā€™t think itā€™s an order thing, because the docs state the red, green, blue and other options are optional and YAML generally doesnā€™t care about order of keys. I think itā€™s defaults, as per the docs:

All percentage options accept values in the range 0% to 100% or 0.0 to 1.0 , and default to not changing the current value (which might be the value from before the light was last turned off). To reset values, explicitly set them to zero.

Still no luck on this one.

Any ideas how I can set the brightness for the flash patterns based on the sunset. My current config is

esphome:
  name: playbulb-candle-1
  friendly_name: playbulb-candle-1
  on_boot:
    priority: -1000
    then: 
      - if:
          condition:
            - sun.is_above_horizon:
          then:
              light.turn_on:
                id: led
                brightness: 100%
                red: 0%
                green: 100%
                blue: 0%
          else:
             - light.turn_on:
                id: led
                brightness: 20%
                red: 0%
                green: 100%
                blue: 0%

esp32:
  board: esp32dev
  framework:
    type: arduino

sun:
  latitude: 
  longitude: 
  id: sun_state
  on_sunset:
    - then:
      - light.turn_on:
          id: led
          brightness: 20%
          red: 0%
          green: 100%
          blue: 0%
          effect: none
  on_sunrise:
    - then:
      - light.turn_on:
          id: led
          brightness: 100%
          red: 0%
          green: 100%
          blue: 0%
          effect: none

time:
  - platform: homeassistant

light:
  - platform: esp32_rmt_led_strip
    rgb_order: GRB
    rmt_channel: 1
    chipset: WS2812
    pin: GPIO23
    num_leds: 1
    id: led
    name: "LED"
    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
      - pulse:
          name: "Asymmetrical Pulse"
          transition_length:
            on_length: 1s
            off_length: 500ms
          update_interval: 1.5s

What is the problem you are observing? You give us your config, that is good. But what difficulty are you having?

The flash effects just default to %100 brightness, regardless of the sun state,

Setting the static brightness is all good, working well but the effects just always default to 100% brightness

I donā€™t think sun is a boolean. I am pretty sure this is just checking to see if sun sensor has any state, and if it does, returns true.

Thanks @Mikefila

Can you show me where in the YAML to put this? I have not worked much at all with Lambdas.

Thanks

It defaults to 100% brightness because your automation tells it to do exaxtly that.

  1. On sunsetā€¦
  2. Then.
  3. Turn On Light
  4. Set color/brightness
  5. Here, you specifically set the effect to ā€œnoneā€ soā€¦ that seems like an issue if your expecting some effects to start with the light. ā€œNoneā€ means no effects.

Just be patient, take a moment to read through your code and make sure you understand it. Break it down into steps1,2,3 if that helps you better mentally understand whats going on. Copying bits of code people post on here can be very helpful but, its still important to try to understand whats going on in your code otherwise youā€™re just overcomplicating it and making your life harde than it needs to be.

Try this add a binary template

binary_sensor:
  - platform: template
    name: "sun up"
    id: sun_up

Add the following to the sun component

sun:
  latitude: ...your lat
  longitude: ...your long
  on_sunrise: 
    then:
      - lambda: |-
          id(sun_up).publish_state(true);
  on_sunset: 
    then:
      - lambda: |-
          id(sun_up).publish_state(false);

Then use the binary for the lambda

                if (id(sun_up).state) {
                  id(led).set_effect_color(0.0f, 1.0f, 0.0f, 1.0f);
                  id(led).set_brightness(1.0);
                } else {
                  id(led).set_effect_color(0.0f, 1.0f, 0.0f, 0.2f);
                  id(led).set_brightness(0.2);
                }

Thanks, just this bit. Is this the lambda for the Effect.

Then use the binary for the lambda

                if (id(sun_up).state) {
                  id(led).set_effect_color(0.0f, 1.0f, 0.0f, 1.0f);
                  id(led).set_brightness(1.0);
                } else {
                  id(led).set_effect_color(0.0f, 1.0f, 0.0f, 0.2f);
                  id(led).set_brightness(0.2);
                }

I just want to make sure I was clear before. Everything in my current posted config is working as expected. So the LED dims to 20% on sunset and goes full bright to 100% on sunrise. It also does this by checking the sun condition on boot and adjusting the brightness according to the sun state.

The only issue I am having is when calling the EFFECTS, then they just display at 100% brightness regardless, such as fast pulse.

I just wanted to Clarify also relating to @Fallingaway24 post.

Can this LABMDA go in the effect?

Thanks