『瀚思彼岸』» 智能家居技术论坛

 找回密码
 立即注册
查看: 10668|回复: 11

[教程系列] 求助,改变灯的颜色和亮度,应该怎么填

[复制链接]

3

主题

219

帖子

932

积分

高级会员

Rank: 4

积分
932
金钱
713
HASS币
0
发表于 2018-9-5 23:08:38 | 显示全部楼层 |阅读模式
求助,改变灯的颜色和亮度,应该怎么填 屏幕快照 2018-09-05 下午11.06.18.png
回复

使用道具 举报

9

主题

787

帖子

3858

积分

论坛元老

Rank: 8Rank: 8

积分
3858
金钱
3071
HASS币
87
发表于 2018-9-6 00:12:08 | 显示全部楼层
brightness是灯的亮度,0-255,可以去ha的服务里查看相应的json对应的key-value
{
  "entity_id": "light.xxx",
"brightness":"20"
}

回复

使用道具 举报

3

主题

219

帖子

932

积分

高级会员

Rank: 4

积分
932
金钱
713
HASS币
0
 楼主| 发表于 2018-9-6 13:09:00 | 显示全部楼层
不知道为什么brightness不行,brightness_pct才行
回复

使用道具 举报

3

主题

219

帖子

932

积分

高级会员

Rank: 4

积分
932
金钱
713
HASS币
0
 楼主| 发表于 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":[color_r,color_g,color_b] }};

return newmsg;

屏幕快照 2018-09-06 下午2.39.35.png
屏幕快照 2018-09-06 下午2.40.46.png
回复

使用道具 举报

8

主题

2073

帖子

6053

积分

论坛元老

流水无味

Rank: 8Rank: 8

积分
6053
金钱
3980
HASS币
145

灌水之王

发表于 2018-9-9 18:28:13 | 显示全部楼层
xinqinew 发表于 2018-9-6 14:42
会弄了,原来最有效的办法是搞个函数。
[code]newmsg = {};

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

使用道具 举报

3

主题

219

帖子

932

积分

高级会员

Rank: 4

积分
932
金钱
713
HASS币
0
 楼主| 发表于 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":[color_r,color_g,color_b] }};\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"}]
回复

使用道具 举报

8

主题

2073

帖子

6053

积分

论坛元老

流水无味

Rank: 8Rank: 8

积分
6053
金钱
3980
HASS币
145

灌水之王

发表于 2018-9-9 21:22:36 | 显示全部楼层
太感谢了,准备配合天气用
回复

使用道具 举报

0

主题

33

帖子

141

积分

注册会员

Rank: 2

积分
141
金钱
108
HASS币
0
发表于 2018-11-17 22:20:12 | 显示全部楼层
好用,看看能不能利用在其他上面,嘿嘿
回复

使用道具 举报

2

主题

77

帖子

310

积分

中级会员

Rank: 3Rank: 3

积分
310
金钱
233
HASS币
0
发表于 2022-2-7 22:51:09 | 显示全部楼层
改變顏色寫法如圖如下所示,RGB顏色查表既可
{ "rgb_color":[255,100,100] }
{
    "rgb_color": [
        255,
        100,
        100
    ]
}


截圖 2022-02-07 下午10.46.01.png
回复

使用道具 举报

1

主题

29

帖子

623

积分

高级会员

Rank: 4

积分
623
金钱
594
HASS币
0
发表于 2022-10-27 20:34:25 | 显示全部楼层
謝謝分享
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|Hassbian

GMT+8, 2024-11-25 03:34 , Processed in 0.064585 second(s), 36 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表