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

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
BakSeeDaa
Posts: 485
Joined: Thursday 17 September 2015 10:13
Target OS: Raspberry Pi / ODroid
Domoticz version:

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

Post 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!
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

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

Post 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.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
BakSeeDaa
Posts: 485
Joined: Thursday 17 September 2015 10:13
Target OS: Raspberry Pi / ODroid
Domoticz version:

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

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

Who is online

Users browsing this forum: No registered users and 1 guest