Yeelight LUA script for multiple IPs (with example)

Post Reply
itjensen
Posts: 4
Joined: Wednesday 03 October 2018 11:43
Target OS: Raspberry Pi
Domoticz version:
Contact:

Yeelight LUA script for multiple IPs (with example)

Post by itjensen » Wednesday 03 October 2018 12:11

How do I add multiple IP (Yeelight lamps) to LUA script?

I followed this guide: https://www.domoticz.com/wiki/Yeelight
and manage to create working switches and dimmers.

In DZVents script example I can controll all my yeelight bulbs at once with

Code: Select all

port = '55443'
yeelights = {'192.168.1.30', '192.168.1.31', '192.168.1.32', '192.168.1.33', '192.168.1.34', '192.168.1.35', '192.168.1.36', '192.168.1.37', '192.168.1.38', '192.168.1.39'}
How can I do the same in LUA?
I dont know programming however I have found examples on this forum and made this working LUA/yeelight scene selector. Is it possible to make this LUA script with a single runcommand for a list of IPs instead of repeating every IP address for each scene?

Code: Select all

commandArray = {}
DomDevice = 'Yeelight Scene';
IP = '192.168.1.30';
PORT = '55443'
 
if devicechanged[DomDevice]=='Off' then -- turn off
     runcommandoff0 = "sudo echo -ne '{\"id\":1,\"method\":\"set_power\", \"params\":[\"off\", \"smooth\", 500]}\\r\\n' | nc -w1 192.168.1.30 " ..PORT.."";
     runcommandoff1 = "sudo echo -ne '{\"id\":1,\"method\":\"set_power\", \"params\":[\"off\", \"smooth\", 500]}\\r\\n' | nc -w1 192.168.1.31 " ..PORT.."";
     runcommandoff2 = "sudo echo -ne '{\"id\":1,\"method\":\"set_power\", \"params\":[\"off\", \"smooth\", 500]}\\r\\n' | nc -w1 192.168.1.32 " ..PORT.."";
     runcommandoff3 = "sudo echo -ne '{\"id\":1,\"method\":\"set_power\", \"params\":[\"off\", \"smooth\", 500]}\\r\\n' | nc -w1 192.168.1.33 " ..PORT.."";
     runcommandoff4 = "sudo echo -ne '{\"id\":1,\"method\":\"set_power\", \"params\":[\"off\", \"smooth\", 500]}\\r\\n' | nc -w1 192.168.1.34 " ..PORT.."";
     runcommandoff5 = "sudo echo -ne '{\"id\":1,\"method\":\"set_power\", \"params\":[\"off\", \"smooth\", 500]}\\r\\n' | nc -w1 192.168.1.35 " ..PORT.."";
     runcommandoff6 = "sudo echo -ne '{\"id\":1,\"method\":\"set_power\", \"params\":[\"off\", \"smooth\", 500]}\\r\\n' | nc -w1 192.168.1.36 " ..PORT.."";
     runcommandoff7 = "sudo echo -ne '{\"id\":1,\"method\":\"set_power\", \"params\":[\"off\", \"smooth\", 500]}\\r\\n' | nc -w1 192.168.1.37 " ..PORT.."";
     runcommandoff8 = "sudo echo -ne '{\"id\":1,\"method\":\"set_power\", \"params\":[\"off\", \"smooth\", 500]}\\r\\n' | nc -w1 192.168.1.38 " ..PORT.."";
     runcommandoff9 = "sudo echo -ne '{\"id\":1,\"method\":\"set_power\", \"params\":[\"off\", \"smooth\", 500]}\\r\\n' | nc -w1 192.168.1.39 " ..PORT.."";
     os.execute(runcommandoff0);os.execute(runcommandoff1);os.execute(runcommandoff2);os.execute(runcommandoff3);os.execute(runcommandoff4)
     os.execute(runcommandoff5);os.execute(runcommandoff6);os.execute(runcommandoff7);os.execute(runcommandoff8);os.execute(runcommandoff9);
   --  print(runcommandoff) 

     elseif devicechanged[DomDevice]=='Kelvin 2700' then -- turn to 
     runcommandkelvin27000 = "sudo echo -ne '{\"id\":1,\"method\":\"set_scene\", \"params\":[\"ct\",2700,100]}\\r\\n' | nc -w1 192.168.1.30 "..PORT .." "; 
     runcommandkelvin27001 = "sudo echo -ne '{\"id\":1,\"method\":\"set_scene\", \"params\":[\"ct\",2700,100]}\\r\\n' | nc -w1 192.168.1.31 "..PORT .." "; 
     runcommandkelvin27002 = "sudo echo -ne '{\"id\":1,\"method\":\"set_scene\", \"params\":[\"ct\",2700,100]}\\r\\n' | nc -w1 192.168.1.32 "..PORT .." "; 
     runcommandkelvin27003 = "sudo echo -ne '{\"id\":1,\"method\":\"set_scene\", \"params\":[\"ct\",2700,100]}\\r\\n' | nc -w1 192.168.1.33 "..PORT .." "; 
     runcommandkelvin27004 = "sudo echo -ne '{\"id\":1,\"method\":\"set_scene\", \"params\":[\"ct\",2700,100]}\\r\\n' | nc -w1 192.168.1.34 "..PORT .." "; 
     runcommandkelvin27005 = "sudo echo -ne '{\"id\":1,\"method\":\"set_scene\", \"params\":[\"ct\",2700,100]}\\r\\n' | nc -w1 192.168.1.35 "..PORT .." "; 
     runcommandkelvin27006 = "sudo echo -ne '{\"id\":1,\"method\":\"set_scene\", \"params\":[\"ct\",2700,100]}\\r\\n' | nc -w1 192.168.1.36 "..PORT .." "; 
     runcommandkelvin27007 = "sudo echo -ne '{\"id\":1,\"method\":\"set_scene\", \"params\":[\"ct\",2700,100]}\\r\\n' | nc -w1 192.168.1.37 "..PORT .." "; 
     runcommandkelvin27008 = "sudo echo -ne '{\"id\":1,\"method\":\"set_scene\", \"params\":[\"ct\",2700,100]}\\r\\n' | nc -w1 192.168.1.38 "..PORT .." "; 
     runcommandkelvin27009 = "sudo echo -ne '{\"id\":1,\"method\":\"set_scene\", \"params\":[\"ct\",2700,100]}\\r\\n' | nc -w1 192.168.1.39 "..PORT .." "; 
     os.execute(runcommandkelvin27000);os.execute(runcommandkelvin27001);os.execute(runcommandkelvin27002);os.execute(runcommandkelvin27003);os.execute(runcommandkelvin27004);
     os.execute(runcommandkelvin27005);os.execute(runcommandkelvin27006);os.execute(runcommandkelvin27007);os.execute(runcommandkelvin27008);os.execute(runcommandkelvin27009);
   --  print(runcommandkelvin2700)  

     elseif devicechanged[DomDevice]=='Kelvin 4300' then -- turn to 
     runcommandkelvin43000 = "sudo echo -ne '{\"id\":1,\"method\":\"set_scene\", \"params\":[\"ct\",4300,100]}\\r\\n' | nc -w1 192.168.1.30 "..PORT .." "; 
     runcommandkelvin43001 = "sudo echo -ne '{\"id\":1,\"method\":\"set_scene\", \"params\":[\"ct\",4300,100]}\\r\\n' | nc -w1 192.168.1.31 "..PORT .." "; 
     runcommandkelvin43002 = "sudo echo -ne '{\"id\":1,\"method\":\"set_scene\", \"params\":[\"ct\",4300,100]}\\r\\n' | nc -w1 192.168.1.32 "..PORT .." "; 
     runcommandkelvin43003 = "sudo echo -ne '{\"id\":1,\"method\":\"set_scene\", \"params\":[\"ct\",4300,100]}\\r\\n' | nc -w1 192.168.1.33 "..PORT .." "; 
     runcommandkelvin43004 = "sudo echo -ne '{\"id\":1,\"method\":\"set_scene\", \"params\":[\"ct\",4300,100]}\\r\\n' | nc -w1 192.168.1.34 "..PORT .." "; 
     runcommandkelvin43005 = "sudo echo -ne '{\"id\":1,\"method\":\"set_scene\", \"params\":[\"ct\",4300,100]}\\r\\n' | nc -w1 192.168.1.35 "..PORT .." "; 
     runcommandkelvin43006 = "sudo echo -ne '{\"id\":1,\"method\":\"set_scene\", \"params\":[\"ct\",4300,100]}\\r\\n' | nc -w1 192.168.1.36 "..PORT .." "; 
     runcommandkelvin43007 = "sudo echo -ne '{\"id\":1,\"method\":\"set_scene\", \"params\":[\"ct\",4300,100]}\\r\\n' | nc -w1 192.168.1.37 "..PORT .." "; 
     runcommandkelvin43008 = "sudo echo -ne '{\"id\":1,\"method\":\"set_scene\", \"params\":[\"ct\",4300,100]}\\r\\n' | nc -w1 192.168.1.38 "..PORT .." "; 
     runcommandkelvin43009 = "sudo echo -ne '{\"id\":1,\"method\":\"set_scene\", \"params\":[\"ct\",4300,100]}\\r\\n' | nc -w1 192.168.1.39 "..PORT .." "; 
     os.execute(runcommandkelvin43000);os.execute(runcommandkelvin43001);os.execute(runcommandkelvin43002);os.execute(runcommandkelvin43003);os.execute(runcommandkelvin43004);
     os.execute(runcommandkelvin43005);os.execute(runcommandkelvin43006);os.execute(runcommandkelvin43007);os.execute(runcommandkelvin43008);os.execute(runcommandkelvin43009);
   --  print(runcommandkelvin4300)  
   
     elseif devicechanged[DomDevice]=='Kelvin 6500' then -- turn to 
     runcommandkelvin65000 = "sudo echo -ne '{\"id\":1,\"method\":\"set_scene\", \"params\":[\"ct\",6500,100]}\\r\\n' | nc -w1 192.168.1.30 "..PORT .." "; 
     runcommandkelvin65001 = "sudo echo -ne '{\"id\":1,\"method\":\"set_scene\", \"params\":[\"ct\",6500,100]}\\r\\n' | nc -w1 192.168.1.31 "..PORT .." "; 
     runcommandkelvin65002 = "sudo echo -ne '{\"id\":1,\"method\":\"set_scene\", \"params\":[\"ct\",6500,100]}\\r\\n' | nc -w1 192.168.1.32 "..PORT .." "; 
     runcommandkelvin65003 = "sudo echo -ne '{\"id\":1,\"method\":\"set_scene\", \"params\":[\"ct\",6500,100]}\\r\\n' | nc -w1 192.168.1.33 "..PORT .." "; 
     runcommandkelvin65004 = "sudo echo -ne '{\"id\":1,\"method\":\"set_scene\", \"params\":[\"ct\",6500,100]}\\r\\n' | nc -w1 192.168.1.34 "..PORT .." "; 
     runcommandkelvin65005 = "sudo echo -ne '{\"id\":1,\"method\":\"set_scene\", \"params\":[\"ct\",6500,100]}\\r\\n' | nc -w1 192.168.1.35 "..PORT .." "; 
     runcommandkelvin65006 = "sudo echo -ne '{\"id\":1,\"method\":\"set_scene\", \"params\":[\"ct\",6500,100]}\\r\\n' | nc -w1 192.168.1.36 "..PORT .." "; 
     runcommandkelvin65007 = "sudo echo -ne '{\"id\":1,\"method\":\"set_scene\", \"params\":[\"ct\",6500,100]}\\r\\n' | nc -w1 192.168.1.37 "..PORT .." "; 
     runcommandkelvin65008 = "sudo echo -ne '{\"id\":1,\"method\":\"set_scene\", \"params\":[\"ct\",6500,100]}\\r\\n' | nc -w1 192.168.1.38 "..PORT .." "; 
     runcommandkelvin65009 = "sudo echo -ne '{\"id\":1,\"method\":\"set_scene\", \"params\":[\"ct\",6500,100]}\\r\\n' | nc -w1 192.168.1.39 "..PORT .." "; 
     os.execute(runcommandkelvin65000);os.execute(runcommandkelvin65001);os.execute(runcommandkelvin65002);os.execute(runcommandkelvin65003);os.execute(runcommandkelvin65004);
     os.execute(runcommandkelvin65005);os.execute(runcommandkelvin65006);os.execute(runcommandkelvin65007);os.execute(runcommandkelvin65008);os.execute(runcommandkelvin65009);
   --  print(runcommandkelvin6500)  

   elseif devicechanged[DomDevice]=='Alarm' then -- turn to 
runcommandalarm0 = "sudo echo -ne '{\"id\":1,\"method\":\"set_scene\", \"params\":[\"cf\",0,0,\"300,1,16711680,1,50,1,222,1,50,1,3355647,100,50,1,222,1,50,1,16711680,100,50,1,222,1,50,1,3355647,100,50,1,222,1\"]}\r\n' | nc -w1 192.168.1.30 "..PORT .." ";
     runcommandalarm1 = "sudo echo -ne '{\"id\":1,\"method\":\"set_scene\", \"params\":[\"cf\",0,0,\"300,1,16711680,1,50,1,222,1,50,1,3355647,100,50,1,222,1,50,1,16711680,100,50,1,222,1,50,1,3355647,100,50,1,222,1\"]}\r\n' | nc -w1 192.168.1.31 "..PORT .." ";
     runcommandalarm2 = "sudo echo -ne '{\"id\":1,\"method\":\"set_scene\", \"params\":[\"cf\",0,0,\"300,1,16711680,1,50,1,222,1,50,1,3355647,100,50,1,222,1,50,1,16711680,100,50,1,222,1,50,1,3355647,100,50,1,222,1\"]}\r\n' | nc -w1 192.168.1.32 "..PORT .." ";
     runcommandalarm3 = "sudo echo -ne '{\"id\":1,\"method\":\"set_scene\", \"params\":[\"cf\",0,0,\"300,1,16711680,1,50,1,222,1,50,1,3355647,100,50,1,222,1,50,1,16711680,100,50,1,222,1,50,1,3355647,100,50,1,222,1\"]}\r\n' | nc -w1 192.168.1.33 "..PORT .." ";
     runcommandalarm4 = "sudo echo -ne '{\"id\":1,\"method\":\"set_scene\", \"params\":[\"cf\",0,0,\"300,1,16711680,1,50,1,222,1,50,1,3355647,100,50,1,222,1,50,1,16711680,100,50,1,222,1,50,1,3355647,100,50,1,222,1\"]}\r\n' | nc -w1 192.168.1.34 "..PORT .." ";
     runcommandalarm5 = "sudo echo -ne '{\"id\":1,\"method\":\"set_scene\", \"params\":[\"cf\",0,0,\"300,1,16711680,1,50,1,222,1,50,1,3355647,100,50,1,222,1,50,1,16711680,100,50,1,222,1,50,1,3355647,100,50,1,222,1\"]}\r\n' | nc -w1 192.168.1.35 "..PORT .." ";
     runcommandalarm6 = "sudo echo -ne '{\"id\":1,\"method\":\"set_scene\", \"params\":[\"cf\",0,0,\"300,1,16711680,1,50,1,222,1,50,1,3355647,100,50,1,222,1,50,1,16711680,100,50,1,222,1,50,1,3355647,100,50,1,222,1\"]}\r\n' | nc -w1 192.168.1.36 "..PORT .." ";
     runcommandalarm7 = "sudo echo -ne '{\"id\":1,\"method\":\"set_scene\", \"params\":[\"cf\",0,0,\"300,1,16711680,1,50,1,222,1,50,1,3355647,100,50,1,222,1,50,1,16711680,100,50,1,222,1,50,1,3355647,100,50,1,222,1\"]}\r\n' | nc -w1 192.168.1.37 "..PORT .." ";
     runcommandalarm8 = "sudo echo -ne '{\"id\":1,\"method\":\"set_scene\", \"params\":[\"cf\",0,0,\"300,1,16711680,1,50,1,222,1,50,1,3355647,100,50,1,222,1,50,1,16711680,100,50,1,222,1,50,1,3355647,100,50,1,222,1\"]}\r\n' | nc -w1 192.168.1.38 "..PORT .." ";
     runcommandalarm9 = "sudo echo -ne '{\"id\":1,\"method\":\"set_scene\", \"params\":[\"cf\",0,0,\"300,1,16711680,1,50,1,222,1,50,1,3355647,100,50,1,222,1,50,1,16711680,100,50,1,222,1,50,1,3355647,100,50,1,222,1\"]}\r\n' | nc -w1 192.168.1.39 "..PORT .." ";
     os.execute(runcommandalarm0);os.execute(runcommandalarm1);os.execute(runcommandalarm2);os.execute(runcommandalarm3);os.execute(runcommandalarm4);
     os.execute(runcommandalarm5);os.execute(runcommandalarm6);os.execute(runcommandalarm7);os.execute(runcommandalarm8);os.execute(runcommandalarm9);
   --  print(runcommandalarm) 

 elseif devicechanged[DomDevice]=='Sunrise RGB' then -- turn to 
     runcommandsunrisergb0 = "sudo echo -ne '{\"id\":1,\"method\":\"start_cf\", \"params\":[\"cf\",3,1,\"50,1,16731392,1,360000,2,1700,10,540000,2,2700,100\"]}\\r\\n' | nc -w1 192.168.1.30 "..PORT .." ";
     runcommandsunrisergb1 = "sudo echo -ne '{\"id\":1,\"method\":\"start_cf\", \"params\":[\"cf\",3,1,\"50,1,16731392,1,360000,2,1700,10,540000,2,2700,100\"]}\\r\\n' | nc -w1 192.168.1.31 "..PORT .." ";
     runcommandsunrisergb2 = "sudo echo -ne '{\"id\":1,\"method\":\"start_cf\", \"params\":[\"cf\",3,1,\"50,1,16731392,1,360000,2,1700,10,540000,2,2700,100\"]}\\r\\n' | nc -w1 192.168.1.32 "..PORT .." ";
     runcommandsunrisergb3 = "sudo echo -ne '{\"id\":1,\"method\":\"start_cf\", \"params\":[\"cf\",3,1,\"50,1,16731392,1,360000,2,1700,10,540000,2,2700,100\"]}\\r\\n' | nc -w1 192.168.1.33 "..PORT .." ";
     runcommandsunrisergb4 = "sudo echo -ne '{\"id\":1,\"method\":\"start_cf\", \"params\":[\"cf\",3,1,\"50,1,16731392,1,360000,2,1700,10,540000,2,2700,100\"]}\\r\\n' | nc -w1 192.168.1.34 "..PORT .." ";
     runcommandsunrisergb5 = "sudo echo -ne '{\"id\":1,\"method\":\"start_cf\", \"params\":[\"cf\",3,1,\"50,1,16731392,1,360000,2,1700,10,540000,2,2700,100\"]}\\r\\n' | nc -w1 192.168.1.35 "..PORT .." ";
     runcommandsunrisergb6 = "sudo echo -ne '{\"id\":1,\"method\":\"start_cf\", \"params\":[\"cf\",3,1,\"50,1,16731392,1,360000,2,1700,10,540000,2,2700,100\"]}\\r\\n' | nc -w1 192.168.1.36 "..PORT .." ";
     runcommandsunrisergb7 = "sudo echo -ne '{\"id\":1,\"method\":\"start_cf\", \"params\":[\"cf\",3,1,\"50,1,16731392,1,360000,2,1700,10,540000,2,2700,100\"]}\\r\\n' | nc -w1 192.168.1.37 "..PORT .." ";
     runcommandsunrisergb8 = "sudo echo -ne '{\"id\":1,\"method\":\"start_cf\", \"params\":[\"cf\",3,1,\"50,1,16731392,1,360000,2,1700,10,540000,2,2700,100\"]}\\r\\n' | nc -w1 192.168.1.38 "..PORT .." ";
     runcommandsunrisergb9 = "sudo echo -ne '{\"id\":1,\"method\":\"start_cf\", \"params\":[\"cf\",3,1,\"50,1,16731392,1,360000,2,1700,10,540000,2,2700,100\"]}\\r\\n' | nc -w1 192.168.1.39 "..PORT .." ";
     os.execute(runcommandsunrisergb0);os.execute(runcommandsunrisergb1);os.execute(runcommandsunrisergb2);os.execute(runcommandsunrisergb3);os.execute(runcommandsunrisergb4);
     os.execute(runcommandsunrisergb5);os.execute(runcommandsunrisergb6);os.execute(runcommandsunrisergb7);os.execute(runcommandsunrisergb8);os.execute(runcommandsunrisergb9);
   --  print(runcommandsunrisergb) 

    elseif devicechanged[DomDevice]=='Home RGB' then -- turn to 
     runcommandhomergb0 = "sudo echo -ne '{\"id\":1,\"method\":\"set_scene\", \"params\":[\"ct\",3200,80]}\\r\\n' | nc -w1 192.168.1.30 "..PORT .." ";
     runcommandhomergb1 = "sudo echo -ne '{\"id\":1,\"method\":\"set_scene\", \"params\":[\"ct\",3200,80]}\\r\\n' | nc -w1 192.168.1.31 "..PORT .." ";
     runcommandhomergb2 = "sudo echo -ne '{\"id\":1,\"method\":\"set_scene\", \"params\":[\"ct\",3200,80]}\\r\\n' | nc -w1 192.168.1.32 "..PORT .." ";
     runcommandhomergb3 = "sudo echo -ne '{\"id\":1,\"method\":\"set_scene\", \"params\":[\"ct\",3200,80]}\\r\\n' | nc -w1 192.168.1.33 "..PORT .." ";
     runcommandhomergb4 = "sudo echo -ne '{\"id\":1,\"method\":\"set_scene\", \"params\":[\"ct\",3200,80]}\\r\\n' | nc -w1 192.168.1.34 "..PORT .." ";
     runcommandhomergb5 = "sudo echo -ne '{\"id\":1,\"method\":\"set_scene\", \"params\":[\"ct\",3200,80]}\\r\\n' | nc -w1 192.168.1.35 "..PORT .." ";
     runcommandhomergb6 = "sudo echo -ne '{\"id\":1,\"method\":\"set_scene\", \"params\":[\"ct\",3200,80]}\\r\\n' | nc -w1 192.168.1.36 "..PORT .." ";
     runcommandhomergb7 = "sudo echo -ne '{\"id\":1,\"method\":\"set_scene\", \"params\":[\"ct\",3200,80]}\\r\\n' | nc -w1 192.168.1.37 "..PORT .." ";
     runcommandhomergb8 = "sudo echo -ne '{\"id\":1,\"method\":\"set_scene\", \"params\":[\"ct\",3200,80]}\\r\\n' | nc -w1 192.168.1.38 "..PORT .." ";
     runcommandhomergb9 = "sudo echo -ne '{\"id\":1,\"method\":\"set_scene\", \"params\":[\"ct\",3200,80]}\\r\\n' | nc -w1 192.168.1.39 "..PORT .." ";
     os.execute(runcommandhomergb0);os.execute(runcommandhomergb1);os.execute(runcommandhomergb2);os.execute(runcommandhomergb3);os.execute(runcommandhomergb4);
     os.execute(runcommandhomergb5);os.execute(runcommandhomergb6);os.execute(runcommandhomergb7);os.execute(runcommandhomergb8);os.execute(runcommandhomergb9);
   --  print(runcommandhomergb) 

     elseif devicechanged[DomDevice]=='Night Mode' then -- turn to 
     runcommandnightmode0 = "sudo echo -ne '{\"id\":1,\"method\":\"set_scene\", \"params\":[\"color\",16750848,1]}\\r\\n' | nc -w1 192.168.1.30 "..PORT .." ";
     runcommandnightmode1 = "sudo echo -ne '{\"id\":1,\"method\":\"set_scene\", \"params\":[\"color\",16750848,1]}\\r\\n' | nc -w1 192.168.1.31 "..PORT .." ";
     runcommandnightmode2 = "sudo echo -ne '{\"id\":1,\"method\":\"set_scene\", \"params\":[\"color\",16750848,1]}\\r\\n' | nc -w1 192.168.1.32 "..PORT .." ";
     runcommandnightmode3 = "sudo echo -ne '{\"id\":1,\"method\":\"set_scene\", \"params\":[\"color\",16750848,1]}\\r\\n' | nc -w1 192.168.1.33 "..PORT .." ";
     runcommandnightmode4 = "sudo echo -ne '{\"id\":1,\"method\":\"set_scene\", \"params\":[\"color\",16750848,1]}\\r\\n' | nc -w1 192.168.1.34 "..PORT .." ";
     runcommandnightmode5 = "sudo echo -ne '{\"id\":1,\"method\":\"set_scene\", \"params\":[\"color\",16750848,1]}\\r\\n' | nc -w1 192.168.1.35 "..PORT .." ";
     runcommandnightmode6 = "sudo echo -ne '{\"id\":1,\"method\":\"set_scene\", \"params\":[\"color\",16750848,1]}\\r\\n' | nc -w1 192.168.1.36 "..PORT .." ";
     runcommandnightmode7 = "sudo echo -ne '{\"id\":1,\"method\":\"set_scene\", \"params\":[\"color\",16750848,1]}\\r\\n' | nc -w1 192.168.1.37 "..PORT .." ";
     runcommandnightmode8 = "sudo echo -ne '{\"id\":1,\"method\":\"set_scene\", \"params\":[\"color\",16750848,1]}\\r\\n' | nc -w1 192.168.1.38 "..PORT .." ";
     runcommandnightmode9 = "sudo echo -ne '{\"id\":1,\"method\":\"set_scene\", \"params\":[\"color\",16750848,1]}\\r\\n' | nc -w1 192.168.1.39 "..PORT .." ";
     os.execute(runcommandnightmode0);os.execute(runcommandnightmode1);os.execute(runcommandnightmode2);os.execute(runcommandnightmode3);os.execute(runcommandnightmode4);
     os.execute(runcommandnightmode5);os.execute(runcommandnightmode6);os.execute(runcommandnightmode7);os.execute(runcommandnightmode8);os.execute(runcommandnightmode9);
   --  print(runcommandnightmode ) 

end
return commandArray

itjensen
Posts: 4
Joined: Wednesday 03 October 2018 11:43
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Yeelight LUA script for multiple IPs (with example)

Post by itjensen » Wednesday 03 October 2018 12:16

I tried with something like this and it is not working:

Code: Select all

commandArray = {}
DomDevice = 'YeelightTemperature';
IP = '192.168.1.30', '192.168.1.31', '192.168.1.32', '192.168.1.33';
PORT = '55443'

if devicechanged[DomDevice] then
   if(devicechanged[DomDevice]=='Off') then 
     DomValue = 0;
     runcommandoff = "sudo echo -ne '{\"id\":1,\"method\":\"set_power\", \"params\":[\"off\", \"smooth\", 500]}\\r\\n' | nc -w1 " ..IP.." " ..PORT.."";;
     os.execute(runcommandoff);
   else
     DomValue = otherdevices_svalues[DomDevice];   
   CalcValue = ((DomValue-1) * 48)+1700;
   end
   if CalcValue==nil then CalcValue=0 end
     runcommand = " sudo echo -ne '{\"id\":1, \"method\":\"set_scene\",\"params\":[\"ct\"," .. CalcValue .. ", 75]}\\r\\n' | nc -w1 " ..IP.." " ..PORT.."";
   os.execute(runcommand);
   print(runcommand)
   print("Color Temp= "..CalcValue);
