How to use history?

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

Moderator: leecollings

Post Reply
MeAlbert
Posts: 65
Joined: Friday 06 December 2019 14:23
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.1
Contact:

How to use history?

Post 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')
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: How to use history?

Post 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
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
MeAlbert
Posts: 65
Joined: Friday 06 December 2019 14:23
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.1
Contact:

Re: How to use history?

Post 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)
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: How to use history?

Post 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
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
MeAlbert
Posts: 65
Joined: Friday 06 December 2019 14:23
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.1
Contact:

Re: How to use history?

Post 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
MeAlbert
Posts: 65
Joined: Friday 06 December 2019 14:23
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.1
Contact:

Re: How to use history?

Post 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.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: How to use history?

Post 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.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: How to use history?

Post 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
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest