创建一个函数以获取S形曲线的值

问题描述

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

我要实现的是按照S形/ s形曲线绘制亮度和开尔文值。

S-shaped curve

我希望亮度从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,updatesecs)

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

解决方法

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

val someDF = Seq(
  (8,"abc"),(64,"sdf"),(-27,"yui")
).toDF("number","word")

def numberValidation(numberValidation:Any): Boolean=
numberValidation match{
case int :Integer => true
case _ => false}

def wordValidatoin(wordValidation:Any): Boolean=
wordValidation match{
case str: String => true
case _ => false}

enter image description here

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...