【问题标题】:Creating a function to get values for an s-shaped curve创建一个函数来获取 s 形曲线的值
【发布时间】:2020-10-26 20:51:00
【问题描述】:

我想创建一个 pyhton 脚本,为连接到 Home Assistant 的一些飞利浦 Hue 灯模拟日出。

我想要实现的是遵循亮度和开尔文值的 sigmoid / s 形曲线。

我希望亮度从 1 变为 100 (%),开尔文值从 2500 变为 4000。

我当前的脚本以线性方式执行此操作:

#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)

任何用 s 形值而不是线性值设置亮度/开尔文的想法都值得赞赏。

【问题讨论】:

标签: python math home-assistant


【解决方案1】:

您可以简单地迭代最终的 df 并使用亮度和开尔文值,每个间隔休眠一分钟左右并调用您的 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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-26
    • 1970-01-01
    • 2020-06-09
    • 2020-02-13
    • 2017-03-14
    • 2013-11-30
    • 2017-12-18
    • 2016-01-07
    相关资源
    最近更新 更多