Page 1 of 1

Fetching otherdevices_svalues by index?

Posted: Wednesday 01 February 2017 1:33
by Lomax
I'm quite new to the Domoticz Lua API - how can I read the svalue of a device by index rather than name? All the examples I've seen use the device/sensor name, e.g. otherdevices_svalues["My Sensor"]. I don't like this at all since the name is liable to change, and would prefer to use the device index instead, e.g. otherdevices_svalues[73] - but I think this returns nil even though the index is valid.

Re: Fetching otherdevices_svalues by index?

Posted: Friday 03 February 2017 1:11
by Lomax
*bump*

Re: Fetching otherdevices_svalues by index?

Posted: Monday 06 February 2017 16:55
by Lomax
*bump*

Re: Fetching otherdevices_svalues by index?

Posted: Monday 06 February 2017 19:28
by Egregius
I believe your idx will faster change than your name. Think about a complete reinstallation. You can't control idx, but you can choose the name.

Re: Fetching otherdevices_svalues by index?

Posted: Monday 06 February 2017 20:14
by jvdz
This code is untested, but something like this should work:

Code: Select all

-- get the devicename for idx
function get_devname4idx(devIDX)
	for i, v in pairs(otherdevices_idx) do
		if v == devIDX then
			return i
		end
	end
	return ""
end

-- get the devicename for idx
function get_svalue_by_idx(devIDX)
	devname = get_devname4idx(devIDX)
	if devname ~= "" then
		return otherdevices_svalues[devname]
	else
		return nil
	end
end

get_svalue_by_idx(123)
Jos

Re: Fetching otherdevices_svalues by index?

Posted: Saturday 11 February 2017 19:59
by Lomax
Thanks guys, I think @Egregius has a good point, and though @jvdz's code is a reasonable work-around it doesn't use the index to directly select the sensor, which is what I was hoping for. At some point it might be worth updating otherdevices_svalues to allow integer as well as string indexing.