Page 1 of 1
edit Group
Posted: Sunday 17 January 2021 19:16
by yara009
Hi,
is it possible in dzVents (or dzVents+JSON) to edit group "GRP001" and fill it with all switches?
For example:
1/ remove all devices in group
2/ add all devices:
Switch001 ondelay=3 offdelay=3
Switch002 ondelay=6 offdelay=6
Switch003 ondelay=9 offdelay=9
...
Switch099 ondelay=300 offdelay=300
Thanks
Re: edit Group
Posted: Sunday 17 January 2021 20:46
by waaren
yara009 wrote: ↑Sunday 17 January 2021 19:16
is it possible in dzVents (or dzVents+JSON) to edit group "GRP001" and fill it with all switches?
Yes, allmost everything in the domoticz GUI is controlled by JSON commands. So if you an do it in the GUI, most of the time, you can do the same with JSON / API calls
Code: Select all
json.htm?type=deletescene&idx=1
json.htm?type=addscene&name=testScene&scenetype=0
json.htm?type=command¶m=addscenedevice&idx=7&isscene=true&devidx=495&command=On&level=100&color=&ondelay=300&offdelay=400
Re: edit Group
Posted: Sunday 17 January 2021 23:30
by yara009
I still have two problems, can you help me please?
1)
This is OK (group id=13, device id=37):
/json.htm?type=command¶m=addscenedevice&idx=13&isscene=false&devidx=37&level=100&ondelay=30&offdelay=30
This is not working:
/json.htm?type=command¶m=deletescenedevice&idx=13&devidx=37
2)
This dzVents is not working (I'm not experienced):
return {
on = {
devices = {
-- activate script by switch
'_dummy001'
}
},
execute = function(domoticz, item)
local myGroup = domoticz.groups("MYGROUP")
-- for future use, i'd like to delete all devices from group
local myDev = myGroup.devices()
del01 = 0
del02 = 0
-- fill group witch devices
-- select all desired switches
domoticz.devices().filter(function(device)
return (string.find(string.lower(device.name),"switch") ~= nil)
end).forEach(function(myDevice)
-- prepare url string
a = domoticz.settings['Domoticz url'] .. '/json.htm?type=command&addscenedevice&idx=13&isscene=false&devidx=' .. myDevice.idx .. '&level=100&ondelay=' .. del01 .. '&offdelay='.. del02
-- just logging, but everything looks fine
domoticz.log(myDevice.name .. " / " .. myDevice.idx .. " / " .. a)
-- maybe problem here. No error log, but Group is unchanged
domoticz.openURL({
url = a
})
-- increments for on-off delay
del01 = del01 + 3
del02 = del02 + 3
end);
end
}
Re: edit Group
Posted: Monday 18 January 2021 1:12
by waaren
yara009 wrote: ↑Sunday 17 January 2021 23:30
I still have two problems, can you help me please?
1)
This is OK (group id=13, device id=37):
/json.htm?type=command¶m=addscenedevice&idx=13&isscene=false&devidx=37&level=100&ondelay=30&offdelay=30
This is not working:
/json.htm?type=command¶m=deletescenedevice&idx=13&devidx=37
/json.htm?type=command¶m=deletescenedevice&idx=37
Should do it.
The dzVents looks ok to me. I tested with this and the device was added.
Code: Select all
domoticz.settings['Domoticz url'] .. '/json.htm?type=command¶m=addscenedevice&idx=19&isscene=false&devidx=800&command=On&level=100&color=&ondelay=6&offdelay=11'
Did you allow passwordless access from you local network ? Check
this
Re: edit Group
Posted: Monday 18 January 2021 9:46
by yara009
2/
I found typo problem in my dzVents, there was missing "param="addscenedevice
So, adding devices into group is working well, thank you.
1/
But, I still have problem with removing devices from group.
Problem is here:
/json.htm?type=command¶m=deletescenedevice&
idx=37
idx=37 is not ID of device, but internal ID of device in group. But I can not find, how to get this ID from dzVents.
This is JSON list of the group:
Code: Select all
{
"Color" : "",
"Command" : "On",
"DevID" : "85", [b]<--------- this is ID of device (switch)[/b]
"DevRealIdx" : "85",
"ID" : "44", [b]<---------- I'm looking for this ID in dzVents[/b]
"Level" : 100,
"Name" : "TV22 - switch",
"OffDelay" : 3,
"OnDelay" : 3,
"Order" : 44,
"SubType" : "Switch",
"Type" : "Light/Switch"
},
Re: edit Group
Posted: Monday 18 January 2021 15:17
by waaren
yara009 wrote: ↑Monday 18 January 2021 9:46
But, I still have problem with removing devices from group.
Problem is here:
/json.htm?type=command¶m=deletescenedevice&
idx=37
idx=37 is not ID of device, but internal ID of device in group. But I can not find, how to get this ID from dzVents.
It is not natively available in dzVents but easy to retrieve using the openURL / httpResponse construct. If not too dynamic you could do that once a day and store the information in persistent data so to have it readily available when needed. Store in Global persistent data if you need it in multiple scripts.
Something like below script
Code: Select all
local scriptVar = 'getGroupDevices'
return
{
on =
{
timer =
{
'at 04:13', -- daily at a quiet time
},
devices =
{
'groupTrigger', -- for test / development
},
httpResponses =
{
scriptVar .. '*',
},
},
logging =
{
level = domoticz.LOG_DEBUG, -- set to Error when all ok
marker = scriptVar,
},
data =
{
myGroups =
{
initial = {},
},
},
execute = function(dz, item)
local function getGroupDevices(group)
groupURL = dz.settings['Domoticz url'] .. '/json.htm?type=command¶m=getscenedevices&isscene=false&idx=' .. group.idx
dz.openURL(
{
url = groupURL,
callback = scriptVar .. '(' .. group.name .. ')'
})
end
local function makePersistent(t, groupName)
local devices = {}
if t and #t > 0 then
for _, rec in ipairs(t) do
devices[rec.Name] = { realID = rec.DevRealIdx, groupDeviceID = rec.ID }
end
dz.data.myGroups[groupName] = devices
else
dz.data.myGroups[groupName] = {}
end
end
if item.isHTTPResponse and item.json then
-- dz.utils.dumpTable(item.json.result)
makePersistent(item.json.result, item.trigger:match('%b()'):sub(2,-2))
elseif item.isHTTPResponse then
dz.log('Problem with http request ' .. item.trigger, dz.LOG_ERROR)
dz.log(item, dz.LOG_DEBUG)
else
dz.groups().forEach(function(group)
getGroupDevices(group)
end)
end
end
}
Re: edit Group
Posted: Monday 18 January 2021 23:47
by yara009
Than you very much! I will try it and let you know.
Re: edit Group
Posted: Thursday 21 January 2021 15:57
by gschmidt
Is it also possible to add devices to Groups or Scenes by device name and not IDX?
This way the script, which creates all your Groups and Scenes and includes the appropriate devices and their settings, can always be used.
If devices have to be re-created due to a major update of a plug-in (which happened with the domoticz-zigbee2mqtt plugin), all device names are still equal but the IDX number is always different.
Re: edit Group
Posted: Thursday 21 January 2021 16:24
by waaren
gschmidt wrote: ↑Thursday 21 January 2021 15:57
This way the script, which creates all your Groups and Scenes and includes the appropriate devices and their settings, can always be used.
Sorry I don't understand the question. Can you please elaborate ?
What do you mean with 'the script ...'
Re: edit Group
Posted: Thursday 21 January 2021 16:33
by gschmidt
waaren wrote: ↑Thursday 21 January 2021 16:24
gschmidt wrote: ↑Thursday 21 January 2021 15:57
This way the script, which creates all your Groups and Scenes and includes the appropriate devices and their settings, can always be used.
Sorry I don't understand the question. Can you please elaborate ?
What do you mean with 'the script ...'
Let me rephrase it.
Is it possible to create a dzVents script which creates Groups and Scenes and adds all intended devices with their appropriate settings?
If so, is it also possible to add devices based on their name and not idx number?
Re: edit Group
Posted: Thursday 21 January 2021 17:45
by waaren
gschmidt wrote: ↑Thursday 21 January 2021 16:33
Is it possible to create a dzVents script which creates Groups and Scenes and adds all intended devices with their appropriate settings?
If so, is it also possible to add devices based on their name and not idx number?
Yes that is possible; but quite complex and how often does this happen?
It would be much easier to take a backup of the database and manually edit the tables
Scenes, SceneDevices and SceneTimers before copying them back.
(the relation between device-ID and device name can be retrieved from the deviceStatus table)
In case you want to try with a script, you would need below API calls
To get the information:
Code: Select all
/json.htm?type=scenes
/json.htm?type=command¶m=getscenedevices&idx=<scene ID>;isscene=<true / false>
/json.htm?idx=<scene ID>&type=scenetimers
/json.htm?type=command¶m=getsceneactivations&idx=<scene ID>
For restoring it:
Code: Select all
/json.htm?type=addscene&name=<scenename>&scenetype=<0 / 1>
/json.htm?type=command¶m=addscenetimer&idx=<scene ID>&active=<true / false>&timertype=<type>&date=<>&hour=<>&min=<>&randomness=<>&command=<>&level=<>&days=<>
/json.htm?type=command¶m=addscenedevice&idx=<scene ID>&isscene=<true / false>&devidx=<deviceindex>&command=<command>[&level=<dim level>][&hue=<hue level>][color=<color JSON>][&ondelay=<delay in seconds>][&offdelay=<delay in seconds>]
/json.htm?type=command¶m=addscenecode&sceneidx=<scene ID>&idx=<device ID>&cmnd=<command>
Also see
this wiki page
Re: edit Group
Posted: Saturday 23 January 2021 10:02
by gschmidt
waaren wrote: ↑Thursday 21 January 2021 17:45
gschmidt wrote: ↑Thursday 21 January 2021 16:33
Is it possible to create a dzVents script which creates Groups and Scenes and adds all intended devices with their appropriate settings?
If so, is it also possible to add devices based on their name and not idx number?
Yes that is possible; but quite complex and how often does this happen?
It would be much easier to take a backup of the database and manually edit the tables
Scenes, SceneDevices and SceneTimers before copying them back.
(the relation between device-ID and device name can be retrieved from the deviceStatus table)
In case you want to try with a script, you would need below API calls
To get the information:
Code: Select all
/json.htm?type=scenes
/json.htm?type=command¶m=getscenedevices&idx=<scene ID>;isscene=<true / false>
/json.htm?idx=<scene ID>&type=scenetimers
/json.htm?type=command¶m=getsceneactivations&idx=<scene ID>
For restoring it:
Code: Select all
/json.htm?type=addscene&name=<scenename>&scenetype=<0 / 1>
/json.htm?type=command¶m=addscenetimer&idx=<scene ID>&active=<true / false>&timertype=<type>&date=<>&hour=<>&min=<>&randomness=<>&command=<>&level=<>&days=<>
/json.htm?type=command¶m=addscenedevice&idx=<scene ID>&isscene=<true / false>&devidx=<deviceindex>&command=<command>[&level=<dim level>][&hue=<hue level>][color=<color JSON>][&ondelay=<delay in seconds>][&offdelay=<delay in seconds>]
/json.htm?type=command¶m=addscenecode&sceneidx=<scene ID>&idx=<device ID>&cmnd=<command>
Also see
this wiki page
True, it doesn’t happen very often but it happened with domoticz-Zigbee2mqtt users, the plug-in had to be changed to match with last version of Z2M 1.17.0, which has a new api.
To make it work properly the plug-in first had to be removed from the domoticz hardware tab, before upgrading to the latest version
which removes all domoticz zigbee devices created by the plug-in.
So all devices created by the updated plug-in have now a different idx number, which needs to be modified in all groups and scenes using these devices. Some users have so many groups and scenes to modify...
I also like the option of modifying the domoticz database with a texteditor, but I guess the new idx numbers still need to be looked up? Most of the time users keep the names of devices when they are replaced, but they always get a different idx number
Re: edit Group
Posted: Saturday 23 January 2021 12:16
by waaren
gschmidt wrote: ↑Saturday 23 January 2021 10:02
I also like the option of modifying the domoticz database with a texteditor, but I guess the new idx numbers still need to be looked up? Most of the time users keep the names of devices when they are replaced, but they always get a different idx number
Should not be that complicated if you kept the same names.
Below bash script would do it for you
Code: Select all
backupDatabase=/home/pi/domoticz/backups/daily/backup-day-12-domoticz.db # change to real name of backup
currentDatabase=/home/pi/domoticz/domoticz.db # change to real name of current database
sudo service domoticz stop
sudo cp $currentDatabase safeCopy.db # just to be sure
sudo cp $currentDatabase dz.db
sudo sqlite3 dz.db "ATTACH DATABASE \"$backupDatabase\" AS backup;
CREATE TABLE ConversionHelper AS SELECT * FROM backup.devicestatus;
CREATE VIEW Conversion AS SELECT a.ID New, b.ID Old, b.Name Name FROM DeviceStatus a, ConversionHelper b WHERE a.Name = b.Name;
UPDATE SceneDevices SET DeviceRowID = (SELECT c.New FROM conversion c WHERE c.Old = DeviceRowID );
DROP VIEW Conversion;
DROP TABLE ConversionHelper;"
sudo mv dz.db domoticz_converted.db
echo Scene devices converted
echo Check database and if OK replace current domoticz database with domoticz_converted.db
echo and start domoticz
Re: edit Group
Posted: Saturday 23 January 2021 12:43
by gschmidt
waaren wrote: ↑Saturday 23 January 2021 12:16
gschmidt wrote: ↑Saturday 23 January 2021 10:02
I also like the option of modifying the domoticz database with a texteditor, but I guess the new idx numbers still need to be looked up? Most of the time users keep the names of devices when they are replaced, but they always get a different idx number
Should not be that complicated if you kept the same names.
Below bash script would do it for you
Code: Select all
backupDatabase=/home/pi/domoticz/backups/daily/backup-day-12-domoticz.db # change to real name of backup
currentDatabase=/home/pi/domoticz/domoticz.db # change to real name of current database
sudo service domoticz stop
sudo cp $currentDatabase safeCopy.db # just to be sure
sudo cp $currentDatabase dz.db
sudo sqlite3 dz.db "ATTACH DATABASE \"$backupDatabase\" AS backup;
CREATE TABLE ConversionHelper AS SELECT * FROM backup.devicestatus;
CREATE VIEW Conversion AS SELECT a.ID New, b.ID Old, b.Name Name FROM DeviceStatus a, ConversionHelper b WHERE a.Name = b.Name;
UPDATE SceneDevices SET DeviceRowID = (SELECT c.New FROM conversion c WHERE c.Old = DeviceRowID );
DROP VIEW Conversion;
DROP TABLE ConversionHelper;"
sudo mv dz.db domoticz_converted.db
echo Scene devices converted
echo Check database and if OK replace current domoticz database with domoticz_converted.db
echo and start domoticz
Does the UPDATE SceneDevices command also contain the group devices or is that a different command: GroupDevices?
Re: edit Group
Posted: Saturday 23 January 2021 14:44
by waaren
gschmidt wrote: ↑Saturday 23 January 2021 12:43
Does the UPDATE SceneDevices command also contain the group devices or is that a different command: GroupDevices?
It is the same. Domoticz groups and scenes information is kept in one table:
Scenes
The relation between devices and groups / scenes is also kept in one table:
SceneDevices
Re: edit Group [Solved]
Posted: Sunday 24 January 2021 8:58
by gschmidt
waaren wrote: ↑Saturday 23 January 2021 14:44
gschmidt wrote: ↑Saturday 23 January 2021 12:43
Does the UPDATE SceneDevices command also contain the group devices or is that a different command: GroupDevices?
It is the same. Domoticz groups and scenes information is kept in one table:
Scenes
The relation between devices and groups / scenes is also kept in one table:
SceneDevices
Thanx, I’ll post this thread to the users of the domoticz Z2M plug-in