Page 1 of 1
Last device IDX
Posted: Saturday 28 July 2018 10:42
by EddyG
I might have missed something, and could not find it either in wiki and forum.
Is there a way to find the last IDX in Domoticz?
I would like to have that to create a new customdevice with a certain name.
At start of the script it could be tested if the device exists and set a marker that it is present.
This way the script could create there devices like in the python plugins.
Re: Last device IDX
Posted: Saturday 28 July 2018 11:56
by dannybloe
The only thing you can do is make a devices reducer that finds the highest index.
Re: Last device IDX
Posted: Saturday 28 July 2018 12:35
by SweetPants
EddyG wrote: Saturday 28 July 2018 10:42
I might have missed something, and could not find it either in wiki and forum.
Is there a way to find the last IDX in Domoticz?
I would like to have that to create a new customdevice with a certain name.
At start of the script it could be tested if the device exists and set a marker that it is present.
This way the script could create there devices like in the python plugins.
As far as i know when creating a new device it always takes the latest IDX + 1 for the new device, or am i missing something?
Re: Last device IDX
Posted: Saturday 28 July 2018 12:37
by waaren
EddyG wrote: Saturday 28 July 2018 10:42
I might have missed something, and could not find it either in wiki and forum.
Is there a way to find the last IDX in Domoticz?
I would like to have that to create a new customdevice with a certain name.
At start of the script it could be tested if the device exists and set a marker that it is present.
This way the script could create there devices like in the python plugins.
This function will do the dirty work for you.
Code: Select all
local function getHighestIndex()
local highestIndex = 0
domoticz.devices().forEach(function(device)
if device.idx > highestIndex then
highestIndex = device.idx
end
end)
domoticz.log("Device: " .. domoticz.devices(highestIndex).name ..
" ==>> index: " .. highestIndex ..
" is (in use) device with highest indexNumber",domoticz.LOG_INFO)
return highestIndex
end
Re: Last device IDX
Posted: Sunday 29 July 2018 14:38
by rrozema
But why would you do this? Seeing that we're working in a multi threaded environment, it's better to use a call that creates a new device for you and returns the idx that it created to you. That way you avoid a potential situation where there suddenly are 2 devices having the same idx value because they happened to be created at (almost) the same time.
Re: Last device IDX
Posted: Sunday 29 July 2018 15:02
by dannybloe
Yup. I’d say create a Pull Request :p
Re: Last device IDX
Posted: Monday 30 July 2018 10:09
by EddyG
Sorry for the delayed reaction (Weekend and nice weather

)
@waaren Tnx I will use that.
As far I as saw you could create New devices in the Python plugin environment, but could not do that in the dzVents environment the same way.
I am not a fan of the Python plugins due to the previous memory problem and and crashes.
I am a fan of dzVents and want to do everything (scripting) with dzVents. That's why I asked.
The only way that I can create a new device is via an API call, but then you have to have the last/highest IDX + 1.
So for now I will use @waaren solution.
Re: Last device IDX
Posted: Monday 30 July 2018 10:57
by EddyG
@waaren Sorry, tried the function, but the function gives back the latest 'active' device.
Any other ideas?
Re: Last device IDX
Posted: Monday 30 July 2018 12:04
by rrozema
Using the .reduce() iterator (
https://www.domoticz.com/wiki/DzVents:_ ... g#Examples:) it is even simpler. And this for sure does
not return the last active device. It returns the device with the highest idx value, just like waaren's code.
Code: Select all
local function getHighestIndex()
local last_device = domoticz.devices().reduce(function(acc, device)
if acc is nil or device.idx > acc.idx then
acc = device
end
return acc
end), nil)
if last_device is nil then
domoticz.log("No devices found!", domoticz.LOG_INFO)
return nil
else
domoticz.log("Device: " .. last_device.name ..
" ==>> index: " .. last_device.idx ..
" is (in use) device with highest indexNumber",domoticz.LOG_INFO)
return last_device.idx
end
end
Re: Last device IDX
Posted: Monday 30 July 2018 12:10
by rrozema
If it's for this particular purpose, why not make a function that's easier to use:
Code: Select all
local function getNewIndex()
local last_device = domoticz.devices().reduce(function(acc, device)
if acc is nil or device.idx > acc.idx then
acc = device
end
return acc
end), nil)
if last_device is nil then
domoticz.log("No devices found, returning 1 for the first device", domoticz.LOG_INFO)
return 1
else
domoticz.log("Device: " .. last_device.name ..
" ==>> index: " .. last_device.idx ..
" is (in use) device with highest indexNumber",domoticz.LOG_INFO)
return last_device.idx + 1
end
end
Re: Last device IDX
Posted: Monday 30 July 2018 13:08
by waaren
EddyG wrote: Monday 30 July 2018 10:09
...
The only way that I can create a new device is via an API call, but then you have to have the last/highest IDX + 1.
...
According to
Create a device and
Create a virtual sensor
,you don't need a unique device indexNumber to create a device. It will simply use the next available one and return the idx. The devices created are set to active on my system but YMMV.
just tested:
- Spoiler: show
Code: Select all
http://xxx.xxx.xxx.xxx:nnnn/json.htm?type=createdevice&idx=35&sensorname=Temp5&devicetype=243&devicesubtype=22
{
"idx" : "1012",
"status" : "OK",
"title" : "CreateSensor"
}
http://xxx.xxx.xxx.xxx:nnnn/json.htm?type=createvirtualsensor&idx=35&sensorname=TempHum&sensortype=82
{
"idx": "1013",
"status": "OK",
"title": "CreateVirtualSensor"
}
- Spoiler: show
Re: Last device IDX
Posted: Monday 30 July 2018 13:44
by EddyG
I was talking about virtual devices in my case.
I did not read the wiki properly.
And I was confused about the 'idx' mentioned there, I tought it was the device idx.
I test again in my code if it does the job. TNX.
Edit.
I can create all kind of devices but not these.
Code: Select all
http://192.168.xxx.xxx:nnnn/json.htm?type=createdevice&idx=3&sensorname=Dummy&devicetype=243&devicesubtype=31
http://192.168.xxx.xxx:nnnn/json.htm?type=createvirtualsensor&idx=3&sensorname=Dummy&sensortype=243 (or 31)
http://192.168.xxx.xxx:nnnn/json.htm?type=createvirtualsensor&idx=3&sensorname=Dummy&sensortype=243&devicesubtype=31
http://192.168.xxx.xxx:nnnn/json.htm?type=createvirtualsensor&idx=3&sensorname=Dummy&sensortype=243&sensorsubtype=31
I wanted to create a virtual device of "General/Custom Sensor", it seems not possible.
So I will leave some instructions for the user to create the devices manually in my script.
Re: Last device IDX
Posted: Monday 30 July 2018 14:42
by EddyG
rrozema wrote: Monday 30 July 2018 12:10
If it's for this particular purpose, why not make a function that's easier to use:
Code: Select all
local function getNewIndex()
local last_device = domoticz.devices().reduce(function(acc, device)
if acc == nil or device.idx > acc.idx then
acc = device
end
return acc
end)
if last_device == nil then
domoticz.log("No devices found, returning 1 for the first device", domoticz.LOG_INFO)
return 1
else
domoticz.log("Device: " .. last_device.name ..
" ==>> index: " .. last_device.idx ..
" is (in use) device with highest indexNumber",domoticz.LOG_INFO)
return last_device.idx + 1
end
end
In this script are some errors, but even if a correct the errors the result is that it returns the last active device idx.
TNX anyway, I can use the logic on other places.