end
return commandArray

User avatar
jvdz
Posts: 1447
Joined: Tuesday 30 December 2014 20:25
Target OS: Raspberry Pi
Domoticz version: Stable
Location: Westland/Netherlands
Contact:

Re: Yeelight LUA script for multiple IPs (with example)

Post by jvdz » Wednesday 03 October 2018 13:00

Does this help to get you going? Ask in case you need help understanding, but I've tried to break it up in logical functions.

Code: Select all

commandArray = {}
local DomDevice = 'YeelightTemperature';
local IPTab = {'192.168.1.30', '192.168.1.31', '192.168.1.32', '192.168.1.33'}
local PORT = '55443'
--
-- process all table entries
function ProcessAllLights(Status,Level)
  for seq,IP in pairs(IPTab) do
    switchYeelight(IP,Status,Level)
  end
end

--
-- switch Yeelight
function switchYeelight(IP,Status,Level)
  print(IP,Status,Level)
  if Status=='Switch' then -- turn On/off
     runcommand = "sudo echo -ne '{\"id\":1,\"method\":\"set_power\", \"params\":[\""..Level.."\", \"smooth\", 500]}\\r\\n' | nc -w1 " ..IP.." " ..PORT.." ";
  elseif Status =='Dimmer' then
    runcommand = "sudo echo -ne '{\"id\":1,\"method\":\"set_scene\", \"params\":[\"ct\"," .. Level .. ", 75]}\\r\\n' | nc -w1 " ..IP.." " ..PORT.." ";
  elseif Status =='Kelvin 2700' then -- turn to
    runcommand = "sudo echo -ne '{\"id\":1,\"method\":\"set_scene\", \"params\":[\"ct\",2700,100]}\\r\\n' | nc -w1 " ..IP.." " ..PORT.." ";
  elseif Status=='Kelvin 4300' then -- turn to
    runcommand = "sudo echo -ne '{\"id\":1,\"method\":\"set_scene\", \"params\":[\"ct\",4300,100]}\\r\\n' | nc -w1 " ..IP.." " ..PORT.." ";
  elseif Status=='Kelvin 6500' then -- turn to
    runcommand = "sudo echo -ne '{\"id\":1,\"method\":\"set_scene\", \"params\":[\"ct\",6500,100]}\\r\\n' | nc -w1 " ..IP.." " ..PORT.." ";
  elseif Status=='Alarm' then -- turn to
    runcommand = "sudo echo -ne '{\"id\":1,\"method\":\"set_scene\", \"params\":[\"cf\",0,0,\"300,1,16711680,1,50,1,222,1,50,1,3355647,100,50,1,222,1,50,1,16711680,100,50,1,222,1,50,1,3355647,100,50,1,222,1\"]}\r\n' | nc -w1 " ..IP.." " ..PORT.." ";
  elseif Status=='Sunrise RGB' then -- turn to
    runcommand = "sudo echo -ne '{\"id\":1,\"method\":\"start_cf\", \"params\":[\"cf\",3,1,\"50,1,16731392,1,360000,2,1700,10,540000,2,2700,100\"]}\\r\\n' | nc -w1 " ..IP.." " ..PORT.." ";
    elseif Status=='Home RGB' then -- turn to
    runcommand = "sudo echo -ne '{\"id\":1,\"method\":\"set_scene\", \"params\":[\"ct\",3200,80]}\\r\\n' | nc -w1 " ..IP.." " ..PORT.." ";
  elseif Status=='Night Mode' then -- turn to
    runcommand = "sudo echo -ne '{\"id\":1,\"method\":\"set_scene\", \"params\":[\"color\",16750848,1]}\\r\\n' | nc -w1 " ..IP.." " ..PORT.." ";
  end
  os.execute(runcommand)
  print(runcommand)
end

if devicechanged[DomDevice] then
  if(devicechanged[DomDevice]=="Off") then
    ProcessAllLights("Switch","Off")
  elseif(devicechanged[DomDevice]=="On") then
    ProcessAllLights("Switch","On")
  else
    DomValue = tonumber(otherdevices_svalues[DomDevice]);
    CalcValue = ((DomValue-1) * 48)+1700;
    if CalcValue==nil then CalcValue=0 end
    ProcessAllLights("Dimmer",CalcValue)
  end
end

return commandArray
Jos

itjensen
Posts: 4
Joined: Wednesday 03 October 2018 11:43
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Yeelight LUA script for multiple IPs (with example)

Post by itjensen » Wednesday 03 October 2018 13:26

Fantastic, thank you. This worked.
I hope your answer can be helpfull to others as I see the same type of question repeated on this forum.

User avatar
jvdz
Posts: 1447
Joined: Tuesday 30 December 2014 20:25
Target OS: Raspberry Pi
Domoticz version: Stable
Location: Westland/Netherlands
Contact:

Re: Yeelight LUA script for multiple IPs (with example)

Post by jvdz » Wednesday 03 October 2018 13:33

You are welcome. ;)
I did add all those other possibilities for status update to switchYeelight(), which were part of the other script you posted, so you can use those 2 functions for setting one or all Yeelights to any of those particular states options.

Jos

Boredcat
Posts: 46
Joined: Friday 17 March 2017 10:31
Target OS: Raspberry Pi
Domoticz version: 3.9639
Location: Netherlands
Contact:

Re: Yeelight LUA script for multiple IPs (with example)

Post by Boredcat » Wednesday 03 October 2018 13:58

I think doing this with dzvents is much easier.

Most of the yeelight function can be conttrolled using Dzvents..
some parts of my DzVents scripts

Code: Select all


     -- my yeelight (no IP needed, just the names)
     local RGBWW={}
	 RGBWW[1] = domoticz.devices('Lamp bank links')
	 RGBWW[2] = domoticz.devices('YeeLight LED Bank')
         RGBWW[3] = domoticz.devices('Lamp dressoir')
         RGBWW[4] = domoticz.devices('Lamp TV Links')
     local RGBWW_TOTAL = 4
     
     --  switching them all on
     
     for i = 1,RGBWW_TOTAL do
		     RGBWW[i].switchOn()
		     -- kelvin between 1701 and 6500
		     -- CalcValue = 100 - ((Kelvin -1700) / 48) 
		    -- Kelving value can be set RGBWW[i].setKelvin(CalcValue)
      end 
     
         
         

RFXCom(E) - Yeelight Lights - KaKu / Alexa HA Bridge
Windows 2012 / Windows 2012R2 / Xpenology/ Pi 3 / Milight / Home build ESP8266 / Sonoff

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest