xinqinew 发表于 2018-9-5 23:08:38

求助,改变灯的颜色和亮度,应该怎么填

求助,改变灯的颜色和亮度,应该怎么填

令狐鸣 发表于 2018-9-6 00:12:08

brightness是灯的亮度,0-255,可以去ha的服务里查看相应的json对应的key-value
```json
{
"entity_id": "light.xxx",
"brightness":"20"
}
```

xinqinew 发表于 2018-9-6 13:09:00

不知道为什么brightness不行,brightness_pct才行

xinqinew 发表于 2018-9-6 14:42:08

会弄了,原来最有效的办法是搞个函数。
newmsg = {};

brightness = msg.payload;

if (brightness <= 30){
    brightness = 60;
}
else if (brightness <= 60){
    brightness = 100;
}
else {
    brightness = 30;
}
// color_r=(Math.random()*255);
// color_g=(Math.random()*255);
// color_b=(Math.random()*255);
color_r=Math.floor(Math.random()*200);
color_g=Math.floor(Math.random()*200);
color_b=Math.floor(Math.random()*200);
newmsg.payload = { data: {"brightness_pct":brightness, "transition":0.5,"rgb_color": }};

return newmsg;

xieahui 发表于 2018-9-9 18:28:13

xinqinew 发表于 2018-9-6 14:42
会弄了,原来最有效的办法是搞个函数。
newmsg = {};



大神 能在下面把 nodered的小夜灯的代码贴出来吗?
nodered新手, 各项功能还不熟悉,刚好有小米网关,只会导入参考修改

xinqinew 发表于 2018-9-9 19:46:42

本帖最后由 xinqinew 于 2018-9-9 19:57 编辑

[{"id":"348df270.c8be9e","type":"api-current-state","z":"62c8c69.94b5c38","name":"小夜灯状态","server":"a9ceda93.3e29c","halt_if":"","override_topic":true,"override_payload":true,"entity_id":"light.gateway_light_34ce0088a3a0","x":910,"y":380,"wires":[["e9420951.db0558"]]},{"id":"e9420951.db0558","type":"switch","z":"62c8c69.94b5c38","name":"Light On/Off","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"on","vt":"str"},{"t":"eq","v":"off","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":1090,"y":380,"wires":[["ddc2dd00.7cd15"],["88fc503e.20e76"]]},{"id":"ddc2dd00.7cd15","type":"change","z":"62c8c69.94b5c38","name":"获取亮度","rules":[{"t":"set","p":"payload","pt":"msg","to":"data.attributes.brightness","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1240,"y":360,"wires":[["e0d5d93e.436eb"]]},{"id":"e0d5d93e.436eb","type":"range","z":"62c8c69.94b5c38","minin":"1","maxin":"255","minout":"1","maxout":"100","action":"clamp","round":true,"property":"payload","name":"亮度 %","x":1420,"y":360,"wires":[["d814cee5.037ca"]]},{"id":"182248db.142397","type":"api-call-service","z":"62c8c69.94b5c38","name":"小夜灯","server":"a9ceda93.3e29c","service_domain":"light","service":"turn_on","data":"{ \"entity_id\": \"light.gateway_light_34ce0088a3a0\"}","mergecontext":"","x":1790,"y":360,"wires":[[]]},{"id":"d814cee5.037ca","type":"function","z":"62c8c69.94b5c38","name":"亮度和颜色 Data","func":"newmsg = {};\n\nbrightness = msg.payload;\n\nif (brightness <= 30){\n    brightness = 60;\n}\nelse if (brightness <= 60){\n    brightness = 100;\n}\nelse {\n    brightness = 30;\n}\n// color_r=(Math.random()*255);\n// color_g=(Math.random()*255);\n// color_b=(Math.random()*255);\ncolor_r=Math.floor(Math.random()*200);\ncolor_g=Math.floor(Math.random()*200);\ncolor_b=Math.floor(Math.random()*200);\nnewmsg.payload = { data: {\"brightness_pct\":brightness, \"transition\":0.5,\"rgb_color\": }};\n\nreturn newmsg;","outputs":1,"noerr":0,"x":1620,"y":360,"wires":[["182248db.142397"]]},{"id":"88fc503e.20e76","type":"api-call-service","z":"62c8c69.94b5c38","name":"小夜灯","server":"a9ceda93.3e29c","service_domain":"light","service":"turn_on","data":"{ \"entity_id\": \"light.gateway_light_34ce0088a3a0\"}","mergecontext":"","x":1230,"y":400,"wires":[[]]},{"id":"72d7fbfa.b7483c","type":"inject","z":"62c8c69.94b5c38","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":750,"y":420,"wires":[["348df270.c8be9e"]]},{"id":"a9ceda93.3e29c","type":"server","z":"","name":"Home Assistant","url":"http://192.168.0.101:8123","pass":"12345678"}]

xieahui 发表于 2018-9-9 21:22:36

太感谢了,准备配合天气用

binghan 发表于 2018-11-17 22:20:12

好用,看看能不能利用在其他上面,嘿嘿

fire1399 发表于 2022-2-7 22:51:09

改變顏色寫法如圖如下所示,RGB顏色查表既可{ "rgb_color": }{
    "rgb_color": [
      255,
      100,
      100
    ]
}

rbsweethome 发表于 2022-10-27 20:34:25

謝謝分享
页: [1] 2
查看完整版本: 求助,改变灯的颜色和亮度,应该怎么填