Page 1 of 1

dzVents design question (multiple readings from an arbitrary number of humidity sensors as persistent data)

Posted: Sunday 19 March 2017 8:27
by BakSeeDaa
Hi there!

I'd like to store multiple readings from an arbitrary number of humidity sensors as persistent data. Of course I'd like to use the nifty dzVents built in statistical functions to be able to calculate an average over several readings etc.

My first script version stored data like this (The data is an array ) :

Code: Select all

-- Persistent Data
local multiRefObjects = {

} -- multiRefObjects
local obj1 = {
        ["humidityReadings"] = {
                [1] = {
                        ["data"] = {
                                [1] = 31;
                                [2] = 24;
                        };
                        ["time"] = "2017-03-18 10:16:00";
                };
                [2] = {
                        ["data"] = {
                                [1] = 31;
                                [2] = 24;
                        };
                        ["time"] = "2017-03-18 10:15:00";
                };
                [3] = {
                        ["data"] = {
                                [1] = 31;
                                [2] = 24;
                        };
                        ["time"] = "2017-03-18 10:14:00";
                };
        };
}
return obj1
As you see the data stored is an array with values read from all my humidity sensors. (2 pcs). My problem with that solution was that I couldn't figure out how to make this work, because the getValue function can only work for a single value, not an array.

So this morning I started to look at another solution where think that the getValue function won't even be needed.

Code: Select all

local multiRefObjects = {

} -- multiRefObjects
local obj1 = {
        ["humidityReadings1"] = {
                [1] = {
                        ["data"] = 27;
                        ["time"] = "2017-03-19 06:57:00";
                };
                [2] = {
                        ["data"] = 27;
                        ["time"] = "2017-03-19 06:57:00";
                };
                [3] = {
                        ["data"] = 27;
                        ["time"] = "2017-03-19 06:57:00";
                };
        };
        ["humidityReadings2"] = {
                [1] = {
                        ["data"] = 22;
                        ["time"] = "2017-03-19 06:57:00";
                };
                [2] = {
                        ["data"] = 22;
                        ["time"] = "2017-03-19 06:57:00";
                };
                [3] = {
                        ["data"] = 22;
                        ["time"] = "2017-03-19 06:57:00";
                };
        };
}
return obj1
Now I will be able to use the statistic functions but my problem is that the number of humidity sensors is hard coded. I have ["humidityReadings1"] and ["humidityReadings2"]. If I decide to share my code, someone else maybe only have 1 humidity sensor.

Any ideas about how to do this the best way?

Cheers!

Re: dzVents design question (multiple readings from an arbitrary number of humidity sensors as persistent data)

Posted: Sunday 19 March 2017 11:39
by dannybloe
Well, what about this (totally untested but you'll get the idea):

Code: Select all

	local humDevices = {
		'd1', 
		'd2',
		'd3'
	}
	
	-- create the data declarations
	local data = { }
	
	for i, device in pairs(humDevices) do
		data[device] = { history = true }
	end
	
	return {
		active = true,
		on = humDevices,
		data = data,
		execute = function(domoticz, device)
			
			-- get variable
			local humvar = domoticz.data[device.name];
			
			humvar.add(device.humidity)
			
			
		end
	}
So the only thing you need is a list of the devices. And they have to be hard coded I'm affraid.

Re: dzVents design question (multiple readings from an arbitrary number of humidity sensors as persistent data)

Posted: Sunday 19 March 2017 17:57
by BakSeeDaa
dannybloe wrote:Well, what about this (totally untested but you'll get the idea):

...
So the only thing you need is a list of the devices. And they have to be hard coded I'm affraid.
Thanks @dannybloe. That piece of code works beautifully. (Except from "on = humDevices". It doesn't like a table as a trigger event. However I use a timer trigger event so it works great! )

I must say that I appreciate the versatile dzVents more and more for every day. :lol: :lol: :lol: