Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option to change graph shape for brightness #437

Closed
Tjomsen opened this issue Feb 27, 2023 · 17 comments
Closed

Option to change graph shape for brightness #437

Tjomsen opened this issue Feb 27, 2023 · 17 comments

Comments

@Tjomsen
Copy link

Tjomsen commented Feb 27, 2023

For my use, the brightness graph shape has much too "narrow" low brightness period, and significant offsets of sunrise/sunsets does not remedy the situation, and also impacts daytime brightness.

I live near the arctic circle, so the day is short half of the year. And, several of my lights have somewhat bright dimming curves, needing to go below 20% to get really dim.

I would LOVE to be able to use the graph shape of the color temperature for brightness values. i.e. a defined/longer period where the lights will be at/near a minimum value before going gradually up max during light daytime. This would allow my usecase of having a significantly longer period of lowly dimmed lights than current graph shapes and manipulator settings allow.

@lightheaded
Copy link

+1

I have a similar use-case, but I'd like to reach the minimum brightness level by the time my kids go to sleep. Currently there's only the "sleep mode" which can be automated, but kind of defeats the purpose of this addon then.

@backcountrymountains
Copy link

I am an adult and I also want the lights to be at minimum brightness when I go to bed.

I made a fork: Wake and Sleep times

The changes add a Wake time for AL to start increasing brightness instead of increasing starting at midnight.

It also adds a Sleep time so AL ramps down the brightness from sunset until Sleep time and keeps it at the minimum brightness from Sleep time until Wake time.

My config is now: (notice the wake_time and sleep_time)

  - name: Main
    interval: 360
    include_config_in_attributes: true
    transition: 10
    initial_transition: 5
    interval: 300
    adapt_delay: 5
    send_split_delay: 2000
    min_brightness: 5
    max_brightness: 100
    min_color_temp: 2700
    max_color_temp: 6500
    sunrise_time: "08:00:00"  # override the sunrise time
    sunset_time: "18:30:00"

    wake_time: "06:00:00"
    sleep_time: "22:00:00"

    take_over_control: true
    separate_turn_on_commands: true

image

So far it is working fine, but I haven't tested edge cases.

Definitely have the times set up so that wake_time<sunrise_time<sunset_time<sleep_time and wake_time != sleep_time != solar_midnight

I'm not a programmer and I don't know what I'm doing so sorry if the code is ugly/wrong.

Also, this might work for #218

@TheWanderer1983
Copy link

TheWanderer1983 commented Jul 20, 2023

Hello @backcountrymountains,

I've been using your mod and I'm happy with the changes. I noticed that the light temperature changes before the ramp down of brightness. I would like to have AL ramp down the colour temperature and brightness from sunset until Sleep time. Basically, I would like to have the colour temperature follow the same as the brightness. Is that already possible?

One other suggestion, Instead of fixed wake time is it possible to have a offset to sunrise as the wake time? for example 25 minutes before sunrise as the wake time.

@backcountrymountains
Copy link

I wrote a thing explaining how adaptive_lighting calculates the percent value, which is used to calculate the brightness and color temperature for a given time: #633. Graph

In my hack of adaptive_lighting, percent is -1 from sleep_time to wake_time. Between wake_time and sunrise it goes from -1 to 0. Between sunrise and sunset it goes from 0, to 1 at noon, and back to 0 at sunset. From sunset to sleep_time it goes from 0 to -1.

The color temperature is calculated using the percent value in this code ( I added some comments):

    def calc_color_temp_kelvin(self, percent: float) -> int:
        """Calculate the color temperature in Kelvin."""
        if percent > 0:
           """Between sunrise and sunset (sun above horizon)"""
            delta = self.max_color_temp - self.min_color_temp
            ct = (delta * percent) + self.min_color_temp
            return 5 * round(ct / 5)  # round to nearest 5
        if percent == 0 or not self.adapt_until_sleep:
            return self.min_color_temp
        if self.adapt_until_sleep and percent < 0:
            delta = abs(self.min_color_temp - self.sleep_color_temp)
            ct = (delta * abs(1 + percent)) + self.sleep_color_temp
            return 5 * round(ct / 5)  # round to nearest 5

I changed the code so it adjusts the color temp from warmest at wake_time to coldest at sunrise and coldest at sunset to warmest at sleep_time:

    def calc_color_temp_kelvin(self, percent: float) -> int:
        """Calculate the color temperature in Kelvin."""
        if percent < 0:
            delta = self.max_color_temp - self.min_color_temp
            ct = delta * abs(1 + percent) + self.min_color_temp
            return 5 * round(ct / 5)  # round to nearest 5
        if percent >= 0:
            return self.max_color_temp

image

I made a fork of my fork for switch.py to change ct and brightness at the same time:

https://github.com/backcountrymountains/adaptive-lighting/blob/backcountrymountains-ct-change-with-brightness/custom_components/adaptive_lighting/switch.py

I'm not sure about how to change the wake time to an offset but I like the idea and I'll look into it.

Also, I found an error in my code with an edge case of exactly noon or midnight that I fixed.

@TheWanderer1983
Copy link

TheWanderer1983 commented Jul 26, 2023

Hello @backcountrymountains
I'm getting these errors using the new switch.py.

Source: custom_components/adaptive_lighting/switch.py:1703
Integration: Adaptive Lighting (documentation, issues)
First occurred: 07:15:44 (124 occurrences)
Last logged: 09:49:27
Error doing job: Task exception was never retrieved

Traceback (most recent call last):
File "/config/custom_components/adaptive_lighting/switch.py", line 1137, in _async_update_at_interval
await self._update_attrs_and_maybe_adapt_lights(
File "/config/custom_components/adaptive_lighting/switch.py", line 1285, in _update_attrs_and_maybe_adapt_lights
self._sun_light_settings.get_settings(
File "/config/custom_components/adaptive_lighting/switch.py", line 1775, in get_settings
brightness_pct = self.calc_brightness_pct(percent, is_sleep)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/config/custom_components/adaptive_lighting/switch.py", line 1703, in calc_percent
k = 1 if next_event in (SUN_EVENT_NOON, SUN_EVENT_SUNSET) else k
^
UnboundLocalError: cannot access local variable 'k' where it is not associated with a value

and

Logger: homeassistant.components.websocket_api.http.connection
Source: custom_components/adaptive_lighting/switch.py:1703
Integration: Home Assistant WebSocket API (documentation, issues)
First occurred: 09:03:24 (6 occurrences)
Last logged: 09:43:27

[547419049744] cannot access local variable 'k' where it is not associated with a value
[547352315536] cannot access local variable 'k' where it is not associated with a value

Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/components/websocket_api/commands.py", line 205, in handle_call_service
await hass.services.async_call(
File "/usr/src/homeassistant/homeassistant/core.py", line 1965, in async_call
response_data = await coro
^^^^^^^^^^
File "/usr/src/homeassistant/homeassistant/core.py", line 2005, in _execute_service
return await cast(
^^^^^^^^^^^
File "/usr/src/homeassistant/homeassistant/helpers/entity_component.py", line 235, in handle_service
return await service.entity_service_call(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 848, in entity_service_call
response_data = task.result() # pop exception if have
^^^^^^^^^^^^^
File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1192, in async_request_call
return await coro
^^^^^^^^^^
File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 892, in _handle_entity_call
result = await task
^^^^^^^^^^
File "/config/custom_components/adaptive_lighting/switch.py", line 1122, in async_turn_on
await self._update_attrs_and_maybe_adapt_lights(
File "/config/custom_components/adaptive_lighting/switch.py", line 1285, in _update_attrs_and_maybe_adapt_lights
self._sun_light_settings.get_settings(
File "/config/custom_components/adaptive_lighting/switch.py", line 1775, in get_settings
brightness_pct = self.calc_brightness_pct(percent, is_sleep)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/config/custom_components/adaptive_lighting/switch.py", line 1703, in calc_percent
k = 1 if next_event in (SUN_EVENT_NOON, SUN_EVENT_SUNSET) else k
^
UnboundLocalError: cannot access local variable 'k' where it is not associated with a value

@backcountrymountains
Copy link

Apologies, I don't know how to use github or program.
It is necessary to move the whole custom_components\adaptive_lighting folder, which contains wake_time and sleep_time changes in switch.py, const.py, and config_flow.py. Then you can use the switch.py from my most recent comment.

Also, it is currently necessary to have a config with wake_time: and sleep_time: like I referenced in comment
You can change the values in the config, but I think each item in the config must be present for it to work.

It's really a hack and doesn't work for all use cases, but does work for the specific config I posted. I'm going to try and make it work better but I'm no programmer so we'll see how that goes.

@TheWanderer1983
Copy link

TheWanderer1983 commented Jul 28, 2023

Hello @backcountrymountains ,

I should of explained my setup more. I had installed your fork of Adaptive lighting via hacs. It was working fine. I then copied over just the new switch.py and restarted adaptive_lights i then got those errors in logs. I've now done it again from scratch and it seems to be working. I'll keep you posted. This was after your recent update though.

@TheWanderer1983
Copy link

Hello again,

The wake time and sleep time is working for me but the temperature colour isn't following it. It's still following the sun.

@dusanjencik
Copy link

Hi,

first of all thanks @backcountrymountains for a nice contribution. It would be great if it's added to the main repo as the additional option for people who appreciate this. Definitely some polishing is still needed, however it's working fine for me. Until that time I'm going to use your "hacky" solution, because it's all what I needed. Modifying start and end transition times is a must have for me.

For @TheWanderer1983 and others having a problem with the integration. I have just copied the code from https://github.com/backcountrymountains/adaptive-lighting/tree/backcountrymountains-ct-change-with-brightness/custom_components/adaptive_lighting and had to update manually one line in switch.py
From this:
line 1703: k = 1 if next_event in (SUN_EVENT_NOON, SUN_EVENT_SUNSET) else k
to this:
line 1703: k = 1 if next_event in (SUN_EVENT_NOON, SUN_EVENT_SUNSET) else -1
and after restart it's working great with no errors.

The temperature color and brightness are in sync.
Snímek obrazovky 2023-07-31 v 9 42 28

The settings I have just for reference:
sunrise_time: 09:00:00
wake_time: 07:30:00
sunset_time: 19:30:00
sleep_time: 21:00:00

I hope this will help others to enjoy this great modification as well.

@TheWanderer1983
Copy link

TheWanderer1983 commented Jul 31, 2023

Hey @dusanjencik ,

Thanks for the update. I had manually updated that line also to get it working, but I had changed k to 1 not -1 and it wasn't in sync. I'll give that another shot with -1.

I agree, it would be nice to see changes like these in the main AL instead of a fork, I would like to continue to get code updates instead of being stuck at this release. Hopefully both wake time / sleep time and temperature change. This makes AL a useful app for me.

@TheWanderer1983
Copy link

TheWanderer1983 commented Aug 1, 2023

Okay it looks like its working for me. Thank you @backcountrymountains . One last thing that would make this perfect is the ability to change the wake time to some offset of sunrise. If that gets implemented great, if not here is a simple HA automation script to update wake time daily to do it.

alias: Change Wake Time for Sunrise
description: HA automation to change the wake time to a offset value of sunrise each day
trigger:
  - platform: sun
    event: sunrise
    offset: "-00:25:00"
condition: []
action:
  - service: adaptive_lighting.change_switch_settings
    data:
      use_defaults: current
      entity_id: switch.adaptive_lighting_adaptive_lighting
      wake_time: "{{ now().strftime(\"%H:%M:%S\") }}"
mode: single

@basnijholt
Copy link
Owner

Hi folks, I have implemented this feature request in #699.

That PR allows setting different brightness_modes which determine how the brightness changes around sunrise and sunset. Here brightness_mode can be either "default" (current behavior), "linear", or "tanh". Additionally, when not using "default", you can set brightness_mode_time_dark and brightness_mode_time_light.

with brightness_mode: "linear":

  • During sunset the brightness will start adapting constantly from max_brightness at time=sunset_time - brightness_mode_time_light to min_brightness at time=sunset_time + brightness_mode_time_dark.
  • During sunrise the brightness will start adapting constantly from min_brightness at time=sunrise_time - brightness_mode_time_dark to max_brightness at time=sunrise_time + brightness_mode_time_light.

with brightness_mode: "tanh" it uses the smooth shape of a hyperbolic tangent function:

  • During sunset the brightness will start adapting from 95% of the max_brightness at time=sunset_time - brightness_mode_time_light to 5% of the min_brightness at time=sunset_time + brightness_mode_time_dark.
  • During sunrise the brightness will start adapting from 5% of the min_brightness at time=sunrise_time - brightness_mode_time_dark to 95% of the max_brightness at time=sunrise_time + brightness_mode_time_light.

It would be awesome to get some feedback!

@basnijholt
Copy link
Owner

basnijholt commented Aug 4, 2023

Some more examples:
Notice the values of brightness_mode_time_light and brightness_mode_time_dark in the text box.
image
image
image

@basnijholt
Copy link
Owner

Please try out version 1.19.0 beta 1 🎉 🚀

@basnijholt
Copy link
Owner

Check out this new webapp to visualize the parameters https://basnijholt.github.io/adaptive-lighting/

adaptive-lighting

adaptive-lighting.mp4

@Tjomsen
Copy link
Author

Tjomsen commented Aug 9, 2023

This is so perfect <3! I'm soo n00b though. How do I install the beta version instead of the official version?

@basnijholt
Copy link
Owner

Check this reply by @danielbrunt57 #616 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants