Fetching otherdevices_svalues by index? Topic is solved

Moderator: leecollings

Post Reply
Lomax
Posts: 32
Joined: Monday 18 May 2015 17:12
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: United Kingdom
Contact:

Fetching otherdevices_svalues by index?

Post 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.
Lomax
Posts: 32
Joined: Monday 18 May 2015 17:12
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: United Kingdom
Contact:

Re: Fetching otherdevices_svalues by index?

Post by Lomax »

*bump*
Lomax
Posts: 32
Joined: Monday 18 May 2015 17:12
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: United Kingdom
Contact:

Re: Fetching otherdevices_svalues by index?

Post by Lomax »

*bump*
User avatar
Egregius
Posts: 2592
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: v2024.7
Location: Beitem, BE
Contact:

Re: Fetching otherdevices_svalues by index?

Post 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.
User avatar
jvdz
Posts: 2328
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: Fetching otherdevices_svalues by index?

Post 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
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
Lomax
Posts: 32
Joined: Monday 18 May 2015 17:12
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: United Kingdom
Contact:

Re: Fetching otherdevices_svalues by index?

Post 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.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest