Newb: Time of day sets brightness of LED light

Hi there. I have the following

output:
 - platform: ledc
   pin: GPIO3
   id: pwm_light
   frequency: 1220Hz

light:
 - platform: monochromatic
   output: pwm_light
   name: "Light"
   id: envlight

It all works as you’d expect. I want to set the brightness based on the cosine of the time of day. (on at 6am, full power at noon off at 6pm) and it needs to run on the ESP not HA.

basically, this:

everyMinute() {
   bri=max(0,cos(percentage_of_day(time))
   id(envlight).turn_on().set_brightness(bri).perform();
}

I already tried looking at the blueprints stuff, but that’s HA side.

I can create buttons that set the value when pressed, and I briefly had an action that turned things on and off based on time, but I can’t find anything simple enough to get my head around that does the fading, and then extend. Can anyone please help :slight_smile:

Thanks
Nigel

Add a time component:

Either HA or SNTP time.

Then add an on_time: trigger:

time:
  - platform: sntp
    # ...
    on_time:
      # Every minute
      - seconds: 0
        minutes: /1
        then:
          - lambda: |-
             int bri=max(0,cos(percentage_of_day(time));
             id(envlight).turn_on().set_brightness(bri).perform();

I haven’t checked the validity of your C++ code, and there may be other ways to achieve the same thing. You may also need to add other checks - but you get the idea…

1 Like

Thanks @zoogara that was basically what I needed, I tweak it with real C++, but this compiled second attempt, and ran first time (ok, second after I inverted the cosine and multiplied the angle by 2PI):

time:
  - platform: homeassistant
    id: homeassistant_time
    on_time_sync:
     then:
      - logger.log: "Synchronized system clock"
    on_time:
      # Every minute
      - seconds: 0
        minutes: /1
        then:
          - logger.log: "Synchronized light source"
          - lambda: |-
             auto time = id(homeassistant_time).utcnow();
             if(time.is_valid()) {
               float hrs = time.hour + time.minute/60.0 + time.second/3600.0;
               float bri=max(0.0,-cos((hrs/24.0)*2*PI));
               id(envlight).turn_on().set_brightness(bri).perform();
             }

Thanks for the perfect helping :slight_smile:
N.

2 Likes