Last device IDX
Moderator: leecollings
Last device IDX
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.
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.
-
dannybloe
- Posts: 1355
- Joined: Friday 29 August 2014 11:26
- Target OS: Raspberry Pi / ODroid
- Domoticz version:
- Location: Ermelo
- Contact:
Re: Last device IDX
The only thing you can do is make a devices reducer that finds the highest index.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
-
SweetPants
Re: Last device IDX
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?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.
- waaren
- Posts: 6028
- Joined: Tuesday 03 January 2017 14:18
- Target OS: Linux
- Domoticz version: Beta
- Location: Netherlands
- Contact:
Re: Last device IDX
This function will do the dirty work for you.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.
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
endDebian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
-
rrozema
- Posts: 470
- Joined: Thursday 26 October 2017 13:37
- Target OS: Raspberry Pi / ODroid
- Domoticz version: beta
- Location: Delft
- Contact:
Re: Last device IDX
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.
-
dannybloe
- Posts: 1355
- Joined: Friday 29 August 2014 11:26
- Target OS: Raspberry Pi / ODroid
- Domoticz version:
- Location: Ermelo
- Contact:
Re: Last device IDX
Yup. I’d say create a Pull Request :p
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
Re: Last device IDX
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.
@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
@waaren Sorry, tried the function, but the function gives back the latest 'active' device.
Any other ideas?
Any other ideas?
-
rrozema
- Posts: 470
- Joined: Thursday 26 October 2017 13:37
- Target OS: Raspberry Pi / ODroid
- Domoticz version: beta
- Location: Delft
- Contact:
Re: Last device IDX
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-
rrozema
- Posts: 470
- Joined: Thursday 26 October 2017 13:37
- Target OS: Raspberry Pi / ODroid
- Domoticz version: beta
- Location: Delft
- Contact:
Re: Last device IDX
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- waaren
- Posts: 6028
- Joined: Tuesday 03 January 2017 14:18
- Target OS: Linux
- Domoticz version: Beta
- Location: Netherlands
- Contact:
Re: Last device IDX
According to Create a device and Create a virtual sensorEddyG 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.
...
,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
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Re: Last device IDX
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.
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.
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=31So I will leave some instructions for the user to create the devices manually in my script.
Re: Last device IDX
In this script are some errors, but even if a correct the errors the result is that it returns the last active device idx.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
TNX anyway, I can use the logic on other places.
Who is online
Users browsing this forum: No registered users and 1 guest