2

I would like to create a pyhton script that simulates sunrise for some of my Philips Hue lights connected to Home Assistant.

What I'm trying to achieve is to follow a sigmoid / s-shaped curve for the brightness and kelvin values.

S-shaped curve

I would like the brightness to go from 1 to 100 (%), and the Kelvin values to go from 2500 to 4000.

My current script is doing this in a linear way:

#import time
def sunrise(entity_id, minutes, updatesecs=10, startbrightness=1, endbrightness=100, startkelvin=2500, endkelvin=4000):
    # Set current brightness and kelvin to the staring values
    currentbrightness=startbrightness
    currentkelvin=startkelvin
    # Calculate the needed iterations for the while loop
    numberofiterations=minutes*60/updatesecs
    kelvinincreasebyiteration=(endkelvin-startkelvin)/numberofiterations
    i=0
    while(i<=numberofiterations):
        # Set new brightness value
        currentbrightness = currentbrightness+endbrightness/numberofiterations
        currentkelvin = currentkelvin+kelvinincreasebyiteration
        if currentbrightness <= endbrightness:
            #print(round(currentbrightness)) # This value will be used for setting the brightness
            #print(round(currentkelvin))
            service_data = {"entity_id": entity_id, "kelvin": currentkelvin, "brightness_pct": currentbrightness, "transition": updatesecs-1}
            hass.services.call("light", "turn_on", service_data, False)

            time.sleep(updatesecs)
        else:
            break

entity_id = data.get("entity_id")
minutes = data.get("minutes")
updatesecs = data.get("updatesecs")

sunrise(entity_id,minutes,updatesecs)

Any ideas for setting the brightness/kelvin with s-shaped values instead of linear is appreciated.

Ôrel
  • 7,044
  • 3
  • 27
  • 46
ricmik
  • 21
  • 2
  • The [hyperbolic tangent](https://en.wikipedia.org/wiki/Hyperbolic_functions) function has a similar shape. [`math.tanh`](https://docs.python.org/3/library/math.html#math.tanh) – 0x5453 Oct 26 '20 at 21:01
  • Another classic is 1/(1 + exp(- x)). – Robert Dodier Oct 26 '20 at 21:10

1 Answers1

1

You could be able to simply iterate over the final df and use brightness and kelvin values, sleeping for a minute or so each interval and call your api.

import numpy as np
import seaborn as sns
import pandas as pd
import math
import matplotlib.pyplot as plt

def sigmoid(x):  
    return math.exp(-np.logaddexp(0, -x))

# You could change 60 to some other interval if you want
t = [(i,sigmoid(x)) for i,x in enumerate(np.linspace(-10,10,60))]
# df of time interval and y value
df = pd.DataFrame(t)


df.columns = ['time','sig']

# multiply sig by 100 to scale up to a percent for brightness
df['brightness'] = (df['sig'] * 100).astype(int)+1

# Scale sig values to 2500,4000 for kelvin
a = df.sig.values
df['kelvin'] = np.interp(a, (a.min(), a.max()), (2500, 4000)).astype(int)


fig, (ax1, ax2) = plt.subplots(ncols=2, sharey=False)
sns.lineplot(data=df,x='time',y='brightness', ax=ax1)
sns.lineplot(data=df,x='time',y='kelvin', ax=ax2)

enter image description here

Chris
  • 15,819
  • 3
  • 24
  • 37