Updating devices of a scene Topic is solved

Topics (not sure which fora)
when not sure where to post, post here and mods will move it to right forum.

Moderators: leecollings, remb0

Post Reply
User avatar
Hesitated
Posts: 42
Joined: Monday 04 June 2018 21:40
Target OS: Raspberry Pi / ODroid
Domoticz version: V4.11665
Location: Ede
Contact:

Updating devices of a scene

Post by Hesitated »

In my living room I have a table with up to 5 (not yet installed all) Hue lights above it.

Now I have made a group and I can turn the group on or off, but I would like to update the colour and brightness for all the devices belonging to that scene.

The Domoticz interface doesn't have that option :?: and the API-documentation is lacking this too, although the parameter updatescenedevice is mentioned.

With the API-call:

Code: Select all

/json.htm?type=command&param=getscenedevices&idx=2&isscene=true
you'll get a list with all the devices of a certain device.
{
"result" : [
{
"Color" : "{\"b\":255,\"cw\":0,\"g\":255,\"m\":3,\"r\":255,\"t\":0,\"ww\":0}",
"Command" : "On",
"DevID" : "374",
"DevRealIdx" : "374",
"ID" : "5",
"Level" : 24,
"Name" : "Lamp eettafel 2",
"OffDelay" : 0,
"OnDelay" : 0,
"Order" : 5,
"SubType" : "RGBWW",
"Type" : "Color Switch"
},
{
"Color" : "{\"b\":255,\"cw\":0,\"g\":255,\"m\":3,\"r\":255,\"t\":0,\"ww\":0}",
"Command" : "On",
"DevID" : "375",
"DevRealIdx" : "375",
"ID" : "6",
"Level" : 24,
"Name" : "Lamp eettafel 4",
"OffDelay" : 0,
"OnDelay" : 0,
"Order" : 6,
"SubType" : "RGBWW",
"Type" : "Color Switch"
}
],
"status" : "OK",
"title" : "GetSceneDevices"
}
To set an individual device you can use the API-call:

Code: Select all

/json.htm?type=command&param=setcolbrightnessvalue&idx=130&color={"m":3,"t":0,"r":0,"g":0,"b":50,"cw":0,"ww":0}&brightness=100
Does this mean that:

Code: Select all

/json.htm?type=command&param=updatescenedevice&idx=XYZ&color={"m":3,"t":0,"r":0,"g":0,"b":50,"cw":0,"ww":0}&brightness=100
is the correct API-call to set the color and brightness for a device in a scene?

But which idx should i use? The DevID's or the ID's of the scenedevices?
Domoticz 2020.2 - RPi3B+ - RFXtrx433XL - P1 - CC2531 - Somfy - Xiaomi - Hue - Omron - MQTT
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Updating devices of a scene

Post by waaren »

Hesitated wrote: Monday 30 December 2019 14:50
To set an individual device you can use the API-call:

Code: Select all

/json.htm?type=command&param=setcolbrightnessvalue&idx=130&color={"m":3,"t":0,"r":0,"g":0,"b":50,"cw":0,"ww":0}&brightness=100
Does this mean that:

Code: Select all

/json.htm?type=command&param=updatescenedevice&idx=XYZ&color={"m":3,"t":0,"r":0,"g":0,"b":50,"cw":0,"ww":0}&brightness=100
is the correct API-call to set the color and brightness for a device in a scene?
No. just use the setcolbrightnessvalue JSON
But which idx should i use? The DevID's or the ID's of the scenedevices?
The device ID as seen on the devices tab
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Updating devices of a scene

Post by waaren »

Hesitated wrote: Monday 30 December 2019 14:50 In my living room I have a table with up to 5 (not yet installed all) Hue lights above it.

Now I have made a group and I can turn the group on or off, but I would like to update the colour and brightness for all the devices belonging to that scene.
Can you try this ?

Code: Select all

--[[

Send these to update existing domoticz uservariable type string. First to set color / brightness of all lights in a group
Send these to update existing domoticz uservariable type string. 2nd to switchOff all devices in a group
Send these to update existing domoticz uservariable type string. 3rd to switchOn all devices in a group
 
<domoticz ip:domoticz port>/json.htm?type=command&param=updateuservariable&vname=setGroupLights&vtype=2&vvalue=groupName;{"m":3,"t":0,"r":0,"g":0,"b":50,"cw":0,"ww":0};31'
<domoticz ip:domoticz port>/json.htm?type=command&param=updateuservariable&vname=setGroupLights&vtype=2&vvalue=groupName;Off
<domoticz ip:domoticz port>/json.htm?type=command&param=updateuservariable&vname=setGroupLights&vtype=2&vvalue=groupName;On

dz.utils.stringSplit require dzVents >= 2.4.19 (domoticz V4.10728)
dz.utils.dumpTable require dzVents >= 2.4.17 (domoticz V4.10602)
dz.utils.groupExists require dzVents >= 2.4.28 (domoticz V4.11079)

see https://www.domoticz.com/wiki/Domoticz_API/JSON_URL's#Set_a_light_to_a_certain_color_or_color_temperature

]]--

