首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >创建一个函数来获取s形曲线的值

创建一个函数来获取s形曲线的值
EN

Stack Overflow用户
提问于 2020-10-27 04:51:00
回答 1查看 127关注 0票数 2

我想创建一个pyhton脚本,模拟日出为我的一些飞利浦色调灯连接到家庭助理。

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

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

我当前的脚本是以线性方式完成此操作的:

代码语言:javascript
复制
#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形值而不是线性值来设置亮度/开尔文的任何想法。

EN

回答 1

Stack Overflow用户

发布于 2020-10-27 05:19:47

您可以简单地迭代最终的df并使用亮度和开尔文值,每个间隔休眠一分钟左右,然后调用您的api。

代码语言:javascript
复制
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)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64544790

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档