Page 1 of 1

How to use history?

Posted: Thursday 06 February 2020 19:47
by MeAlbert
I want to calculate the usage of a heater .
The status of the heater status I can read and set, but the rekenTijdt will not work. What should I do or what do I do wrong?

Code: Select all

 return {
	active = true,
	on = { 
        devices = { 59 }
	},
	logging = {
        level = domoticz.LOG_DEBUG, -- for debugging
        --level = domoticz.LOG_INFO,
        marker = "Heater"
    },
	data = { rekenTijd = { history = true, maxItems = 2,  }
        },
	data = { status = { history = true, maxItems = 2 } 
	},   
	
	
	execute = function (domoticz, device)
        domoticz.log ( 'voor if statements ' .. device.state .. ' huidige status' )
	    if ( device.state == 'Off') then            -- device 59 is uit
	    
	    domoticz.data.rekenTijd.add (domoticz.time.secondsSinceMidnight) -- werkt niet
            domoticz.data.status.add (device.state)
            domoticz.data.status.add ('On')

Re: How to use history?

Posted: Thursday 06 February 2020 22:58
by waaren
MeAlbert wrote: Thursday 06 February 2020 19:47 I want to calculate the usage of a heater .
The status of the heater status I can read and set, but the rekenTijd will not work. What should I do or what do I do wrong?
this script does store your data as historical persistent data

Code: Select all

return 
{
    active = true,
    
    on = 
    { 
        devices = 
        { 
            59, 
        },
    },
    
    logging = 
    {
        level = domoticz.LOG_DEBUG, -- for debugging
        marker = "Heater",
    },
    
    data = 
    { 
        rekenTijd = { history = true, maxItems = 2 },
        status = { history = true, maxItems = 2 },     
    },
    
    execute = function (domoticz, device)
        domoticz.log ( 'voor if statements ' .. device.state .. ' huidige status' )
        if ( device.state == 'Off') then            -- device 59 is uit
            domoticz.data.rekenTijd.add(domoticz.time.secondsSinceMidnight) -- werkt niet
            domoticz.data.status.add(device.state)
        end    
    end
}
It creates this persistent data file
Spoiler: show

Code: Select all

-- Persistent Data
local multiRefObjects = {

} -- multiRefObjects
local obj1 = {
        ["status"] = {
                [1] = {
                        ["time"] = "2020-2-6 21:53:6.751";
                        ["data"] = "Off";
                };
        };
        ["rekenTijd"] = {
                [1] = {
                        ["time"] = "2020-2-6 21:53:6.751";
                        ["data"] = 82386;
                };
        };
}
return obj1

Re: How to use history?

Posted: Friday 07 February 2020 0:26
by MeAlbert
Then I get this error message.
2020-02-07 00:24:01.374 Status: dzVents: Error (2.4.19): Heater: An error occured when calling event handler Script #1
2020-02-07 00:24:01.384 Status: dzVents: Error (2.4.19): Heater: ...domoticz/scripts/dzVents/generated_scripts/Script #1.lua:30: attempt to index field 'rekenTijd' (a nil value)

Re: How to use history?

Posted: Friday 07 February 2020 0:49
by waaren
MeAlbert wrote: Friday 07 February 2020 0:26 Then I get this error message.
2020-02-07 00:24:01.374 Status: dzVents: Error (2.4.19): Heater: An error occured when calling event handler Script #1
2020-02-07 00:24:01.384 Status: dzVents: Error (2.4.19): Heater: ...domoticz/scripts/dzVents/generated_scripts/Script #1.lua:30: attempt to index field 'rekenTijd' (a nil value)
Could be another or modified script ? The script I posted does not have rekenTijd on line 30

Re: How to use history?

Posted: Friday 07 February 2020 11:05
by MeAlbert
Waaren your script has this line
domoticz.data.rekenTijd.add(domoticz.time.secondsSinceMidnight)
and there the fault is in. Wathever I try I always get the error message.

domoticz.data.status.add (device.state) or ('On') or ('Off') works fine.
domoticz.time.secondsSinceMidnight gives a number that works fine.
If I use local start = domoticz.time.secondsSinceMidnight
Then print (start) gives a number. putting start in domoticz.data.rekenTijd.add(start) gives an error message again.
Very frustratiing

Re: How to use history?

Posted: Saturday 08 February 2020 23:32
by MeAlbert
I found the problem a string and not a value. On this site it was explained.
https://www.domoticz.com/wiki/User_variables

But the commandArray will not accept a number even if uservariables["MyVar"] is a number.

commandArray['Variable:MyVar']= tostring(uservariables["MyVar"] + 25)

will work. The resulting number is converted to a string.
Now my program works.

Re: How to use history?

Posted: Sunday 09 February 2020 0:23
by waaren
MeAlbert wrote: Friday 07 February 2020 11:05 Waaren your script has this line
domoticz.data.rekenTijd.add(domoticz.time.secondsSinceMidnight)
and there the fault is in. Wathever I try I always get the error message.
Yes but not on line 30.
I tested the script I posted before and it does work without problems. If you encounter this error in line 30 you are not executing the same script.

Re: How to use history?

Posted: Sunday 09 February 2020 0:25
by waaren
MeAlbert wrote: Saturday 08 February 2020 23:32 I found the problem a string and not a value. On this site it was explained.
https://www.domoticz.com/wiki/User_variables

But the commandArray will not accept a number even if uservariables["MyVar"] is a number.

commandArray['Variable:MyVar']= tostring(uservariables["MyVar"] + 25)

will work. The resulting number is converted to a string.
Now my program works.
Good that your script works now but for the record:
domoticz.data.rekenTijd.add does not do anything with commandArray. It's completely native dzVents