return 
{
    on = 
    {
        variables = 
        {
            'setGroupLights',              -- change to your variable name
        },
    },

       logging = 
    { 
        level = domoticz.LOG_DEBUG -- set to LOG_ERROR when all working as expected
    },

    execute = function(dz, item)
        _G.logMarker =  _G.moduleLabel
       
        local function setColor(id, color, brightness)
            local url = dz.settings['Domoticz url'] ..
                        '/json.htm?type=command&param=setcolbrightnessvalue' ..
                        '&idx=' .. id   ..
                        '&brightness=' .. brightness ..
                        '&color=' .. color
            return dz.openURL(url)
        end
        
        -- @parm group      [required]; domoticz.group object 
        -- @parm action     [optional]; domoticz color object, 'On' or 'Off' ; default = 'Off'
        -- @parm brightness [optional]; brightness (0-100)  ; default 100
        local function setDevices(group, action , brightness)
            local action = action or 'Off'
            local brightness = brightness or 100
            group.devices().forEach(function(dv)
                if action == 'Off' then dv.switchOff()
                elseif action == 'On' then dv.switchOn()
                else setColor(dv.id, action, brightness)
                end
            end)
        end
        
        -- Main
        local parms = dz.utils.stringSplit(item.value, ';') -- 1: groupName or groupID, 2: On, Off or colorJSON, 3: brightness or empty
        if _G.logLevel == dz.LOG_DEBUG then dz.utils.dumpTable(parms) end -- Only on debug
		
		if dz.utils.groupExists(parms[1])) then 
			object = dz.groups(parms[1])
        else
			object = dz.scenes(parms[1])
		end
	   
        setDevices(object,parms[2],parms[3])
        
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
Hesitated
Posts: 42
Joined: Monday 04 June 2018 21:40
Target OS: Raspberry Pi / ODroid
Domoticz version: V4.11665
Location: Ede
Contact:

Re: Updating devices of a scene

Post by Hesitated »

waaren wrote: Sunday 05 January 2020 10:40 Can you try this ?

dz.utils.stringSplit require dzVents >= 2.4.19 (domoticz V4.10728)
dz.utils.dumpTable require dzVents >= 2.4.17 (domoticz V4.10602)
dz.utils.groupExists require dzVents >= 2.4.28 (domoticz V4.11079)
It worked. :D

Although I had to change it a bit as I'm still on version V4.10717, instead of dz.utils.stringSplit, I had to use:

Code: Select all

        local parms = {}
        local index1 = string.find(item.value, ';', 1, true) - 1
        parms[1] = string.sub(item.value, 1,index1)
        dz.log('parms[1] = ' .. parms[1], dz.LOG_INFO)

        index1 = index1 + 2
        local index2 = string.find(item.value, ';', index1, true) - 1
        parms[2] = string.sub(item.value, index1, index2)
        dz.log('parms[2] = ' .. parms[2], dz.LOG_INFO)

        index2 = index2 + 2
        local index3 = string.len(item.value)
        parms[3] = string.sub(item.value, index2, index3)
        dz.log('parms[3] = ' .. parms[3], dz.LOG_INFO)
But that is just a matter of coding and maintainability... ;)

Thanxs again!
Domoticz 2020.2 - RPi3B+ - RFXtrx433XL - P1 - CC2531 - Somfy - Xiaomi - Hue - Omron - MQTT
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Updating devices of a scene

Post by waaren »

Hesitated wrote: Sunday 05 January 2020 18:31 It worked. :D
Although I had to change it a bit as I'm still on version V4.10717
Nice
If you look in dzVents/runtime/Utils.lua of a recent Beta and search for stringSplit you will find the function self.stringSplit

You can copy that function and merge it into the script as

Code: Select all

function stringSplit(text, sep)
	if not(text) then return {} end
	local sep = sep or '%s'
	local t = {}
	for str in string.gmatch(text, "([^"..sep.."]+)") do
		table.insert(t, str)
	end
	return t
end
Same approach could be used for groupExists and dumpTable. No magic involved here.. Just plain Lua.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
Hesitated
Posts: 42
Joined: Monday 04 June 2018 21:40
Target OS: Raspberry Pi / ODroid
Domoticz version: V4.11665
Location: Ede
Contact:

Re: Updating devices of a scene

Post by Hesitated »

waaren wrote: Sunday 05 January 2020 19:09 If you look in dzVents/runtime/Utils.lua of a recent Beta and search for stringSplit you will find the function self.stringSplit
You can copy that function and merge it into the script as
At first it did not work.... Until I saw that ds.utils.stringSplit was not correct... :o
waaren wrote: Sunday 05 January 2020 19:09 Same approach could be used for groupExists and dumpTable. No magic involved here.. Just plain Lua.
Now first back to work, maybe I'll look into this later...

Thnxs again!
Domoticz 2020.2 - RPi3B+ - RFXtrx433XL - P1 - CC2531 - Somfy - Xiaomi - Hue - Omron - MQTT
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest