Yeelight and Hue lights - use same settings

Post Reply
bertbigb
Posts: 137
Joined: Thursday 13 August 2015 13:36
Target OS: NAS (Synology & others)
Domoticz version: beta
Location: Netherlands
Contact:

Yeelight and Hue lights - use same settings

Post by bertbigb » Tuesday 21 February 2017 13:45

I was asked to share my Lua script by LouiS22 in item http://www.domoticz.com/forum/viewtopic ... 82#p120482 Re: [Plugin now available in Beta] Help with Yeelight Bulb

The script below is what I found, combined, tried myself with the purpose

-- Lua script to set Philips Hue and Yeelights to the same Brightness, HUE and saturation parameters
-- First to adjust all the bulbs to the same values
-- Second to find out what values will suite me, debug = true and in the log file the commands are printed
-- copy paste the values you like and use them in your more "final" program

I hope this will others help also

I created a few "virtual" dimmer switches
Dimmer Switches.png
Dimmer Switches.png (256.5 KiB) Viewed 3283 times
and a selector switch
Selector Switch.png
Selector Switch.png (150.71 KiB) Viewed 3283 times
In the code you see a lot of comment, I do this for myself to document the code with explanation


and this is the code for the script:

Code: Select all

-- Lights
-- Lua script to set Philips Hue and Yeelights to the same Brightness, HUE and saturation parameters
-- First to adjust all the bulbs to the same values
-- Second to find out what values will suite me, debug = true and in the log file the commands are printed
-- copy paste the ones you like and use them in your more "final" program


--[[ 
YEELIGHTS

    PART OF YEELIGHT MANUAL (for Reference pasted here)
    complete manual can be found at http://www.yeelight.com/download/Yeelight_Inter-Operation_Spec.pdf
    
    Method: set_hsv
    Usage: This method is used to change the color of a smart LED.
    Parameters: 4.
    "hue" is the target hue value, whose type is integer. It should be
    expressed in decimal integer ranges from 0 to 359.
    "sat" is the target saturation value whose type is integer. It's range is 0
    to 100.
    "effect": Refer to "set_ct_abx" method.

    "duration": Refer to "set_ct_abx" method.
    Request Example: {"id":1,"method":"set_hsv","params":[255, 45, "smooth", 500]}
    
    Method: set_bright
    Usage: This method is used to change the brightness of a smart LED.
    Parameters: 3.
    "brightness" is the target brightness. The type is integer and ranges
    from 1 to 100. The brightness is a percentage instead of a absolute value. 100 means
    maximum brightness while 1 means the minimum brightness.
    "effect": Refer to "set_ct_abx" method.
    "duration": Refer to "set_ct_abx" method.
    Request Example: {"id":1,"method":"set_bright","params":[50, "smooth", 500]}
    Response Example: {"id":1, "result":["ok"]}
    NOTE: Only accepted if the smart LED is currently in "on" state.
    
    Method: set_power
    Usage: This method is used to switch on or off the smart LED (software
    managed on/off).
    Parameters: 3.
    "power" can only be "on" or "off". "on" means turn on the smart LED,
    "off" means turn off the smart LED.
    "effect": Refer to "set_ct_abx" method.
    "duration": Refer to "set_ct_abx" method.
    Request Example: {"id":1,"method":"set_power","params":["on", "smooth", 500]}
    Response Example: {"id":1, "result":["ok"]}
    
    Method: start_cf
    Usage: This method is used to start a color flow. Color flow is a series of smart
    LED visible state changing. It can be brightness changing, color changing or color
    temperature changing. This is the most powerful command. All our recommended scenes,
    e.g. Sunrise/Sunset effect is implemented using this method. With the flow expression, user
    can actually “program” the light effect.
    Parameters: 3.
    "count" is the total number of visible state changing before color flow
    stopped. 0 means infinite loop on the state changing.
    "action" is the action taken after the flow is stopped.
    0 means smart LED recover to the state before the color flow started.
    1 means smart LED stay at the state when the flow is stopped.
    2 means turn off the smart LED after the flow is stopped.
    "flow_expression" is the expression of the state changing series.
    Request Example: {"id":1,"method":"start_cf","params":[ 4, 2, "1000, 2, 2700, 100, 500, 1,
    255, 10, 5000, 7, 0,0, 500, 2, 5000, 1"]
    Response Example: {"id":1, "result":["ok"]}
    NOTE: Each visible state changing is defined to be a flow tuple that contains 4
    elements: [duration, mode, value, brightness]. A flow expression is a series of flow tuples.
    So for above request example, it means: change CT to 2700K & maximum brightness
    gradually in 1000ms, then change color to red & 10% brightness gradually in 500ms, then
    stay at this state for 5 seconds, then change CT to 5000K & minimum brightness gradually in
    500ms. After 4 changes reached, stopped the flow and power off the smart LED.
    
    START CF Command explanation
    {"id":1,"method":"start_cf","params":[ 4, 2, "1000, 2, 2700, 100, 500, 1, 255, 10, 5000, 7, 0,0, 500, 2, 5000, 1"]
    Example explained
    {"id":1,"method":"start_cf", 	--start colour flow
    "params":[ 4, 					--count, after 4 changes stop
    2, 								--action, "action" is the action taken after the flow is stopped.
    										  0 means smart LED recover to the state before the color flow started.
    										  1 means smart LED stay at the state when the flow is stopped.
    										  2 means turn off the smart LED after the flow is stopped.
    								--the next values in pairs 0f 4 means
    								--[duration, mode, value, brightness].
    								--mode = COLOR_MODE": Current light mode. 1 means color mode, 2 means color temperature, mode, 3 means HSV mode.
    "1000,							--change from present state in 1000ms
    2, 								--color temperature mode
    2700, 							--color temperature 2700
    100, 							--brightness 100
    500, 1, 255, 10,     5000, 7, 0,0,      500, 2, 5000, 1"]

Philips HUE Lights
    usefull links:
    http://192.168.2.8/debug/clip.html
    https://developers.meethue.com/documentation/getting-started
    https://www.domoticz.com/forum/viewtopic.php?t=10493
	https://www.domoticz.com/wiki/Philips_Hue_Lights
    http://huelights.com/
    
    values:
    Sat = Saturation a value between 0 and 255
    Bri = Brightness a value between 0 and 255
    Hue = Hue, color a value between 0 and 65535

Yeelight Hue Lights
	usefull links:
	http://www.yeelight.com/download/Yeelight_Inter-Operation_Spec.pdf
	http://forum.yeelight.com/c/wonder
	https://www.domoticz.com/wiki/Yeelight
	
	
    values:
    Sat = Saturation a value between 0 and 100
    Bri = Brightness a value between 1 and 100
    Hue = Hue, color a value between 0 and 359

]]--
    
    --CHANGE BELOW FOR YOUR OWN NEEDS
    
    SelectorDevice1 = 'Lights-Selector'
    SelectorDevice2 = "Lights-Scene"
    DimDevice = 'Dimmer';
    TempDevice = 'WhiteTemp'
    HueDevice = 'Hue'
    SatDevice ='Sat'
    BriDevice ='Bri' 
    DurationDevice ='Duration' --here you set the duration of the Dimmer Cycle 100% = 100 seconds, 10% = 10 seconds
    
	--YEELIGHT
    -- port number for the yeelights
    PORT = '55443'
    --IP adresses of the bulbs involved, 
    local IPs = {'192.168.2.80', '192.168.2.81', '192.168.2.82', '192.168.2.83', '192.168.2.84', '192.168.2.85'}
	
	--PHILIPS HUE LIGHTS
	--ID of the Philips Hue of the bulbs involved; 1 = hanglamp, 3 = kamer-kast
	local BulB = {'1', '3'}
    bridge_ip_address ="192.168.2.8"
    hue_api_user = "2uZavYOxxxxxxxxxxxxTLGlHxxxxxxNGA"

    
    debug = true
	StartColorFlow = false
	synology = true
	
	
    
    function round(num, numDecimalPlaces)
      local mult = 10^(numDecimalPlaces or 0)
      return math.floor(num * mult + 0.5) / mult
    end
	
	function HueOff()
		-- command Philips Hue Off
		for n,bulb_id in pairs(BulB) do
			-- concatenate the http part of the command for HUE
			http = " http://" .. bridge_ip_address .. "/api/" .. hue_api_user .. "/lights/" ..bulb_id.."/state"
			runcommandHue = "curl --request PUT --data '{\"on\":false}'" 
			os.execute(runcommandHue..http)
		end
	end
	
	function YeelightOff()
		--command Yeelight Off
		for n,IP in pairs(IPs) do
			runcommandYeelight = "sudo echo -ne '{\"id\":"..n..",\"method\":\"set_power\", \"params\":[\"off\", \"smooth\", 500]}\\r\\n' | /opt/bin/nc -w1 " ..IP.." " ..PORT.."";
			os.execute(runcommandYeelight)
		end
    end		

	function HueOn()
		-- command Philips Hue Off
		for n,bulb_id in pairs(BulB) do
			-- concatenate the http part of the command for HUE
			http = " http://" .. bridge_ip_address .. "/api/" .. hue_api_user .. "/lights/" ..bulb_id.."/state"
			runcommandHue = "curl --request PUT --data '{\"on\":true}'" 
			os.execute(runcommandHue..http)
		end
	end
	
	function YeelightOn()
		--command Yeelight Off
		for n,IP in pairs(IPs) do
			runcommandYeelight = "sudo echo -ne '{\"id\":"..n..",\"method\":\"set_power\", \"params\":[\"on\", \"smooth\", 500]}\\r\\n' | /opt/bin/nc -w1 " ..IP.." " ..PORT.."";
			os.execute(runcommandYeelight)
		end
	end	

	function HueTemperature(WhiteHue, DimHue, DurationHue)
		--HUE part
		for n,bulb_id in pairs(BulB) do
			-- concatenate the http part of the command for HUE
			http = " http://" .. bridge_ip_address .. "/api/" .. hue_api_user .. "/lights/" ..bulb_id.."/state"
			--concatenate the command part of the command for HUE
			runcommandHue = "curl --request PUT --data '{\"on\":true, \"transitiontime\":"..DurationHue..", \"ct\":" ..WhiteHue.. ", \"bri\":" ..DimHue .."}'"
			if debug then print(runcommandHue..http) end
			os.execute(runcommandHue..http)
		end
	end
	
	function YeelightTemperature(WhiteYee, DimYee, DurationYee)
		--Yeelight part
		for n,IP in pairs(IPs) do
		    --runcommandYeelight = "sudo echo -ne '{\"id\":1,\"method\":\"set_scene\",\"params\":[\"ct\", " .. WhiteYee .. "," .. DimYee .. "]}\\r\\n' | nc -w1 " ..IP.." " ..PORT.."";
			--runcommandYeelight = "sudo echo -ne '{\"id\":1,\"method\":\"set_ct_abx\",\"params\":[" .. WhiteYee .. ",\"smooth\","..DurationYee.."]}\\r\\n' | nc -w1 " ..IP.." " ..PORT.."";
			runcommandYeelight ="sudo echo -ne '{\"id\":"..n..",\"method\":\"start_cf\",\"params\":[1,1,\"" ..DurationYee..",2,"..WhiteYee..","..DimYee.."\"]}\\r\\n' | /opt/bin/nc -w1 " ..IP.." " ..PORT.."";
			if debug then print(runcommandYeelight) end
			os.execute(runcommandYeelight)
		end
	end	
	
	
	function HueHue(HueHue, SatHue, BriHue, DurationHue)
		for n,bulb_id in pairs(BulB) do
			-- concatenate the http part of the command for HUE
			http = " http://" .. bridge_ip_address .. "/api/" .. hue_api_user .. "/lights/" ..bulb_id.."/state"
			--concatenate the command part of the command for HUE
			runcommandHue = "curl --request PUT --data '{\"on\":true, \"transitiontime\":"..DurationHue..", \"sat\":" ..SatHueValue.. ", \"bri\":" ..BriHueValue ..",\"hue\":" ..HueHueValue .."}'"
			if debug then print(runcommandHue..http) end 
			os.execute(runcommandHue..http)
		end
	end	
	
	function YeeHue(HueYee, SatYee, BriYee, DurationYee)
		--Yeelight part
		for n,IP in pairs(IPs) do
			if StartColorFlow then
				runcommandYeelight = "sudo echo -ne '{\"id\":"..n..", \"method\":\"start_cf\", \"params\":[1,1,\"" ..DurationYee..",3,".. HueYee .. "," .. SatYee ..","..BriYee.."\"]}\\r\\n' | /opt/bin/nc -w1 " ..IP.." " ..PORT.."";
				if debug then print(runcommandYeelight) end
				os.execute(runcommandYeelight)
			else	
				--since color flow on Yeelight for HSV mode is not working, we need to send 2 commands, 1 for the hue and satuartion and 1 for brightness
				
				--command for the Yeelight Hue Example: {"id":1,"method":"set_hsv","params":[255, 45, "smooth", 500]}  --> hue, sat, effect, duration
				runcommandYeelight = "sudo echo -ne '{\"id\":"..n..", \"method\":\"set_hsv\", \"params\":[" .. HueYee .. ", " .. SatYee .. ", \"smooth\", "..DurationYee.."]}\\r\\n' | /opt/bin/nc -w1 " ..IP.." " ..PORT.."";
				if debug then print(runcommandYeelight) end
				os.execute(runcommandYeelight)
				
				--command for the Yeelight brightness Example: {"id":1,"method":"set_bright","params":[50, "smooth", 500]} --> brightness, effect, duration
				runcommandYeelight = "sudo echo -ne '{\"id\":"..n..", \"method\":\"set_bright\", \"params\":[" .. BriYee ..  ", \"smooth\", "..DurationYee.."]}\\r\\n' | /opt/bin/nc -w1 " ..IP.." " ..PORT.."";
				if debug then print(runcommandYeelight) end
				os.execute(runcommandYeelight)
			end	

		end
	end	
	
	
commandArray = {}
    
    --see if one of the sliders was changed
    if devicechanged[HueDevice] or devicechanged[SatDevice] or devicechanged[BriDevice] or devicechanged[TempDevice] or devicechanged[DimDevice] or devicechanged[DurationDevice]then 
        
        --there are changes in the settings, so estimate the values for each type of the bulbs
	    TempValue = otherdevices_svalues[TempDevice];
	    BriValue = otherdevices_svalues[BriDevice]
	    SatValue = otherdevices_svalues[SatDevice]
	    HueValue = otherdevices_svalues[HueDevice]
	    DimValue = otherdevices_svalues[DimDevice]; 
	    DurationValue = otherdevices_svalues[DurationDevice];
		
	    --to prevent errors for nil values when initiated
        if TempValue==nil then TempValue=0 end
        if BriValue==nil then BriValue=0 end
	    if SatValue==nil then SatValue=0 end
	    if HueValue==nil then HueValue=0 end
	    if DimValue==nil then DimValue=0 end
	    if DurationValue==nil then DurationValue=0 end
	    if debug then print("Temp = " ..TempValue.." Dim = "..DimValue.." Bri = "..BriValue.." Sat = "..SatValue.." Hue = "..HueValue.." Duration = "..DurationValue) end
	    
	    --determine the duration of the transation, 100% = 100 seconds, 10% = 10 seconds, 1% = 1 sec  
        if DurationValue == 0 or DurationValue == nil then
            DurationHueValue = 4    --standard transation time Hue = 400 ms
            DurationYeeValue = 400  -- make Yeelights have the same value as Philips Hue Lights
        else
            DurationHueValue = DurationValue * 10       -- Hue Transation time is defined in 0,1 sec
            DurationYeeValue = DurationValue * 1000     -- Yee transation time is in ms = 0,001 sec
        end
        
        --color changes
        BriHueValue = round(tonumber(BriValue * 2.55),0);
        SatHueValue = round(tonumber(SatValue * 2.55),0);
        HueHueValue = round(tonumber(HueValue * 655.35),0);
        
        BriYeeValue = tonumber(BriValue)
        SatYeeValue = tonumber(SatValue)
        HueYeeValue = round(tonumber(HueValue * 3.59),0);
        
        --white changes
        --Yeelight Temp between 1700 and 6500 Kelvin
        WhiteYeeValue = ((TempValue-1) * 48)+1700;
        DimYeeValue =  tonumber(DimValue)
        
        --Huelight value between the warmest color 2000K is 500 mirek ("ct":500) and the coldest color 6500K is 153 mirek ("ct":153)
        WhiteHueValue = 500 - round(tonumber(TempValue * 3.47),0)
        DimHueValue = round(tonumber(DimValue * 2.55),0)
             
	    
        
        if (devicechanged[HueDevice]=='Off' or devicechanged[SatDevice]=='Off' or devicechanged[BriDevice]=='Off' or 
            devicechanged[TempDevice]=='Off'or devicechanged[DimDevice]=='Off') or 
		    (tonumber(otherdevices_svalues[BriDevice]) ==0 or tonumber(otherdevices_svalues[SatDevice])==0 or tonumber(otherdevices_svalues[HueDevice])==0 or 
		        tonumber(otherdevices_svalues[TempDevice])==0 or tonumber(otherdevices_svalues[DimDevice])==0 ) then 
            
			-- the bulb is Off
			HueOff()
			YeelightOff()
        end
		
		if devicechanged[TempDevice] or devicechanged[DimDevice] then
		    --there was change at Lighttemperature setting so we need to send that commands

			HueTemperature(WhiteHueValue, DimHueValue, DurationHueValue)
            YeelightTemperature(WhiteYeeValue, DimYeeValue, DurationYeeValue)   
			
		else    
		    --there were changes in the color settings so send the proper commands for that case

			HueHue(HueHueValue, SatHueValue, BriHueValue, DurationHueValue)
			YeeHue(HueYeeValue, SatYeeValue, BriYeeValue, DurationYeeValue)
	    end
   
    end

    if devicechanged[SelectorDevice1] then
        if otherdevices['HueSun'] == 'On' then commandArray['HueSun'] = 'Off' end    
        if otherdevices['Staande schemerlamp'] == 'Off' then commandArray['Staande schemerlamp'] = 'On' end
        HueOn()
        YeelightOn()
        print("selectorswitch")
        if otherdevices[SelectorDevice1] == "Energize" then 
            commandArray[TempDevice]='Set Level 99'
            commandArray[DimDevice]='Set Level 90'
        end
        if otherdevices[SelectorDevice1] == "Concentrate" then 
            commandArray[TempDevice]='Set Level 77'
            commandArray[DimDevice]='Set Level 80'
        end
        if otherdevices[SelectorDevice1] == "Reading" then 
            commandArray[TempDevice]='Set Level 44' 
            commandArray[DimDevice]='Set Level 70'
        end
        if otherdevices[SelectorDevice1] == "Normal" then 
            commandArray[TempDevice]='Set Level 37'
            commandArray[DimDevice]='Set Level 60'
        end
        if otherdevices[SelectorDevice1] == "Relax" then 
            commandArray[TempDevice]='Set Level 16'
            commandArray[DimDevice]='Set Level 10'
        end
        if otherdevices[SelectorDevice1] == "Movie" then 
            commandArray[TempDevice]='Set Level 100' 
            commandArray[DimDevice]='Set Level 30'
        end
        if otherdevices[SelectorDevice1] == "Romantic" then 
            commandArray[TempDevice]='Set Level 12' 
            commandArray[DimDevice]='Set Level 10'
        end
        --[[ some test results for later reference
        HUE SAT BRI - description
          4  60  42 - rose 
          7  65  19 - yellow-ish
          3  65  19 - orange /red
         10  65  19 - yellow/green 
         20  65  19 - green/yellow
         30  65  19 - green
         40  65  19 - green/blue
         50  65  19 - bue/green - light blue
         60  65  19 - blue
         70  65  19 - deep blue
         76  65  19 - night blue
         80  65  19 - purple
         90  65  19 - purple / red
        100  65  19 - red
        ]]--
        if otherdevices[SelectorDevice1] == "Warm" then 
            commandArray[HueDevice]='Set Level 4' 
            commandArray[SatDevice]='Set Level 60'
            commandArray[BriDevice]='Set Level 42'
        end
        if otherdevices[SelectorDevice1] == "NightBlue" then 
            commandArray[HueDevice]='Set Level 76' 
            commandArray[SatDevice]='Set Level 65'
            commandArray[BriDevice]='Set Level 19'
        end
        if otherdevices[SelectorDevice1] == "Warm-Dimmed" then 
            commandArray[HueDevice]='Set Level 99' 
            commandArray[SatDevice]='Set Level 65'
            commandArray[BriDevice]='Set Level 19'
        end
        if otherdevices[SelectorDevice1] == "Off" then 
        	HueOff()
			YeelightOff()
		end
    end


return commandArray


Best regards Bert

Synology DS1515+ - DSM 6.1
Raspberry PI2 - B, Raspberry Nano - Raspberry PI3 - model B
Xiaomi Gateway - Philips HUE Lights - Zwave - RFXCom(E) with KaKu and other 433MHz devices - Yeelight Lights - Toon

hekm77
Posts: 46
Joined: Thursday 09 February 2017 19:31
Target OS: Raspberry Pi
Domoticz version: beta
Contact:

Re: Yeelight and Hue lights - use same settings

Post by hekm77 » Friday 24 February 2017 8:48

Тhanks. I deleted "Hue line" from code. Works fine only for Yeelight.

bertbigb
Posts: 137
Joined: Thursday 13 August 2015 13:36
Target OS: NAS (Synology & others)
Domoticz version: beta
Location: Netherlands
Contact:

Re: Yeelight and Hue lights - use same settings

Post by bertbigb » Friday 24 February 2017 9:19

hekm77 wrote:Тhanks. I deleted "Hue line" from code. Works fine only for Yeelight.
Nice to hear you could make use of the script. Enjoy!
Best regards Bert

Synology DS1515+ - DSM 6.1
Raspberry PI2 - B, Raspberry Nano - Raspberry PI3 - model B
Xiaomi Gateway - Philips HUE Lights - Zwave - RFXCom(E) with KaKu and other 433MHz devices - Yeelight Lights - Toon

User avatar
Derik
Posts: 2068
Joined: Friday 18 October 2013 23:33
Target OS: Raspberry Pi
Domoticz version: BETA
Location: Arnhem/Nijmegen Nederland
Contact:

Re: Yeelight and Hue lights - use same settings

Post by Derik » Saturday 07 October 2017 14:08

dear all.

Old post only...

Is there some one that can help me with a "simple"srcipt for the Yeelight.
I will try to make different scripts working:
http://4pda.ru/forum/index.php?showtopi ... 58350704-1
Sunset sunrise etc..
Only how do i use this codes?
a lua and then?
or?

Thanks
Xu4: Beta Extreme antenna RFXcomE,WU Fi Ping ip P1 Gen5 PVOutput Harmony HUE SolarmanPv OTG Winddelen Alive ESP Buienradar MySensors WOL Winddelen counting RPi: Beta SMAspot RFlinkTest Domoticz ...Different backups

damirch
Posts: 7
Joined: Wednesday 01 November 2017 10:01
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Yeelight and Hue lights - use same settings

Post by damirch » Wednesday 01 November 2017 10:03

hekm77 wrote:
Friday 24 February 2017 8:48
Тhanks. I deleted "Hue line" from code. Works fine only for Yeelight.
Hi, Can you please share your script?

Thanks.

hekm77
Posts: 46
Joined: Thursday 09 February 2017 19:31
Target OS: Raspberry Pi
Domoticz version: beta
Contact:

Re: Yeelight and Hue lights - use same settings

Post by hekm77 » Wednesday 01 November 2017 10:35


damirch
Posts: 7
Joined: Wednesday 01 November 2017 10:01
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Yeelight and Hue lights - use same settings

Post by damirch » Wednesday 01 November 2017 18:57

hekm77 wrote:
Wednesday 01 November 2017 10:35
Hi.
http://4pda.ru/forum/index.php?act=find ... 58660929-5
Thank you, script works.

But... everything is really slow, any change takes long time to complete and in Domoticz logs i see:

2017-11-01 17:55:58.435 Error: EventSystem: Warning!, lua script Yeelight-Scene has been running for more than 10 seconds

User avatar
EdwinK
Posts: 1148
Joined: Sunday 22 January 2017 22:46
Target OS: Raspberry Pi
Domoticz version: BETA
Location: Rhoon
Contact:

Re: Yeelight and Hue lights - use same settings

Post by EdwinK » Wednesday 01 November 2017 21:34

bertbigb wrote:
Tuesday 21 February 2017 13:45

a lot of things.
What do I use in the settings for the selector-switchs?
Running latest BETA on a Pi-3 | Toon Thermostat | RFxcom | Dashticz V2 |

hekm77
Posts: 46
Joined: Thursday 09 February 2017 19:31
Target OS: Raspberry Pi
Domoticz version: beta
Contact:

Re: Yeelight and Hue lights - use same settings

Post by hekm77 » Saturday 11 November 2017 1:29

damirch wrote:
Wednesday 01 November 2017 18:57
Thank you, script works.
But... everything is really slow, any change takes long time to complete and in Domoticz logs i see:
2017-11-01 17:55:58.435 Error: EventSystem: Warning!, lua script Yeelight-Scene has been running for more than 10 seconds
Updated the script to dzVents 2.3.0. Only for Yeelight RGB Bulb
Test it.
http://4pda.ru/forum/index.php?act=find ... 58660929-5

NegerT
Posts: 2
Joined: Tuesday 12 December 2017 10:48
Target OS: Windows
Domoticz version: 3.8153
Location: Eindhoven
Contact:

Re: Yeelight and Hue lights - use same settings

Post by NegerT » Tuesday 12 December 2017 15:08

For those still interested in this thread, I made some adjustments to the script to use it on my windows installation. Also added netcat and bash on windows to run it.
Removed all but one dimmer switch as well since I don't use them and made some changes in the way the Yeelights are turned on (tends to be faster).

Code: Select all

-- Lua script to set Philips Hue and Yeelights to the same Brightness, HUE and saturation parameters
-- Windows requirements: 
--      -netcat for windows
--      -bash enabled
--Fixed IP addresses for the Yeelights

--[[ FOR OWN REFERENCE
    values:
    Sat = Saturation a value between 0 and 100
    Bri = Brightness a value between 1 and 100
    Hue = Hue, color a value between 0 and 359
    
    HUE SAT BRI - description
      4  60  42 - rose 
      7  65  19 - yellow-ish
      3  65  19 - orange /red
     10  65  19 - yellow/green 
     20  65  19 - green/yellow
     30  65  19 - green
     40  65  19 - green/blue
     50  65  19 - bue/green - light blue
     60  65  19 - blue
     70  65  19 - deep blue
     76  65  19 - night blue
     80  65  19 - purple
     90  65  19 - purple / red
    100  65  19 - red
]]--


-------------------------------------------
--CHANGE BELOW FOR YOUR OWN NEEDS
-------------------------------------------    
    
debug = false
    
--[[        Needed Switches         ]]--
----------------------------------------
SelectorDevice1 = 'Livingroom Scene'
Dimmer1         = 'Livingroom dimmer'
---------------------------------------- 

    --[[        PHILIPS HUE LIGHTS         ]]--
    -------------------------------------------
	--ID of the Philips Hue bulbs involved
    local BulB = {'xxx','xxx', 'xxx'}
	--IP and API of Hue bridge

    bridge_ip_address ="xxx"
    hue_api_user = "xxx"
    -------------------------------------------
    
    --[[        YEELIGHT LIGHTS         ]]--
    ----------------------------------------
    -- port number for the yeelights
    PORT = '55443'
    
    --IP adresses of the bulbs involved, 
    local IPs = {'xxx', 'xxx', 'xxx'}
	----------------------------------------
    
  -------------------------------------------
--PROGRAM
-------------------------------------------

function round(num, numDecimalPlaces)
    local mult = 10^(numDecimalPlaces or 0)
    return math.floor(num * mult + 0.5) / mult
end

--[[            Yeelight commands           ]]--
------------------------------------------------
function YeelightOff()
    local Yeelight_command={}
    Yeelight_command_total = ""
        
	for n,IP in pairs(IPs) do
	    Yeelight_command[n] = "C:\\Windows\\Sysnative\\bash.exe -c \"sudo echo -ne '{\\\"id\\\":"..n..",\\\"method\\\":\\\"set_power\\\",\\\"params\\\":[\\\"off\\\"]}\\\\r\\\\n' | nc -w1 " ..IP.." " ..PORT.."\"";
	    --Yeelight_command[n] = "C:\\Windows\\Sysnative\\bash.exe -c \"sudo echo -ne '{\\\"id\\\":"..n..",\\\"method\\\":\\\"toggle\\\",\\\"params\\\":[]}\\\\r\\\\n' | nc -w1 " ..IP.." " ..PORT.."\"";
        print("Yeelight "..n.." turned off")		    
	    Yeelight_command_total = Yeelight_command_total..Yeelight_command[n].." & "
	end
	if debug then 
        print(Yeelight_command_total);
    end
    os.execute(Yeelight_command_total);
end 
    
function YeelightTemperature(WhiteYeeValue, DimYeeValue, DurationYeeValue)
    local Yeelight_command={}
    Yeelight_command_total = ""
    
	for n,IP in pairs(IPs) do
	    Yeelight_command[n] = "C:\\Windows\\Sysnative\\bash.exe -c \"sudo echo -ne '{\\\"id\\\":"..n..",\\\"method\\\":\\\"set_scene\\\",\\\"params\\\":[\\\"ct\\\","..WhiteYeeValue..","..DimYeeValue.."]}\\\\r\\\\n' | nc -w1 " ..IP.." " ..PORT.."\"";
	    print("Yeelight "..n.." turned on")
	    Yeelight_command_total = Yeelight_command_total..Yeelight_command[n].." & "
	end
	if debug then 
        print(Yeelight_command_total);
    end
    os.execute(Yeelight_command_total);
end
		
function YeelightHue(BriYeeValue, SatYeeValue, HueYeeValue, DurationYeeValue)
    local Yeelight_command={}
    Yeelight_command_total = ""
    
    for n,IP in pairs(IPs) do
        Yeelight_command[n]= "C:\\Windows\\Sysnative\\bash.exe -c \"sudo echo -ne '{\\\"id\\\":"..n..",\\\"method\\\":\\\"set_scene\\\",\\\"params\\\":[\\\"hsv\\\"," .. HueYeeValue .. "," .. SatYeeValue .. ","..BriYeeValue.."]}\\\\r\\\\n' | nc -w1 " ..IP.." " ..PORT.."\"";
        print("Yeelight "..n.." turned on")
        Yeelight_command_total = Yeelight_command_total..Yeelight_command[n].." & "
	end
	if debug then 
        print(Yeelight_command_total);
    end
    os.execute(Yeelight_command_total);
end
    
function YeelightBright(BriYeeValue)
   local Yeelight_command={}
    Yeelight_command_total = ""
    
    for n,IP in pairs(IPs) do
        Yeelight_command[n]= "C:\\Windows\\Sysnative\\bash.exe -c \"sudo echo -ne '{\\\"id\\\":"..n..",\\\"method\\\":\\\"set_bright\\\",\\\"params\\\":["..BriYeeValue..",\\\"smooth\\\", 500]}\\\\r\\\\n' | nc -w1 " ..IP.." " ..PORT.."\"";
        print("Yeelight "..n.." changed brightness")
        Yeelight_command_total = Yeelight_command_total..Yeelight_command[n].." & "
	end
	if debug then 
        print(Yeelight_command_total);
    end
    os.execute(Yeelight_command_total); 
end
------------------------------------------------
	
	
--[[            Hue light commands           ]]--
-------------------------------------------------
function HueOff()
	for n,bulb_id in pairs(BulB) do
		http = " http://" .. bridge_ip_address .. "/api/" .. hue_api_user .. "/lights/" ..bulb_id.."/state "
	    runcommandHue = "curl -H \"Accept: application/json\" -X PUT -d \"{\\\"on\\\":false}\""
	    
        print("Huelight "..bulb_id.." turned off")
        if debug then 
            print(runcommandHue..http);
        end
		os.execute(runcommandHue..http.."");
	end
end
		
function HueTemperature(WhiteHueValue, DimHueValue, DurationHueValue)
	for n,bulb_id in pairs(BulB) do
		http = " http://" .. bridge_ip_address .. "/api/" .. hue_api_user .. "/lights/" ..bulb_id.."/state "
		runcommandHue = "curl -H \"Accept: application/json\" -X PUT -d \"{\\\"on\\\":true,\\\"transitiontime\\\":"..DurationHueValue..",\\\"ct\\\":" ..WhiteHueValue.. ",\\\"bri\\\":" ..DimHueValue .."}\""
        print("Huelight "..bulb_id.." turned on")
        if debug then 
            print(runcommandHue..http);
        end
		os.execute(runcommandHue..http.."");
	end
end
	
function HueHue(BriHueValue, SatHueValue, HueHueValue, DurationHueValue)
	for n,bulb_id in pairs(BulB) do
		http = " http://" .. bridge_ip_address .. "/api/" .. hue_api_user .. "/lights/" ..bulb_id.."/state "
		runcommandHue = "curl -H \"Accept: application/json\" -X PUT -d \"{\\\"on\\\":true,\\\"transitiontime\\\":"..DurationHueValue..",\\\"sat\\\":" ..SatHueValue.. ",\\\"bri\\\":" ..BriHueValue ..",\\\"hue\\\":" ..HueHueValue .."}\""
        print("Huelight "..bulb_id.." turned on")
        if debug then 
            print(runcommandHue..http);
        end
		os.execute(runcommandHue..http.."");
	end
end
 
function HueBright(BriHueValue)
   for n,bulb_id in pairs(BulB) do
		http = " http://" .. bridge_ip_address .. "/api/" .. hue_api_user .. "/lights/" ..bulb_id.."/state "
		runcommandHue = "curl -H \"Accept: application/json\" -X PUT -d \"{\\\"bri\\\":" ..BriHueValue .."}\""
        print("Huelight "..bulb_id.." changed brightness")
        if debug then 
            print(runcommandHue..http);
        end
		os.execute(runcommandHue..http.."");
	end 
end
-------------------------------------------------
   
commandArray = {}
    if devicechanged[SelectorDevice1] then
    print("Livingroom lights scene was set to = "..devicechanged[SelectorDevice1])
        --Scene values
        DurationValue = 8;                                                  --value defined as x times 0,1 sec
        
        if otherdevices[SelectorDevice1] == "Energize" then 
            TempValue = 99;
	        DimValue = 90;
	        Method = 1;
        end
        if otherdevices[SelectorDevice1] == "Concentrate" then
            TempValue = 77;
	        DimValue = 80;
	        Method = 1;
        end
        if otherdevices[SelectorDevice1] == "Reading" then 
            TempValue = 44;
	        DimValue = 70;
	        Method = 1;
        end
        if otherdevices[SelectorDevice1] == "Normal" then 
            TempValue = 37;
	        DimValue = 60;
	        Method = 1;
        end
        if otherdevices[SelectorDevice1] == "Relax" then 
            TempValue = 16;
	        DimValue = 10;
	        Method = 1;
        end
        if otherdevices[SelectorDevice1] == "Movie" then 
            TempValue = 100;
	        DimValue = 30;
	        Method = 1;
        end
        if otherdevices[SelectorDevice1] == "Romantic" then 
            TempValue = 12;
	        DimValue = 10;
	        Method = 1;
        end
        if otherdevices[SelectorDevice1] == "Warm" then 
            BriValue = 42;
	        SatValue = 60;
	        HueValue = 4;
	        Method = 2;
        end
        if otherdevices[SelectorDevice1] == "NightBlue" then 
            BriValue = 19;
	        SatValue = 65;
	        HueValue = 76;
	        Method = 2;
        end
        if otherdevices[SelectorDevice1] == "Warm-Dimmed" then
            BriValue = 19;
	        SatValue = 65;
	        HueValue = 99;
	        Method = 2;
        end
        
        if otherdevices[SelectorDevice1] == "Off" then 
        	HueOff()
        	YeelightOff()
	    else
	        if BriValue==nil then BriValue = DimValue end
	        
            if TempValue==nil then TempValue=0 end
            if BriValue==nil then BriValue=0 end
	        if SatValue==nil then SatValue=0 end
	        if HueValue==nil then HueValue=0 end
	        if DimValue==nil then DimValue=0 end
	        if DurationValue==nil then DurationValue=0 end
	        
	        commandArray[Dimmer1]='Set Level '..BriValue
	        
	        WhiteHueValue = 500 - round(tonumber(TempValue * 3.47),0)
            DimHueValue = round(tonumber(DimValue * 2.55),0)                    --Huelight value between the warmest color 2000K is 500 mirek ("ct":500) and the coldest color 6500K is 153 mirek ("ct":153)
	        BriHueValue = round(tonumber(BriValue * 2.55),0);
            SatHueValue = round(tonumber(SatValue * 2.55),0);
            HueHueValue = round(tonumber(HueValue * 655.35),0);
            DurationHueValue = DurationValue                                    --Hue Transation time is defined in 0,1 sec
            
            WhiteYeeValue = ((TempValue-1) * 48)+1700;                          --Yeelight Temp between 1700 and 6500 Kelvin
            DimYeeValue =  tonumber(DimValue)
            BriYeeValue = tonumber(BriValue)
            SatYeeValue = tonumber(SatValue)
            HueYeeValue = round(tonumber(HueValue * 3.59),0);
            DurationYeeValue = DurationValue * 100                              --Yee transation time is in ms = 0,001 sec
            
            if Method == 1 then
                print("Temperature Value = " ..TempValue..", Dimmer Value = "..DimValue.."")
                HueTemperature(WhiteHueValue, DimHueValue, DurationHueValue)
                YeelightTemperature(WhiteYeeValue, DimYeeValue, DurationYeeValue)
            elseif Method == 2 then
                print("Brightness Value = "..BriValue..", Saturation Value = "..SatValue..", Hue Value = "..HueValue.."")
                HueHue(BriHueValue, SatHueValue, HueHueValue, DurationHueValue)
                YeelightHue(BriYeeValue, SatYeeValue, HueYeeValue, DurationYeeValue)
            end
        end
    end

    if devicechanged[Dimmer1] and Method == nill then
            BriValue = otherdevices_svalues[Dimmer1]
            BriYeeValue = tonumber(BriValue)
            BriHueValue = round(tonumber(BriValue * 2.55),0);
            print("Brightness = "..BriValue.."")
            HueBright(BriHueValue)
            YeelightBright(BriYeeValue)
    end
return commandArray

damirch
Posts: 7
Joined: Wednesday 01 November 2017 10:01
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Yeelight and Hue lights - use same settings

Post by damirch » Wednesday 10 January 2018 10:04

hekm77 wrote:
Saturday 11 November 2017 1:29
damirch wrote:
Wednesday 01 November 2017 18:57
Thank you, script works.
But... everything is really slow, any change takes long time to complete and in Domoticz logs i see:
2017-11-01 17:55:58.435 Error: EventSystem: Warning!, lua script Yeelight-Scene has been running for more than 10 seconds
Updated the script to dzVents 2.3.0. Only for Yeelight RGB Bulb
Test it.
http://4pda.ru/forum/index.php?act=find ... 58660929-5
Hello

I'm testing it but I don't know what to put here:

local Light = domoticz.devices('YeeVisoki') -- Yeelight Bulb

hekm77
Posts: 46
Joined: Thursday 09 February 2017 19:31
Target OS: Raspberry Pi
Domoticz version: beta
Contact:

Re: Yeelight and Hue lights - use same settings

Post by hekm77 » Wednesday 10 January 2018 10:19

local Light = domoticz.devices('Name_of_your_lamp_from_the_device_tab')

damirch
Posts: 7
Joined: Wednesday 01 November 2017 10:01
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Yeelight and Hue lights - use same settings

Post by damirch » Wednesday 10 January 2018 10:25

hekm77 wrote:
Wednesday 10 January 2018 10:19
local Light = domoticz.devices('Name_of_your_lamp_from_the_device_tab')
Hello,

Problem is I have 8 Yeelight lamps and if i put only one here i can turn off only that one lamp. How can I add all 8 lamps?

Thanks

hekm77
Posts: 46
Joined: Thursday 09 February 2017 19:31
Target OS: Raspberry Pi
Domoticz version: beta
Contact:

Re: Yeelight and Hue lights - use same settings

Post by hekm77 » Wednesday 10 January 2018 10:55

Code: Select all

local Lights = {'name1', 'name2', 'name3'}

for n, name in pairs(Lights) do
domoticz.devices(name).switchOn()
end

damirch
Posts: 7
Joined: Wednesday 01 November 2017 10:01
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Yeelight and Hue lights - use same settings

Post by damirch » Wednesday 10 January 2018 12:02

hekm77 wrote:
Wednesday 10 January 2018 10:55

Code: Select all

local Lights = {'name1', 'name2', 'name3'}

for n, name in pairs(Lights) do
domoticz.devices(name).switchOn()
end
Thanks. I guess I'm doing something wrong.

Can you please attach whole file?

Thank you very much for your effort!

hekm77
Posts: 46
Joined: Thursday 09 February 2017 19:31
Target OS: Raspberry Pi
Domoticz version: beta
Contact:

Re: Yeelight and Hue lights - use same settings

Post by hekm77 » Wednesday 10 January 2018 13:41


damirch
Posts: 7
Joined: Wednesday 01 November 2017 10:01
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Yeelight and Hue lights - use same settings

Post by damirch » Wednesday 10 January 2018 13:57

hekm77 wrote:
Wednesday 10 January 2018 13:41
Sorry, I can not help with this.

http://www.domoticz.com/wiki/DzVents:_n ... _scripting
http://www.domoticz.com/wiki/LUA_commands
Ok, Thanks.

I'm switching to older version which works for me. Hopefully someone in the future will post working script.

My scripting knowlegde is limited :)

Anyway, thank you!

damirch
Posts: 7
Joined: Wednesday 01 November 2017 10:01
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Yeelight and Hue lights - use same settings

Post by damirch » Wednesday 10 January 2018 17:05

Anybody who can fix this for me? I'll pay :)

Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests