Yeelights with selector Script not working  [Solved]

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
savage007
Posts: 19
Joined: Sunday 13 September 2015 20:24
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Yeelights with selector Script not working

Post by savage007 »

Hi, my script for controlling several yeelights through a selector is not working correct, can anybody help me?

I found this script on some russian forum ( https://translate.googleusercontent.com ... ry58660929 ) and adapted it with my own yeelights, but i dont realy know what i am doing :lol:

When i select off it only shuts down the firs light in " local Light = domoticz.devices('Yeelight bank links', 'Yeelight tv links', 'Yeelight lantaarn', 'Yeelight bank rechts', 'Yeelight tv rechts')"

Code: Select all

return {
    on = {
        devices = {
            'Lights-Selector',
            'Dimmer',
            'WhiteTemp',
            'Hue',
            'Sat',
            'Bri',
            'Duration'
        } 
    },
    logging = {
        level = domoticz.LOG_DEBUG
    },
    execute = function(domoticz, device)
        
         local Light  = domoticz.devices('Yeelight bank links', 'Yeelight tv links', 'Yeelight lantaarn', 'Yeelight bank rechts', 'Yeelight tv rechts') -- Yeelight Bulb
         local PORT   = '55443'
         local IPs    = {'192.168.179.88', '192.168.179.19','192.168.179.178','192.168.179.104','192.168.179.48'} -- group --> {'192.168.0.1', '192.168.0.2', '192.168.0.3'} -- group --> {'192.168.0.1', '192.168.0.2', '192.168.0.3'}
         local switch = domoticz.devices
         
         local sceneWhite = {}
          sceneWhite[#sceneWhite+1] = {'Energize', 99, 90}
          sceneWhite[#sceneWhite+1] = {'Concentrate', 77, 80} 
          sceneWhite[#sceneWhite+1] = {'Reading', 44, 70} 
          sceneWhite[#sceneWhite+1] = {'Normal', 37, 60} 
          sceneWhite[#sceneWhite+1] = {'Movie',  100, 30}
          sceneWhite[#sceneWhite+1] = {'Relax', 16, 10}
          sceneWhite[#sceneWhite+1] = {'Romantic', 12, 10}
          
         local sceneRGB = {} 
          sceneRGB[#sceneRGB+1] = {'Warm', 4, 60, 42}
          sceneRGB[#sceneRGB+1] = {'Warm-Dimmed', 99, 65, 19} 
          sceneRGB[#sceneRGB+1] = {'NightBlue',  76, 65, 19}
         
         debug =  true 
 
 function round(num, numDecimalPlaces)
    local mult = 10^(numDecimalPlaces or 0)
    return math.floor(num * mult + 0.5) / mult
 end
   
 function YeelightOff()
    Light.switchOff().checkFirst()
    switch('Dimmer').switchOff().checkFirst().silent()
    switch('WhiteTemp').switchOff().checkFirst().silent()
    switch('Hue').switchOff().checkFirst().silent()
    switch('Sat').switchOff().checkFirst().silent()
    switch('Bri').switchOff().checkFirst().silent()
    switch('Lights-Selector').switchOff().checkFirst().silent()
 end

 function YeelightTemperature(WhiteValue, DimValue, DurationYeeValue)
    Light.switchOn().checkFirst()
    for n, IP in pairs(IPs) do
       runcommandYeelight = "sudo echo -ne '{\"id\":1,\"method\":\"set_scene\",\"params\":[\"ct\", " .. WhiteValue .. "," .. DimValue .. "]}\\r\\n' | nc -w1 " ..IP.." " ..PORT..""
          if debug then print(runcommandYeelight) end
       os.execute(runcommandYeelight)
    end
    switch('Dimmer').switchOn().checkFirst().silent()
    switch('WhiteTemp').switchOn().checkFirst().silent()
    switch('Hue').switchOff().checkFirst().silent()
    switch('Sat').switchOff().checkFirst().silent()
    switch('Bri').switchOff().checkFirst().silent()
 end
   
 function YeeHue(HueValue, SatValue, BriValue, DurationYeeValue)
    Light.switchOn().checkFirst()
    for n, IP in pairs(IPs) do
       runcommandYeelight = "sudo echo -ne '{\"id\":"..n..", \"method\":\"set_hsv\", \"params\":[" .. HueValue .. ", " .. SatValue .. ", \"smooth\", "..DurationYeeValue.."]}\\r\\n' | nc -w1 " ..IP.." " ..PORT..""
          if debug then print(runcommandYeelight) end
       os.execute(runcommandYeelight)
            
       runcommandYeelight = "sudo echo -ne '{\"id\":"..n..", \"method\":\"set_bright\", \"params\":[" .. BriValue ..  ", \"smooth\", "..DurationYeeValue.."]}\\r\\n' | nc -w1 " ..IP.." " ..PORT..""
          if debug then print(runcommandYeelight) end
       os.execute(runcommandYeelight)
    end 
    switch('Hue').switchOn().checkFirst().silent()
    switch('Sat').switchOn().checkFirst().silent()
    switch('Bri').switchOn().checkFirst().silent()
    switch('Dimmer').switchOff().checkFirst().silent()
    switch('WhiteTemp').switchOff().checkFirst().silent()
 end  

        
        DimValue      = switch('Dimmer').level
        TempValue     = switch('WhiteTemp').level
        WhiteValue    = ((TempValue - 1) * 48) + 1700
        HueValue      = round(switch('Hue').level * 3.59),0
        SatValue      = switch('Sat').level
        BriValue      = switch('Bri').level
        DurationValue = switch('Duration').level
           if DimValue      == nil then DimValue = 0 end
           if TempValue     == nil then TempValue = 0 end
           if HueValue      == nil then HueValue = 0 end
           if SatValue      == nil then SatValue = 0 end
           if BriValue      == nil then BriValue = 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
           if DurationValue == 0 or DurationValue == nil then
              DurationYeeValue = 400
           else
              DurationYeeValue = DurationValue * 1000
           end
           if ((device.name == 'Dimmer' or device.name == 'WhiteTemp') and device.state ~= 'Off') then
                YeelightTemperature(WhiteValue, DimValue, DurationYeeValue)
           end
           if ((device.name == 'Hue' or device.name == 'Sat' or device.name == 'Bri') and device.state ~= 'Off') then
                YeeHue(HueValue, SatValue, BriValue, DurationYeeValue)
           end
           if ((device.name == 'Dimmer' or device.name == 'WhiteTemp' or device.name == 'Hue' or device.name == 'Sat' or device.name == 'Bri') and device.state == 'Off') then
                YeelightOff()
           end
           if (device.name == 'Lights-Selector') then
                  if (device.levelName == 'Off') then
                      YeelightOff()
                  end 
                  for i in pairs(sceneWhite) do
                     if (device.levelName == sceneWhite[i][1] ) then
                         switch('WhiteTemp').dimTo(sceneWhite[i][2])
                         switch('Dimmer').dimTo(sceneWhite[i][3])
                     end
                  end
                  for i in pairs(sceneRGB) do
                     if (device.levelName == sceneRGB[i][1] ) then
                         switch('Hue').dimTo(sceneRGB[i][2])
                         switch('Sat').dimTo(sceneRGB[i][3])
                         switch('Bri').dimTo(sceneRGB[i][4])
                     end
                  end

           end   
    end
}
This is the debug log

Code: Select all

2019-09-17 11:53:47.156 (Dummy) Light/Switch (Lights-Selector)
2019-09-17 11:53:47.641 (Yeelight) Color Switch (Yeelight bank links)
2019-09-17 11:53:47.653 (Dummy) Light/Switch (Dimmer)
2019-09-17 11:53:47.663 (Dummy) Light/Switch (WhiteTemp)
2019-09-17 11:53:47.155 Status: User: Admin initiated a switch command (108/Lights-Selector/Set Level)
2019-09-17 11:53:47.179 Status: dzVents: Info: Handling events for: "Lights-Selector", value: "Off"
2019-09-17 11:53:47.179 Status: dzVents: Info: ------ Start internal script: Yeelight: Device: "Lights-Selector (Dummy)", Index: 108
2019-09-17 11:53:47.179 Status: dzVents: Debug: Processing device-adapter for Yeelight bank links: RGB(W) device adapter
2019-09-17 11:53:47.179 Status: dzVents: Debug: Processing device-adapter for Yeelight bank links: Switch device adapter
2019-09-17 11:53:47.179 Status: dzVents: Debug: Processing device-adapter for Dimmer: Switch device adapter
2019-09-17 11:53:47.180 Status: dzVents: Debug: Processing device-adapter for WhiteTemp: Switch device adapter
2019-09-17 11:53:47.180 Status: dzVents: Debug: Processing device-adapter for Hue: Switch device adapter
2019-09-17 11:53:47.180 Status: dzVents: Debug: Processing device-adapter for Sat: Switch device adapter
2019-09-17 11:53:47.180 Status: dzVents: Debug: Processing device-adapter for Bri: Switch device adapter
2019-09-17 11:53:47.180 Status: dzVents: Debug: Processing device-adapter for Duration: Switch device adapter
2019-09-17 11:53:47.180 Status: dzVents: Temp = 12 Dim = 10 Bri = 0 Sat = 0 Hue = 0 Duration = 0
2019-09-17 11:53:47.180 Status: dzVents: Debug: Constructed timed-command: Off
2019-09-17 11:53:47.180 Status: dzVents: Debug: Constructed timed-command: Off
2019-09-17 11:53:47.180 Status: dzVents: Debug: Constructed timed-command: Off
2019-09-17 11:53:47.180 Status: dzVents: Debug: Constructed timed-command: Off
2019-09-17 11:53:47.180 Status: dzVents: Debug: Constructed timed-command: Off NOTRIGGER
2019-09-17 11:53:47.180 Status: dzVents: Debug: Constructed timed-command: Off
2019-09-17 11:53:47.180 Status: dzVents: Debug: Constructed timed-command: Off
2019-09-17 11:53:47.180 Status: dzVents: Debug: Constructed timed-command: Off NOTRIGGER
2019-09-17 11:53:47.180 Status: dzVents: Debug: Constructed timed-command: Off
2019-09-17 11:53:47.180 Status: dzVents: Debug: Constructed timed-command: Off
2019-09-17 11:53:47.180 Status: dzVents: Debug: Constructed timed-command: Off
2019-09-17 11:53:47.180 Status: dzVents: Debug: Constructed timed-command: Off
2019-09-17 11:53:47.180 Status: dzVents: Info: ------ Finished Yeelight
Last edited by savage007 on Tuesday 17 September 2019 12:34, edited 3 times in total.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Yeelights with selector Script not working  [Solved]

Post by waaren »

savage007 wrote: Tuesday 17 September 2019 9:26 Hi, my script for controlling several yeelights through a selector is not working, can anybody help me?
I found this script on some russian forum and adapted it with my own yeelights.
Kind of hard to understand from your problem description
is not working :)
and from the log (where everything seems to work) what the issue(s) is / are.

I guess your main problem is the use of

Code: Select all

local Light  = domoticz.devices('Yeelight bank links', 'Yeelight tv links', 'Yeelight lantaarn', 'Yeelight bank rechts', 'Yeelight tv rechts')
This will not work as you might expect in dzVents when used in an action like Light.switchOn(). Because domoticz.devices is in fact a function with 1 parameter it will ignore the other 'parameters' and only act on the first one; being 'Yeelight bank links'

I modified the script to overcome this. I only checked syntax and did not test functionality so if something is not working as expected then please describe it in more detail and add some debug lines in the area where your issue occur.

Code: Select all

return {
    on = {
        devices = {
            'Lights-Selector',
            'Dimmer',
            'WhiteTemp',
            'Hue',
            'Sat',
            'Bri',
            'Duration'
        } 
    },
    logging = {
        level = domoticz.LOG_DEBUG
    },

    execute = function(domoticz, device)
        local myLights  = { 'Yeelight bank links', 'Yeelight tv links', 'Yeelight lantaarn', 'Yeelight bank rechts', 'Yeelight tv rechts' }  -- Yeelight Bulb
        local PORT   = '55443'
        local IPs    = {'192.168.179.88', '192.168.179.19','192.168.179.178','192.168.179.104','192.168.179.48'} -- group --> {'192.168.0.1', '192.168.0.2', '192.168.0.3'}
        local switch = domoticz.devices
         
         local sceneWhite = {}
          sceneWhite[#sceneWhite+1] = {'Energize', 99, 90}
          sceneWhite[#sceneWhite+1] = {'Concentrate', 77, 80} 
          sceneWhite[#sceneWhite+1] = {'Reading', 44, 70} 
          sceneWhite[#sceneWhite+1] = {'Normal', 37, 60} 
          sceneWhite[#sceneWhite+1] = {'Movie',  100, 30}
          sceneWhite[#sceneWhite+1] = {'Relax', 16, 10}
          sceneWhite[#sceneWhite+1] = {'Romantic', 12, 10}
          
         local sceneRGB = {} 
          sceneRGB[#sceneRGB+1] = {'Warm', 4, 60, 42}
          sceneRGB[#sceneRGB+1] = {'Warm-Dimmed', 99, 65, 19} 
          sceneRGB[#sceneRGB+1] = {'NightBlue',  76, 65, 19}
         
         debug =  true 
 
		 function round(num, numDecimalPlaces)
			local mult = 10^(numDecimalPlaces or 0)
			return math.floor(num * mult + 0.5) / mult
		 end
	
		local function switchAll(deviceNames, action)
			if action == 'Off' then
				for _, name in ipairs(deviceNames) do
					switch(name).switchOff().checkFirst()
				end
			else
				for _, name in ipairs(deviceNames) do
					switch(name).switchOn().checkFirst()
				end
			end
		end
   
		 function YeelightOff()
			switchAll(myLights, 'Off' )
			switch('Dimmer').switchOff().checkFirst().silent()
			switch('WhiteTemp').switchOff().checkFirst().silent()
			switch('Hue').switchOff().checkFirst().silent()
			switch('Sat').switchOff().checkFirst().silent()
			switch('Bri').switchOff().checkFirst().silent()
			switch('Lights-Selector').switchOff().checkFirst().silent()
		 end

 function YeelightTemperature(WhiteValue, DimValue, DurationYeeValue)
    switchAll(myLights, 'On' )
    for n, IP in pairs(IPs) do
       runcommandYeelight = "sudo echo -ne '{\"id\":1,\"method\":\"set_scene\",\"params\":[\"ct\", " .. WhiteValue .. "," .. DimValue .. "]}\\r\\n' | nc -w1 " ..IP.." " ..PORT..""
          if debug then print(runcommandYeelight) end
       os.execute(runcommandYeelight)
    end
    switch('Dimmer').switchOn().checkFirst().silent()
    switch('WhiteTemp').switchOn().checkFirst().silent()
    switch('Hue').switchOff().checkFirst().silent()
    switch('Sat').switchOff().checkFirst().silent()
    switch('Bri').switchOff().checkFirst().silent()
 end
   
 function YeeHue(HueValue, SatValue, BriValue, DurationYeeValue)
    switchAll(myLights, 'On' )
    for n, IP in pairs(IPs) do
       runcommandYeelight = "sudo echo -ne '{\"id\":"..n..", \"method\":\"set_hsv\", \"params\":[" .. HueValue .. ", " .. SatValue .. ", \"smooth\", "..DurationYeeValue.."]}\\r\\n' | nc -w1 " ..IP.." " ..PORT..""
          if debug then print(runcommandYeelight) end
       os.execute(runcommandYeelight)
            
       runcommandYeelight = "sudo echo -ne '{\"id\":"..n..", \"method\":\"set_bright\", \"params\":[" .. BriValue ..  ", \"smooth\", "..DurationYeeValue.."]}\\r\\n' | nc -w1 " ..IP.." " ..PORT..""
          if debug then print(runcommandYeelight) end
       os.execute(runcommandYeelight)
    end 
    switch('Hue').switchOn().checkFirst().silent()
    switch('Sat').switchOn().checkFirst().silent()
    switch('Bri').switchOn().checkFirst().silent()
    switch('Dimmer').switchOff().checkFirst().silent()
    switch('WhiteTemp').switchOff().checkFirst().silent()
 end  

        
        DimValue      = switch('Dimmer').level
        TempValue     = switch('WhiteTemp').level
        WhiteValue    = ((TempValue - 1) * 48) + 1700
        HueValue      = domoticz.utils.round((switch('Hue').level * 3.59),0)
        SatValue      = switch('Sat').level
        BriValue      = switch('Bri').level
        DurationValue = switch('Duration').level
           if DimValue      == nil then DimValue = 0 end
           if TempValue     == nil then TempValue = 0 end
           if HueValue      == nil then HueValue = 0 end
           if SatValue      == nil then SatValue = 0 end
           if BriValue      == nil then BriValue = 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
           if DurationValue == 0 or DurationValue == nil then
              DurationYeeValue = 400
           else
              DurationYeeValue = DurationValue * 1000
           end
           if ((device.name == 'Dimmer' or device.name == 'WhiteTemp') and device.state ~= 'Off') then
                YeelightTemperature(WhiteValue, DimValue, DurationYeeValue)
           end
           if ((device.name == 'Hue' or device.name == 'Sat' or device.name == 'Bri') and device.state ~= 'Off') then
                YeeHue(HueValue, SatValue, BriValue, DurationYeeValue)
           end
           if ((device.name == 'Dimmer' or device.name == 'WhiteTemp' or device.name == 'Hue' or device.name == 'Sat' or device.name == 'Bri') and device.state == 'Off') then
                YeelightOff()
           end
           if (device.name == 'Lights-Selector') then
                  if (device.levelName == 'Off') then
                      YeelightOff()
                  end 
                  for i in pairs(sceneWhite) do
                     if (device.levelName == sceneWhite[i][1] ) then
                         switch('WhiteTemp').dimTo(sceneWhite[i][2])
                         switch('Dimmer').dimTo(sceneWhite[i][3])
                     end
                  end
                  for i in pairs(sceneRGB) do
                     if (device.levelName == sceneRGB[i][1] ) then
                         switch('Hue').dimTo(sceneRGB[i][2])
                         switch('Sat').dimTo(sceneRGB[i][3])
                         switch('Bri').dimTo(sceneRGB[i][4])
                     end
                  end

           end   
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
savage007
Posts: 19
Joined: Sunday 13 September 2015 20:24
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Yeelights with selector Script not working

Post by savage007 »

Yes, thank you, you are right on the spot about just switching the first item! :)

I will try your script and give you some feedback! :D
savage007
Posts: 19
Joined: Sunday 13 September 2015 20:24
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Yeelights with selector Script not working

Post by savage007 »

your adaptations to the script are working! :)

I also figured out i had defined the wrong types of dimmers for the 'Dimmer','WhiteTemp','Hue','Sat','Bri','Duration' switches, now that i corrected those and added your script its all working.

For future reference : from the Yeelight/Domoticz wiki : "First You have to add a dummy selector switch, and set it as a dimmer on switch page ", this goes for the 'Dimmer','WhiteTemp','Hue','Sat','Bri','Duration' switches

Many Thanks!!!!!! ;)
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest