Persistent (global)data save Time()

Easy to use, 100% Lua-based event scripting framework.
Post Reply
Freemann
Posts: 88
Joined: Thursday 24 November 2016 15:55
Target OS: Raspberry Pi
Domoticz version: >=Béta
Location: Netherlands
Contact:

Persistent (global)data save Time()

Post by Freemann » Monday 18 June 2018 12:30

I'm struggeling a bit with the following case;
I want to have the last time a device is switch ON and in most case the last ON time is not equal to the lastUpdate property.
So I decided to save the Time() to an (global)data variable, but that isn't working.
Look like dzVents is not saving the Time() object when doing something like:
domoticz.globalData.BadkamerLastMotionOn = Time()
or
domoticz.data.BadkamerLastMotionOn = Time()

both variables are define in OR global_data.lua OR in the script itself.

Is this a known "bug" and/or has somebody an idea how to save the last ON time to persistent storage?
Domotica/graphs "freak" :)
Rpi3
Amazon Alexa (2 Echo Dots)
Aeotec Z-Stick S2(Gen5)
HarmonyElite
ETHRelayBoard
HUE(5 bulbs, 2 blooms)
NetAtmo(complete setup)
MiLight iboxV6(2 MiLightBulbs)
IP-Cam

https://www.frijduurzaam.nl

dannybloe
Posts: 1473
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi
Domoticz version:
Location: Ermelo
Contact:

Re: Persistent (global)data save Time()

Post by dannybloe » Monday 18 June 2018 12:37

Don't save the Time object but just the raw value. If you use historical=true with persistence you automatic get a time stamp on your data. So, define your persistent variable with history = true and maxItems = 1 and simply store the state value in there. Then you can easily read the time stamp of the current value. Check the documentation about historical data.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.

Freemann
Posts: 88
Joined: Thursday 24 November 2016 15:55
Target OS: Raspberry Pi
Domoticz version: >=Béta
Location: Netherlands
Contact:

Re: Persistent (global)data save Time()

Post by Freemann » Monday 18 June 2018 13:25

So easy... (zo simple)

Hero (HELD)!!
Domotica/graphs "freak" :)
Rpi3
Amazon Alexa (2 Echo Dots)
Aeotec Z-Stick S2(Gen5)
HarmonyElite
ETHRelayBoard
HUE(5 bulbs, 2 blooms)
NetAtmo(complete setup)
MiLight iboxV6(2 MiLightBulbs)
IP-Cam

https://www.frijduurzaam.nl

User avatar
waaren
Posts: 930
Joined: Tuesday 03 January 2017 15:18
Target OS: Raspberry Pi
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Persistent (global)data save Time()

Post by waaren » Monday 18 June 2018 16:47

Freemann wrote:
Monday 18 June 2018 12:30
I want to have the last time a device is switch ON and in most case the last ON time is not equal to the lastUpdate property.
So I decided to save the Time() to an (global)data variable.

Look like dzVents is not saving the Time() object when doing something like:
domoticz.globalData.BadkamerLastMotionOn = Time()
or
domoticz.data.BadkamerLastMotionOn = Time()

... has somebody an idea how to save the last ON time to persistent storage?
Just for completeness; dzVents is capable of storing the time object in persistent storage.
see example code and resulting datafile
Spoiler: show

Code: Select all

-- timeTest.lua 

return {
    on      =   {    devices         =   { "timeTriggerDevice"}},

    data    =   {   currentTimeTable = { initial = "" },
                    nextTimeTable    = { initial = "" },
                    nextTimeValue    = { initial = "" }},

    execute = function(dz, trigger)
            dz.data.currentTimeTable = os.date('*t')   -- Now as table
            dz.data.nextTimeTable = os.date("*t",os.time(dz.data.currentTime)+3600)  -- now + 1 hour as table
            dz.data.nextTimeValue = os.date("%c",os.time(dz.data.currentTime))  -- now as string
    end
}
__data_timeTest.lua
Spoiler: show

Code: Select all

 
-- Persistent Data
local multiRefObjects = {

} -- multiRefObjects
local obj1 = {
        ["nextTimeValue"] = "Mon Jun 18 16:39:49 2018";
        ["nextTimeTable"] = {
                ["min"] = 39;
                ["hour"] = 17;
                ["sec"] = 49;
                ["day"] = 18;
                ["isdst"] = true;
                ["wday"] = 2;
                ["yday"] = 169;
                ["month"] = 6;
                ["year"] = 2018;
        };
        ["currentTimeTable"] = {
                ["min"] = 39;
                ["hour"] = 16;
                ["sec"] = 49;
                ["day"] = 18;
                ["isdst"] = true;
                ["wday"] = 2;
                ["yday"] = 169;
                ["month"] = 6;
                ["year"] = 2018;
        };
}
return obj1
Raspberry (debian stretch via berryboot on Synology DS916+) , Domoticz (almost) latest Beta, , dzVents 2.6, RFLink, RFXtrx433e, P1, Youless, Harmony, Hue, Yeelight, Xiaomi, HomeWizard, Zwave, Amazon echo

dannybloe
Posts: 1473
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi
Domoticz version:
Location: Ermelo
Contact:

Re: Persistent (global)data save Time()

Post by dannybloe » Monday 18 June 2018 18:20

Yes but it is not needed as I explained above. You can however extract the raw time string and store that and feed that back into Time() when you need it again.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.

Freemann
Posts: 88
Joined: Thursday 24 November 2016 15:55
Target OS: Raspberry Pi
Domoticz version: >=Béta
Location: Netherlands
Contact:

Re: Persistent (global)data save Time()

Post by Freemann » Sunday 08 July 2018 21:59

So to "reuse" this topic;
Trying to make my variables global available, but not getting there.

how should I use the global define variables for getting/setting in my scripts?

Tried some variations like;
for setting:

Code: Select all

domoticz.globalData.motionVar.add('Foo')
domoticz.globalData.motionVar = 'On'
domoticz.data.motionVar.add('Foo')
domoticz.data.motionVar = 'On'
I'm not getting the __data_global_data.lua to show data...

and should I define the global_data vars in my "local" scripts again?


[SOLVED] and maybe it can help somebody
found it after some "testing/debugging";
global_data.lua looks like;

Code: Select all

data = {
      LastMotionOn = { history = true, maxItems = 1}
}
normal dzvents script is doing for historical setting;

Code: Select all

domoticz.globalData.LastMotionOn.add('On')
normal dzvents script is doing for historical getting;

Code: Select all

domoticz.globalData.LastMotionOn.get(1).time.secondsAgo
Domotica/graphs "freak" :)
Rpi3
Amazon Alexa (2 Echo Dots)
Aeotec Z-Stick S2(Gen5)
HarmonyElite
ETHRelayBoard
HUE(5 bulbs, 2 blooms)
NetAtmo(complete setup)
MiLight iboxV6(2 MiLightBulbs)
IP-Cam

https://www.frijduurzaam.nl

Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests