Persistent data as a variable

Easy to use, 100% Lua-based event scripting framework.
User avatar
waaren
Posts: 1345
Joined: Tuesday 03 January 2017 15:18
Target OS: Raspberry Pi
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Persistent data as a variable

Post by waaren » Sunday 18 November 2018 22:26

pvklink wrote:
Sunday 18 November 2018 12:32
@waaren GREAT, Thanks!
I changed the script with 2 devices and one does work!, have two questions.

1) dz.data.sensors[name] is rizing even when the current usage huidwaarde is lower then watt (see the below log)
Is that no problem ?

2) the script only starts sensor buro
my sensor droger isnt reachable(to far away at the time) could that be the problem, because i dont see an error that it isnt reachable...
@pvklink, Hi Peter,

1) Should not be a problem if you use the device more then once a year :D . The counter is reset to 0 when the kwH rises above the usage fence.
2) If the second sensor is not reachable it will not trigger the script. Not a problem but not very useful either.
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

pvklink
Posts: 136
Joined: Wednesday 12 November 2014 16:01
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Persistent data as a variable

Post by pvklink » Sunday 18 November 2018 22:37

@waaren
Both work! THANKKKKKS

I also used SwitchTimeOutMinutes for the timeout period. i use the counter for this and i adjust the hard values =1, =2 and >2 for the counter
and i made the notificationsounds flexibel.

Peter

Code: Select all

local USAGE_SENSORS = {
    ['buro_kwh'] = 'buro_kwh',                -- Adjust to your needs. Between every line you need to add a ",".
    ['wasmachine_kwh'] = 'wasmachine_kwh',                -- Adjust to your needs. Between every line you need to add a ",".
}

local USAGE_SWITCHES = {
    ['buro_kwh'] = 'buro',                     -- Name of device that switch kw_1 On/ Off 
    ['wasmachine_kwh'] = 'wasmachine',                     -- Name of device that switch kw_1 On/ Off 
} 

local USAGE_SwitchTimeOutMinutes = {
    ['buro_kwh'] = 4,                              -- Adjust to your needs. Between every line you need to add a ",".
    ['wasmachine_kwh'] = 4,                              -- Adjust to your needs. Between every line you need to add a ",".
}

local USAGE_MaxWatt = {
    ['buro_kwh'] = 2,                    -- Adjust to your needs. Between every line you need to add a ",".
    ['wasmachine_kwh'] = 2,                    -- Adjust to your needs. Between every line you need to add a ",".
}

local USAGE_SwitchOnOff = {
    ['buro_kwh'] = 'Yes',                -- Adjust to your needs. Between every line you need to add a ",".
    ['wasmachine_kwh'] = 'Yes',                -- Adjust to your needs. Between every line you need to add a ",".
}

local USAGE_Notify = {
    ['buro_kwh'] = 'Yes',
    ['wasmachine_kwh'] = 'Yes',
}

local USAGE_Pushoverreceivers = {
    ['buro_kwh'] = '',
    ['wasmachine_kwh'] = '',
}

local USAGE_Pushoversound = {
    ['buro_kwh'] = 'bike',
    ['wasmachine_kwh'] = 'bike',
}

return {
    on = { devices = USAGE_SENSORS },
    
    logging = { level   = domoticz.LOG_DEBUG ,                   -- Uncomment to override the dzVents global logging setting
                marker  = actOnIdleDevices 
              },
              
    data = { sensors              = { initial  = {} } },
        
    execute = function(dz, device)
 
        local name = device.name
               
        if dz.data.sensors[name] == nil then 
            dz.data.sensors[name] = 0
        end
        
        local switch        = dz.devices(USAGE_SWITCHES[name])
        local timeout       = USAGE_SwitchTimeOutMinutes[USAGE_SENSORS[name]]
        local watt          = USAGE_MaxWatt[USAGE_SENSORS[name]]
        local onoff         = USAGE_SwitchOnOff[USAGE_SENSORS[name]]
        local notify        = USAGE_Notify[USAGE_SENSORS[name]]
        local receivers     = USAGE_Pushoverreceivers[USAGE_SENSORS[name]]
        local sound         = USAGE_Pushoversound[USAGE_SENSORS[name]]
        local huidwaarde    = device.WhActual

        dz.log("00 Sensor device        = " .. name)
        dz.log("00 Sensor onoff         = " .. onoff)
        dz.log("00 Sensor notify        = " .. notify)
        dz.log("00 Sensor receivers     = " .. receivers)
        dz.log("00 Sensor sound         = " .. sound)
        dz.log("00 Sensor timeout       = " .. timeout)
        dz.log("00 Sensor watt          = " .. watt)
        dz.log("00 Sensor teller eronder= " .. tostring(dz.data.sensors[name]))
        dz.log("00 Sensor huidwaarde    = " .. tostring(huidwaarde))
        dz.log(dz.data.sensors[name],dz.LOG_DEBUG)
        dz.log("---------------------------------------")

        if  huidwaarde <= watt then 
            dz.log("01 Sensor info         = Oude en huidige waarde zijn onder de norm")
            dz.data.sensors[name] = dz.data.sensors[name] + 1

            if dz.data.sensors[name] < timeout then 
                dz.log("02 Sensor info         = Nu nog geen actie omdat de timeout limiet nog niet is bereikt")
                
            elseif dz.data.sensors[name] == timeout then
                dz.log("03 Sensor info         = Oude en huidige waarde zijn onder norm en gelijk aan de timeout dus actie")

                if onoff == 'Yes' then
                    dz.log("03a Sensor info         = Oude en huidige waarde zijn onder norm en gelijk aan de timeout, device wordt uitgezet")
                    switch.switchOff().checkFirst().silent() 
                end
                if notify == 'Yes' then
                    dz.log("03b Sensor info         = Oude en huidige waarde zijn onder norm en gelijk aan de timeout, er wordt een notificatie gestuurd")
                        dz.notify(    'dz: ' .. name .. ' is gereed',
                                            name,
                                            PRIORITY_NORMAL,
                                            sound,
                                            receivers,
                                            dz.NSS_PUSHOVER
                                        )
                end

            elseif dz.data.sensors[name] > timeout then
                dz.log("04 Sensor info         = Oude en huidige waarde zijn onder norm maar acties zijn al gedaan dus niets meer doen")

            end
        else
            dz.data.sensors[name] = 0
            dz.log("05 Sensor info         = Huidig waarde boven norm dus reset van idlecounter")
        end
    end
}

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

Re: Persistent data as a variable

Post by waaren » Sunday 18 November 2018 22:54

pvklink wrote:
Sunday 18 November 2018 12:32
I changed the variables in the script with 2 devices and both work! THANKKKKKS
Is it ok that USAGE_SENSORS uses the same values on the left and right side of the comparison?
I don't see any line where you compare USAGE_SENSORS against USAGE_SENSORS. Can you elaborate which line you have in mind when asking this ?
I also used SwitchTimeOutMinutes for the timeout period. i use the counter for this and i adjust the hard values =1, =2 and >2 for the counter
and
i made the notificationsounds flexibel.
Can you have a quick look if this makes sense ?
Looks OK to me
Only thing i cant get to work is that when my wasmachine_kwh is ready wasmachine is used as text for notify instead of wasmachine_kwh (or other device_kwh names)
Should work if you add

Code: Select all

name = USAGE_SWITCHES[name]
direct after the line

Code: Select all

  if notify == 'Yes' then
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

pvklink
Posts: 136
Joined: Wednesday 12 November 2014 16:01
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Persistent data as a variable

Post by pvklink » Sunday 18 November 2018 23:34

This was the line where left is right comparison, looks kind a strange... but works great
Rest is working GREAT

local USAGE_SENSORS = {
['buro_kwh'] = 'buro_kwh',

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

Re: Persistent data as a variable

Post by waaren » Monday 19 November 2018 0:12

pvklink wrote:
Sunday 18 November 2018 23:34
This was the line where left is right comparison, looks kind a strange... but works great
Rest is working GREAT

local USAGE_SENSORS = {
['buro_kwh'] = 'buro_kwh',
OK understand your confusion now.
The line

Code: Select all

 ['buro_kwh'] = 'buro_kwh',
is actually not a comparison but an assignment where you assign the value "buro_kwh" to the key "buro_kwh" in the table USAGE_SENSORS

from Programming in Lua "The table type ... can be indexed not only with numbers, but also with strings or any other value of Lua, except nil"
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

pvklink
Posts: 136
Joined: Wednesday 12 November 2014 16:01
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Persistent data as a variable

Post by pvklink » Monday 19 November 2018 14:39

@waaren

Everything works perfect :D
Here is the finanal usage_script based(99,9999%) on the script from @waaren.
Thanks for your help!

Peter

Code: Select all

local USAGE_SENSORS = {
    ['buro_kwh'] = 'buro_kwh',                -- Adjust to your needs. Between every line you need to add a ",".
    ['wasmachine_kwh'] = 'wasmachine_kwh',                -- Adjust to your needs. Between every line you need to add a ",".
}

local USAGE_SWITCHES = {
    ['buro_kwh'] = 'buro',                     -- Name of device that switch kw_1 On/ Off 
    ['wasmachine_kwh'] = 'wasmachine',                     -- Name of device that switch kw_1 On/ Off 
} 

local USAGE_SwitchTimeOutMinutes = {
    ['buro_kwh'] = 4,                              -- Adjust to your needs. Between every line you need to add a ",".
    ['wasmachine_kwh'] = 4,                              -- Adjust to your needs. Between every line you need to add a ",".
}

local USAGE_MaxWatt = {
    ['buro_kwh'] = 2,                    -- Adjust to your needs. Between every line you need to add a ",".
    ['wasmachine_kwh'] = 2,                    -- Adjust to your needs. Between every line you need to add a ",".
}

local USAGE_SwitchOnOff = {
    ['buro_kwh'] = 'Yes',                -- Adjust to your needs. Between every line you need to add a ",".
    ['wasmachine_kwh'] = 'Yes',                -- Adjust to your needs. Between every line you need to add a ",".
}

local USAGE_Notify = {
    ['buro_kwh'] = 'Yes',
    ['wasmachine_kwh'] = 'Yes',
}

local USAGE_Pushoverreceivers = {
    ['buro_kwh'] = '',
    ['wasmachine_kwh'] = '',
}

local USAGE_Pushoversound = {
    ['buro_kwh'] = 'bike',
    ['wasmachine_kwh'] = 'bike',
}

return {
    on = { devices = USAGE_SENSORS },
    
    logging = { level   = domoticz.LOG_DEBUG ,                   -- Uncomment to override the dzVents global logging setting
                marker  = actOnIdleDevices 
              },
              
    data = { sensors              = { initial  = {} } },
        
    execute = function(dz, device)
 
        local name = device.name
        local name2 = USAGE_SWITCHES[name]
               
        if dz.data.sensors[name] == nil then 
            dz.data.sensors[name] = 0
        end
        
        local switch        = dz.devices(USAGE_SWITCHES[name])
        local timeout       = USAGE_SwitchTimeOutMinutes[USAGE_SENSORS[name]]
        local watt          = USAGE_MaxWatt[USAGE_SENSORS[name]]
        local onoff         = USAGE_SwitchOnOff[USAGE_SENSORS[name]]
        local notify        = USAGE_Notify[USAGE_SENSORS[name]]
        local receivers     = USAGE_Pushoverreceivers[USAGE_SENSORS[name]]
        local sound         = USAGE_Pushoversound[USAGE_SENSORS[name]]
        local huidwaarde    = device.WhActual

        dz.log("00 Sensor device        = " .. name)
        dz.log("00 Sensor onoff         = " .. onoff)
        dz.log("00 Sensor notify        = " .. notify)
        dz.log("00 Sensor receivers     = " .. receivers)
        dz.log("00 Sensor sound         = " .. sound)
        dz.log("00 Sensor timeout       = " .. timeout)
        dz.log("00 Sensor watt          = " .. watt)
        dz.log("00 Sensor teller eronder= " .. tostring(dz.data.sensors[name]))
        dz.log("00 Sensor huidwaarde    = " .. tostring(huidwaarde))
        dz.log(dz.data.sensors[name],dz.LOG_DEBUG)
        dz.log("---------------------------------------")

        if  huidwaarde <= watt then 
            dz.log("01 Sensor info         = Oude en huidige waarde zijn onder de norm")
            dz.data.sensors[name] = dz.data.sensors[name] + 1

            if dz.data.sensors[name] < timeout then 
                dz.log("02 Sensor info         = Nu nog geen actie omdat de timeout limiet nog niet is bereikt")
                
            elseif dz.data.sensors[name] == timeout then
                dz.log("03 Sensor info         = Oude en huidige waarde zijn onder norm en gelijk aan de timeout dus actie")

                if onoff == 'Yes' then
                    dz.log("03a Sensor info         = Oude en huidige waarde zijn onder norm en gelijk aan de timeout, device wordt uitgezet")
                    switch.switchOff().checkFirst().silent() 
                end
                if notify == 'Yes' then
                    dz.log("03b Sensor info         = Oude en huidige waarde zijn onder norm en gelijk aan de timeout, er wordt een notificatie gestuurd")
                        dz.notify(    'dz: ' .. name2 .. ' is gereed',
                                            name,
                                            PRIORITY_NORMAL,
                                            sound,
                                            receivers,
                                            dz.NSS_PUSHOVER
                                        )
                end

            elseif dz.data.sensors[name] > timeout then
                dz.log("04 Sensor info         = Oude en huidige waarde zijn onder norm maar acties zijn al gedaan dus niets meer doen")

            end
        else
            dz.data.sensors[name] = 0
            dz.log("05 Sensor info         = Huidig waarde boven norm dus reset van idlecounter")
        end
    end
}

pvklink
Posts: 136
Joined: Wednesday 12 November 2014 16:01
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Persistent data as a variable

Post by pvklink » Saturday 08 December 2018 14:11

Hi,

I made a script for an electric heater, sort of antifrostdevice.
It has a thermostatic powermechanisme. When it uses 13 watt is is working. when it uses 1 watt it is problably in thermo-mode.
I stick the thermo in a zwave switch with powermeasuring.
I check every hour when the zwaveswitch is on (another scripts that puts the switch on when temp is below 3 gr, this part works!)
if this devices consumes power when it does it is ok, nothing broken, when it dont, it is broken or in thermostate...

Problems
018-12-08 14:09:03.300 Status: dzVents: Info: ------ Start internal script: testxxx: Device: "buro_kwh (zwavepluspvk)", Index: 458
2018-12-08 14:09:03.301 Status: dzVents: Error (2.4.9): An error occured when calling event handler testxxx
2018-12-08 14:09:03.301 Status: dzVents: Error (2.4.9): /home/pi/domoticz/dzVents/runtime/Domoticz.lua:165: attempt to perform arithmetic on local 'x' (a nil value)
2018-12-08 14:09:03.301 Status: dzVents: Info: ------ Finished testxxx

Code: Select all

-- test met stroomlint
-- boven maxwatt dan werkt t lint 
-- onder maxwatt en geijk aan switchoutminutes dan is het lint mogelijk kapot of is toch de thermostaat aan. dan is maxwatt niet juist gekozen

local USAGE_DEVICES = {
    ['buro_kwh'] = 'buro_kwh',                -- Adjust to your needs. Between every line you need to add a ",".
}

local USAGE_SwitchTimeOutMinutes = {
    ['buro_kwh'] = 4,                           -- Adjust to your needs. Between every line you need to add a ",".
}

local USAGE_MaxWatt = {
    ['buro_kwh'] = 88,                    -- Adjust to your needs. Between every line you need to add a ",".
}

local USAGE_Notify = {
    ['buro_kwh'] = 'Yes',
}

local USAGE_Pushoverreceivers = {
    ['buro_kwh'] = '',
}

local USAGE_Pushoversound = {
    ['buro_kwh'] = 'siren',
}

return {
    on = {devices = USAGE_DEVICES,
        },

    logging = { level   = domoticz.LOG_DEBUG ,                   -- Uncomment to override the dzVents global logging setting
                marker  = actOnIdleDevices 
             },
              
    data    =   {
                    prevhistwaarde  = { history = true, maxItems = 10 },
                    sensors         = { initial  = {} },
                },
            
    execute = function(dz, device)
 
        local name          = device.name

        if dz.data.prevhistwaarde.getOldest() == nil then  -- Check if history is already filled (initial does not work with history persistent data
          dz.data.prevhistwaarde.add(100)
        end

        if dz.data.sensors[name] == nil then 
            dz.data.sensors[name] = 0
        end

        local huidwaarde    = dz.utils.round(device.temperature,1)
        local prevwaarde    = dz.utils.round(dz.data.prevhistwaarde.getLatest().data,1)   
        local timeout       = USAGE_SwitchTimeOutMinutes[USAGE_DEVICES[name]]
        local watt          = USAGE_MaxWatt[USAGE_DEVICES[name]]
        local notify        = USAGE_Notify[USAGE_DEVICES[name]]
        local receivers     = USAGE_Pushoverreceivers[USAGE_DEVICES[name]]
        local sound         = USAGE_Pushoversound[USAGE_DEVICES[name]]


        dz.log("00 Sensor device        = " .. name)
        dz.log("00 Sensor notify        = " .. notify)
        dz.log("00 Sensor receivers     = " .. receivers)
        dz.log("00 Sensor sound         = " .. sound)
        dz.log("00 Sensor timeout       = " .. timeout)
        dz.log("00 Sensor watt          = " .. watt)
        dz.log("00 Sensor vorige waarde = " .. prevwaarde)
        dz.log("00 Sensor huidwaarde    = " .. huidwaarde)
       for i = dz.data.prevhistwaarde.size,1,-1 do 
            item = dz.data.prevhistwaarde.get(i)
            dz.log("00 History data van " .. item.time.rawDate .. " " .. item.time.rawTime .. 
                                    " = " .. item.data .. " graden")
        end
        dz.log("---------------------------------------")

         if dz.devices("buro").state == 'On' then
            if  huidwaarde <= watt then 
                dz.log("01 Sensor info         = Huidige waarde is onder de norm, dus lint werkt niet of thermostaat staat aan")
                dz.data.sensors[name] = dz.data.sensors[name] + 1

                if dz.data.sensors[name] < timeout then 
                    dz.log("02 Sensor info         = Nu nog geen actie omdat de timeout limiet nog niet is bereikt")
                
                elseif dz.data.sensors[name] == timeout then
                    dz.log("03 Sensor info         = Oude en huidige waarde zijn onder norm en gelijk aan de timeout dus actie")

                    if notify == 'Yes' then
                        dz.log("03b Sensor info         = Huidige waarde is onder de norm en gelijk aan de timeout, er wordt een notificatie gestuurd")
                        dz.notify(    'dz: ' .. name2 .. ' is gereed',
                                            name,
                                            PRIORITY_NORMAL,
                                            sound,
                                            receivers,
                                            dz.NSS_PUSHOVER
                                        )
                    end
                elseif dz.data.sensors[name] > timeout then
                    dz.log("04 Sensor info         = Oude en huidige waarde zijn onder norm maar acties zijn al gedaan dus niets meer doen")
                end
            else
                dz.data.sensors[name] = 0
                dz.log("05 Sensor info         = Huidig waarde boven norm, lint gebruikt stroom dus reset van idlecounter")
            end
        end
    end
}

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

Re: Persistent data as a variable

Post by waaren » Saturday 08 December 2018 15:36

pvklink wrote:
Saturday 08 December 2018 14:11
Problems

Code: Select all

018-12-08 14:09:03.300 Status: dzVents: Info: ------ Start internal script: testxxx: Device: "buro_kwh (zwavepluspvk)", Index: 458
2018-12-08 14:09:03.301 Status: dzVents: Error (2.4.9): An error occured when calling event handler testxxx
2018-12-08 14:09:03.301 Status: dzVents: Error (2.4.9): /home/pi/domoticz/dzVents/runtime/Domoticz.lua:165: attempt to perform arithmetic on local 'x' (a nil value)
2018-12-08 14:09:03.301 Status: dzVents: Info: ------ Finished testxxx
What I think goes wrong here is that the script calls the function dz.utils.round with a nil value. the device is a kwh device but the call to the round function is with the temperature which likely is nil here.

Try replacing the line l

Code: Select all

ocal huidwaarde    = dz.utils.round(device.temperature,1)
with

Code: Select all

dz.log("00 Sensor temperature   = " .. tostring(device.temperature))
dz.log("00 Sensor usage         = " .. tostring(device.usage))
local huidwaarde    = dz.utils.round(device.usage,1)
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

pvklink
Posts: 136
Joined: Wednesday 12 November 2014 16:01
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Persistent data as a variable

Post by pvklink » Saturday 08 December 2018 16:10

you were wright(again). Script is running!

pvklink
Posts: 136
Joined: Wednesday 12 November 2014 16:01
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Persistent data as a variable

Post by pvklink » Monday 10 December 2018 19:36

Script is running fine!
There is one thing i like to uptimize, but to difficult for me.
I use dz.data.sensors[name] as a counter and when it reaches USAGE_DurTimer then a notification could be send.
The number of USAGE_DurTimer is a value that represense time. Is it possible to use actuel time?

Example:
When the first value of dz.data.sensors[name] is created, it also has a timestamp X.
USAGE_DurTimer could be something like '04:00:00' (4 hours) and notification is send when currentime >timestamp X + '04:00:00'

Code: Select all

-- test met stroomlint
-- boven maxwatt dan werkt t lint 
-- onder maxwatt en geijk aan switchoutminutes dan is het lint mogelijk kapot of is toch de thermostaat aan. dan is maxwatt niet juist gekozen
-- evt kijken naar tijdstip van teller =1 en dan waarde in uren
-- ipv huidwaarde en prevwaarde de laatste 5 waarde nemen om pieken te voorkomen, historie op 5 zetten en die nemen
-- https://github.com/dannybloe/dzVents

local USAGE_DEVICES = {
    ['buro_kwh'] = 'buro_kwh',                -- Adjust to your needs. Between every line you need to add a ",".
}

local USAGE_SWITCHES = {
    ['buro_kwh'] = 'buro',          -- Name of device that switch kw_1 On/ Off 
} 

local USAGE_Watt = {
    ['buro_kwh'] = 110,                    -- There is powerconsumption
}

local USAGE_DurTimer = {
    ['buro_kwh'] = 20,                   -- There is powerconsumption
}

local USAGE_Notify = {
    ['buro_kwh'] = 'Yes',
}

local USAGE_Pushoverreceivers = {
    ['buro_kwh'] = 'galaxya52017',
}

local USAGE_Pushoversound = {
    ['buro_kwh'] = 'siren',
}

return {
    on = {devices = USAGE_DEVICES,
        },

    logging = { level   = domoticz.LOG_DEBUG ,                   -- Uncomment to override the dzVents global logging setting
                marker  = actOnIdleDevices 
             },
              
    data    =   {
                    prevhistwaarde  = { history = true, maxHours = 4 },     --Deze tijdspanne moet je juist kiezen zodat het lint ook de kans krijgt om aan te gaan en boven t gemiddelde te komen
                    sendnotification = {initial = {}}, 
                    sensors         = { initial  = {} },
                },
            
    execute = function(dz, device)
 
        local name          = device.name
        local name2 = USAGE_SWITCHES[name]

        if dz.data.prevhistwaarde.getOldest() == nil then  -- Check if history is already filled (initial does not work with history persistent data
          dz.data.prevhistwaarde.add(0)  -- eerste waarde op 0 gezet
        end

        if dz.data.sensors[name] == nil then 
            dz.data.sensors[name] = 0       -- eerste waarde teller op 0 gezet  -- geen notify gedaan
        end
        if dz.data.sendnotification[name] == nil then 
            dz.data.sendnotification[name] = false       -- eerste waarde teller op false gezet  -- geen notify gedaan
        end

        local huidwaarde    = dz.utils.round(device.usage,1)
        local prevwaarde    = dz.utils.round(dz.data.prevhistwaarde.getLatest().data,1)             --local prevwaarde5   = dz.utils.round(dz.data.prevhistwaarde.get(5).data,1)
        local gemwaarden    = dz.utils.round(dz.data.prevhistwaarde.avgSince('00:05:00'),1)
        local avgwaarde     = dz.utils.round(dz.data.prevhistwaarde.avg(),1)
        local watt          = USAGE_Watt[USAGE_DEVICES[name]]
        local DurTimer      = USAGE_DurTimer[USAGE_DEVICES[name]]
        local minwaarde     = dz.utils.round(dz.data.prevhistwaarde.min(),1)
        local maxwaarde     = dz.utils.round(dz.data.prevhistwaarde.max(),1)
        local notify        = USAGE_Notify[USAGE_DEVICES[name]]
        local receivers     = USAGE_Pushoverreceivers[USAGE_DEVICES[name]]
        local sound         = USAGE_Pushoversound[USAGE_DEVICES[name]]

        for i = dz.data.prevhistwaarde.size,1,-1 do 
            item = dz.data.prevhistwaarde.get(i)
            dz.log("00 History data van " .. item.time.rawDate .. " " .. item.time.rawTime .. " = " .. item.data .. " watt")
        end
        dz.log("00 Sensor kwh device    = " .. name)
        dz.log("00 Sensor switch device = " .. name2)
        dz.log("00 Sensor notify        = " .. notify)
        dz.log("00 Sensor receivers     = " .. receivers)
        dz.log("00 Sensor sound         = " .. sound)
        dz.log("-------------------------------------------------")
        dz.log("00 Sensor watt norm     = " .. watt)
        dz.log("00 Sensor notif. norm   = " .. DurTimer)
        dz.log("00 Sensor notif. teller = " .. tostring(dz.data.sensors[name]))
        dz.log("-------------------------------------------------")
        dz.log("00 Sensor huidige waarde= " .. huidwaarde)
        dz.log("00 Sensor vorige waarde = " .. prevwaarde)
        dz.log("00 Sensor gemid. 5min   = " .. gemwaarden)
        dz.log("00 Sensor gemid. hist   = " .. avgwaarde)
        dz.log("00 Sensor minimum waarde= " .. minwaarde)
        dz.log("00 Sensor maximum waarde= " .. maxwaarde)
        dz.log("00 Status notificatie   = " .. tostring(dz.data.sendnotification[name]))
        dz.log("-------------------------------------------------")

        dz.data.prevhistwaarde.add(huidwaarde)
        
         if dz.devices(name2).state == 'On' then
            if  gemwaarden <= watt then 
                dz.log("01 Sensor info         = Huidig gem waarde(".. gemwaarden ..") is onder de norm(".. watt .. "), device: " .. name ..  "")
                dz.data.sensors[name] = dz.data.sensors[name] + 1

                if dz.data.sensors[name] < DurTimer  then 
                    dz.log("04 Sensor info         = Timerperiode(".. tostring(DurTimer) .. ") is nog niet verstreken, huidige timer is: (" .. dz.data.sensors[name] .. ")")

                elseif dz.data.sensors[name] == DurTimer then
                    dz.log("03 Sensor info         = Verbruik van device: " .. name ..  " is lange tijd onder de norm, mogelijk is deze kapot!")

                    if (notify == 'Yes') and (dz.data.sendnotification[name] == false) then
                        dz.notify('03 Sensor info         = Gemiddeld verbruik('.. gemwaarden .. ') van device:' .. name2 .. ' is lange tijd('.. tostring(DurTimer) .. '), onder de norm('.. watt .. '), mogelijk is deze kapot!',
                        name,PRIORITY_NORMAL,sound,receivers,dz.NSS_PUSHOVER)
                        dz.data.sendnotification[name] = true       -- eerste waarde teller op 0 gezet  -- registreren dat notificatie is gedaan
                    end

                else
                    dz.log("04 Sensor info         = Timerperiode(".. tostring(DurTimer) .. ") is al voorbij(" .. dz.data.sensors[name] .. "), Gemmideld verbruik(" .. gemwaarden .. ") van device: " .. name ..  " is  lange tijd onder de norm(".. watt .. "), mogelijk is deze kapot!")

                end
            elseif gemwaarden > watt then 
                --dz.data.prevhistwaarde.reset()  
                dz.data.sensors[name] = 0
                dz.data.sendnotification[name] = false
                dz.log("05 Sensor info         = Gemiddeld verbruik van device: " .. name ..  " met huidig waarde(".. gemwaarden ..") is boven de norm(".. watt .. "), device: " .. name ..  " gebruikt stroom")
            end
        else
            dz.data.sensors[name] = 0
            dz.data.sendnotification[name] = false
        end
    end
}


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

Re: Persistent data as a variable

Post by waaren » Tuesday 11 December 2018 3:58

pvklink wrote:
Monday 10 December 2018 19:36
There is one thing i like to optimize. I use dz.data.sensors[name] as a counter and when it reaches USAGE_DurTimer then a notification could be send.The number of USAGE_DurTimer is a value that represense time. Is it possible to use actuel time?
Could be something like the code below ** Untested so keep current script in a safe place.

Code: Select all

-- test met stroomlint
-- boven maxwatt dan werkt t lint 
-- onder maxwatt en geijk aan switchoutminutes dan is het lint mogelijk kapot of is toch de thermostaat aan. dan is maxwatt niet juist gekozen
-- evt kijken naar tijdstip van teller =1 en dan waarde in uren
-- ipv huidwaarde en prevwaarde de laatste 5 waarde nemen om pieken te voorkomen, historie op 5 zetten en die nemen
-- https://github.com/dannybloe/dzVents

local USAGE_DEVICES = {
    ['buro_kwh'] = 'buro_kwh',                -- Adjust to your needs. Between every line you need to add a ",".
}

local USAGE_SWITCHES = {
    ['buro_kwh'] = 'buro',          -- Name of device that switch kw_1 On/ Off 
} 

local USAGE_Watt = {
    ['buro_kwh'] = 110,                    -- There is powerconsumption
}

local USAGE_DurTimer = {
    ['buro_kwh'] = 1800,                -- Timer in seconds       
}

local USAGE_Notify = {
    ['buro_kwh'] = 'Yes',
}

local USAGE_Pushoverreceivers = {
    ['buro_kwh'] = 'galaxya52017',
}

local USAGE_Pushoversound = {
    ['buro_kwh'] = 'siren',
}

return {
    on = {devices = USAGE_DEVICES,
        },

    logging = { level   = domoticz.LOG_DEBUG ,                   -- Uncomment to override the dzVents global logging setting
                marker  = actOnIdleDevices 
             },
              
    data    =   {
                    prevhistwaarde  = { history = true, maxHours = 4 },     --Deze tijdspanne moet je juist kiezen zodat het lint ook de kans krijgt om aan te gaan en boven t gemiddelde te komen
                    sendnotification = {initial = {}}, 
                    sensors         = { initial  = {} },
                },
            
    execute = function(dz, device)
 
        local name          = device.name
        local name2 = USAGE_SWITCHES[name]

        if dz.data.prevhistwaarde.getOldest() == nil then  -- Check if history is already filled (initial does not work with history persistent data
          dz.data.prevhistwaarde.add(0)  -- eerste waarde op 0 gezet
        end

        if dz.data.sensors[name] == nil then 
            dz.data.sensors[name] = 0       -- eerste waarde teller op 0 gezet  -- geen notify gedaan
        end
        if dz.data.sendnotification[name] == nil then 
            dz.data.sendnotification[name] = false       -- eerste waarde teller op false gezet  -- geen notify gedaan
        end

        local huidwaarde    = dz.utils.round(device.usage,1)
        local prevwaarde    = dz.utils.round(dz.data.prevhistwaarde.getLatest().data,1)             --local prevwaarde5   = dz.utils.round(dz.data.prevhistwaarde.get(5).data,1)
        local gemwaarden    = dz.utils.round(dz.data.prevhistwaarde.avgSince('00:05:00'),1)
        local avgwaarde     = dz.utils.round(dz.data.prevhistwaarde.avg(),1)
        local watt          = USAGE_Watt[USAGE_DEVICES[name]]
        local DurTimer      = USAGE_DurTimer[USAGE_DEVICES[name]]
        local minwaarde     = dz.utils.round(dz.data.prevhistwaarde.min(),1)
        local maxwaarde     = dz.utils.round(dz.data.prevhistwaarde.max(),1)
        local notify        = USAGE_Notify[USAGE_DEVICES[name]]
        local receivers     = USAGE_Pushoverreceivers[USAGE_DEVICES[name]]
        local sound         = USAGE_Pushoversound[USAGE_DEVICES[name]]
		local rawTime       = dz.data.sensors[name .. "_rawTime"] or "" 

		
        for i = dz.data.prevhistwaarde.size,1,-1 do 
            item = dz.data.prevhistwaarde.get(i)
            dz.log("00 History data van " .. item.time.rawDate .. " " .. item.time.rawTime .. " = " .. item.data .. " watt")
        end
        dz.log("00 Sensor kwh device    = " .. name)
        dz.log("00 Sensor switch device = " .. name2)
        dz.log("00 Sensor notify        = " .. notify)
        dz.log("00 Sensor receivers     = " .. receivers)
        dz.log("00 Sensor sound         = " .. sound)
        dz.log("-------------------------------------------------")
        dz.log("00 Sensor watt norm     = " .. watt)
        dz.log("00 Sensor notif. norm   = " .. DurTimer)
        dz.log("00 Sensor notif. teller = " .. tostring(dz.data.sensors[name]))
        dz.log("-------------------------------------------------")
        dz.log("00 Sensor huidige waarde= " .. huidwaarde)
        dz.log("00 Sensor vorige waarde = " .. prevwaarde)
        dz.log("00 Sensor gemid. 5min   = " .. gemwaarden)
        dz.log("00 Sensor gemid. hist   = " .. avgwaarde)
        dz.log("00 Sensor minimum waarde= " .. minwaarde)
        dz.log("00 Sensor maximum waarde= " .. maxwaarde)
        dz.log("00 Status notificatie   = " .. tostring(dz.data.sendnotification[name]))
        dz.log("-------------------------------------------------")

        dz.data.prevhistwaarde.add(huidwaarde)
        
         if dz.devices(name2).state == 'On' then
            if  gemwaarden <= watt then 
				local now = os.time(os.date('*t'))
                dz.log("01 Sensor info         = Huidig gem waarde(".. gemwaarden ..") is onder de norm(".. watt .. "), device: " .. name ..  "")
				if dz.data.sensors[name] == 0 then
					dz.data.sensors[name] = now
					dz.data.sensors[name .. "_rawTime"] = dz.time.rawTime
					rawTime = dz.time.rawTime
				end
                if dz.data.sensors[name] > ( now - DurTimer ) then 
                    dz.log("04 Sensor info         = Timerperiode(".. tostring(DurTimer) .. ") is nog niet verstreken, huidige timer is set at: (" .. rawTime .. ")")

                elseif dz.data.sensors[name] < ( now - DurTimer) then
                    dz.log("03 Sensor info         = Verbruik van device: " .. name ..  " is lange tijd onder de norm, mogelijk is deze kapot!")

                    if (notify == 'Yes') and (dz.data.sendnotification[name] == false) then
                        dz.notify('03 Sensor info         = Gemiddeld verbruik('.. gemwaarden .. ') van device:' .. name2 .. ' is lange tijd('.. tostring(DurTimer) .. ' seconden), onder de norm('.. watt .. '), mogelijk is deze kapot!',
                        name,PRIORITY_NORMAL,sound,receivers,dz.NSS_PUSHOVER)
                        dz.data.sendnotification[name] = true       -- eerste waarde teller op 0 gezet  -- registreren dat notificatie is gedaan
                    end

                else
                    dz.log("04 Sensor info         = Timerperiode(".. tostring(DurTimer) .. ") is al voorbij(" .. dz.data.sensors[name] .. "), Gemmideld verbruik(" .. gemwaarden .. ") van device: " .. name ..  " is  lange tijd onder de norm(".. watt .. "), mogelijk is deze kapot!")

                end
            elseif gemwaarden > watt then 
                --dz.data.prevhistwaarde.reset()  
                dz.data.sensors[name] = 0
				dz.data.sensors[name].rawTime = nil            
				dz.data.sendnotification[name] = false
                dz.log("05 Sensor info         = Gemiddeld verbruik van device: " .. name ..  " met huidig waarde(".. gemwaarden ..") is boven de norm(".. watt .. "), device: " .. name ..  " gebruikt stroom")
            end
        else
            dz.data.sensors[name] = 0
            dz.data.sensors[name].rawTime = nil
            dz.data.sendnotification[name] = false
        end
    end
}
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

pvklink
Posts: 136
Joined: Wednesday 12 November 2014 16:01
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Persistent data as a variable

Post by pvklink » Tuesday 11 December 2018 9:25

Hi, Thanks!
I just tested it, got some errors and used your tricks to rewrite it a bit with a new variable in the data section for timer functions
data = {pvktimer = {initial = {}},

Everything seems to work great, many thanks to yoy @Waaren on behalve of my father in law, his waterpipes wont freeze this winter!

Last thing to do, is to publish time of the lines below not as a number but als actual time, is that possible?
My experience is dat dat/time conversion are terrible

dz.log("00 Notificatie.huidige tijd..............= " .. tostring(now))
dz.log("00 Notificatie.timer.....................= " .. tostring(dz.data.pvktimer[name]))

Can you take a final look at the code, if this is a decent workout?
Again, many thanks for your help, i am not a programmer, but with a little help i can make some spagetti-code :D
I am almost ready to convert all of my old lua-scripts in dzventz!

Code: Select all

-- test met stroomlint
-- boven maxwatt dan werkt t lint 
-- onder maxwatt en geijk aan switchoutminutes dan is het lint mogelijk kapot of is toch de thermostaat aan. dan is maxwatt niet juist gekozen
-- evt kijken naar tijdstip van teller =1 en dan waarde in uren
-- ipv huidwaarde en prevwaarde de laatste 5 waarde nemen om pieken te voorkomen, historie op 5 zetten en die nemen
-- https://github.com/dannybloe/dzVents

local USAGE_SENSORS = {
    ['buro_kwh'] = 'buro_kwh',                -- Adjust to your needs. Between every line you need to add a ",".
    --['Verbruikwarmtelint'] = 'Verbruikwarmtelint',                -- Adjust to your needs. Between every line you need to add a ",".
}

local USAGE_SWITCHES = {
    ['buro_kwh'] = 'buro',          -- Name of device that switch kw_1 On/ Off 
    --['Verbruikwarmtelint'] = 'stroomlint',          -- Name of device that switch kw_1 On/ Off 
} 

local USAGE_Watt = {
    ['buro_kwh'] = 110,                    -- There is powerconsumption
    --['Verbruikwarmtelint'] = 1,                    -- There is powerconsumption
}

local USAGE_DurTimer = {
    ['buro_kwh'] = 300,                   --in seconden, tijd dat gemiddeld gebruik onder de norm zit, na deze periode evt n notificatie
    --['Verbruikwarmtelint'] = 200,                  
}

local USAGE_Notify = {
    ['buro_kwh'] = 'Yes',
   -- ['Verbruikwarmtelint'] = 'Yes',
}

local USAGE_Pushoverreceivers = {
    ['buro_kwh'] = 'galaxya52017',
    --['Verbruikwarmtelint'] = 'galaxya52017',
}

local USAGE_Pushoversound = {
    ['buro_kwh'] = 'siren',
    --['Verbruikwarmtelint'] = 'siren',
}

return {
    on = {devices = USAGE_SENSORS,
        },

    logging = { level   = domoticz.LOG_DEBUG ,                   -- Uncomment to override the dzVents global logging setting
                marker  = actOnIdleDevices 
             },
              
    data    =   {
                    prevhistwaarde  = { history = true, maxHours = 1 },     --
                    sendnotification = {initial = {}}, 
                    pvktimer = {initial = {}}, 
                    sensors         = { initial  = {} },
                },
            
    execute = function(dz, device)
 
        if dz.data.prevhistwaarde.getOldest() == nil then  -- Check if history is already filled (initial does not work with history persistent data
          dz.data.prevhistwaarde.add(0)  -- eerste waarde op 0 gezet
        end

        local name          = device.name
        local name2         = USAGE_SWITCHES[name]

        local notify        = USAGE_Notify[USAGE_SENSORS[name]]
        local receivers     = USAGE_Pushoverreceivers[USAGE_SENSORS[name]]
        local sound         = USAGE_Pushoversound[USAGE_SENSORS[name]]
        
        local watt          = USAGE_Watt[USAGE_SENSORS[name]]
        local DurTimer      = USAGE_DurTimer[USAGE_SENSORS[name]]
        local huidwaarde    = dz.utils.round(device.usage,1)
        local prevwaarde    = dz.utils.round(dz.data.prevhistwaarde.getLatest().data,1)             --local prevwaarde5   = dz.utils.round(dz.data.prevhistwaarde.get(5).data,1)
        local gemwaarden    = dz.utils.round(dz.data.prevhistwaarde.avgSince('00:05:00'),1)
        local avgwaarde     = dz.utils.round(dz.data.prevhistwaarde.avg(),1)
        local minwaarde     = dz.utils.round(dz.data.prevhistwaarde.min(),1)
        local maxwaarde     = dz.utils.round(dz.data.prevhistwaarde.max(),1)
        local now           = os.time(os.date('*t'))                      

        if dz.data.sensors[name] == nil then 
            dz.data.sensors[name] = 0       -- eerste waarde teller op 0 gezet  -- geen notify gedaan
        end
        if dz.data.sendnotification[name] == nil then 
            dz.data.sendnotification[name] = false       -- eerste waarde teller op false gezet  -- geen notify gedaan
        end
        if dz.data.pvktimer[name] == nil then 
            dz.data.pvktimer[name] = '0'       --eerste waarde op 0 gezet
        end

        for i = dz.data.prevhistwaarde.size,1,-1 do 
            item = dz.data.prevhistwaarde.get(i)
            dz.log("00 History data van " .. item.time.rawDate .. " " .. item.time.rawTime .. " = " .. item.data .. " watt")
        end
        dz.log("Deviceinfo-----------------------------------------")
        dz.log("00 Kwh-device............................= " .. name)
        dz.log("00 Switch-device.........................= " .. name2)
        dz.log("Verbruikinfo---------------------------------------")
        dz.log("00 Verbruik.norm.........................= " .. watt)
        dz.log("00 Verbruik.gemid. 5min..................= " .. gemwaarden)
        dz.log("00 Verbruik.vorige waarde................= " .. prevwaarde)
        dz.log("00 Verbruik.gemid. hist..................= " .. avgwaarde)
        dz.log("00 Verbruik.minimumwaarde................= " .. minwaarde)
        dz.log("00 Verbruik.maximumwaarde................= " .. maxwaarde)
        dz.log("Notificatie-----------------------------------------")
        dz.log("00 Notificatie.norm(in.min)..............= " .. DurTimer/60)
        dz.log("00 Notificatie.huidige tijd..............= " .. tostring(now))
        dz.log("00 Notificatie.timer.....................= " .. tostring(dz.data.pvktimer[name]))
        if now <= tonumber(dz.data.pvktimer[name]) then
            dz.log("00 Notificatie.timer resterend (in.sec)..= " .. tostring(dz.utils.round(tonumber(dz.data.pvktimer[name]) - now,1)))
        else
            dz.log("00 Notificatie.timer resterend (in.sec)..= " .. '0')
        end
        dz.log("00 Notificatie.verzonden.................= " .. tostring(dz.data.sendnotification[name]))
        dz.log("00 Notificatie.teller....................= " .. tostring(dz.data.sensors[name]))
        dz.log("00 Notificatie.aan.......................= " .. notify)
        dz.log("00 Notificatie.receivers.................= " .. receivers)
        dz.log("00 Notificatie.sound.....................= " .. sound)
        dz.log("---------------------------------------------------")

        dz.data.prevhistwaarde.add(huidwaarde)
        
         if dz.devices(name2).state == 'On' then
            if  gemwaarden <= watt then
                dz.log("01 Sensor info         = Gemiddeld verbruik van device: " .. name ..  " met huidig gemiddeld verbruik: (".. gemwaarden ..") is onder de norm(".. watt .. ")")

                if dz.data.sensors[name] == 0 then                        
	                dz.data.pvktimer[name] = tostring(now+DurTimer)
				end                                                     
                dz.data.sensors[name] = dz.data.sensors[name] + 1

                if tonumber(now)  < ( tonumber(dz.data.pvktimer[name])) then         
                    dz.log("02 Sensor info         = Timerperiode(".. tostring(DurTimer/60) .. "Min) is nog niet verstreken, huidige timer is set at: (" .. dz.data.pvktimer[name] .. ")")           --TOEVOEGING
                else       
                    if (dz.data.sendnotification[name] == false) then
                        dz.log("03 Sensor info         = Gemiddeld verbruik van device: " .. name ..  " is lange tijd onder de norm, deze is defect!")
                        
                        if (notify == 'Yes') then 
                            dz.notify('03 Sensor info         = Gemiddeld verbruik('.. gemwaarden .. ') van device:' .. name2 .. ' is lange tijd('.. tostring(DurTimer/60) .. 'min), onder de norm('.. watt .. '), deze is defect!',
                            name,PRIORITY_NORMAL,sound,receivers,dz.NSS_PUSHOVER)
                        end
                        
                        dz.data.sendnotification[name] = true
                    else
                        dz.log("04 Sensor info         = Gemiddeld verbruik(".. gemwaarden .. ") van device:" .. name2 .. " is lange tijd(".. tostring(DurTimer/60) .. "min), onder de norm(".. watt .. "), deze is defect! melding is reeds verzonden")
                    end
                end
            elseif gemwaarden > watt then 
                dz.data.sensors[name]  = 0
                dz.data.pvktimer[name] = '0' 
                dz.data.sendnotification[name] = false
                dz.log("05 Sensor info         = Gemiddeld verbruik van device: " .. name ..  " met huidig gemiddeld verbruik: (".. gemwaarden ..") is boven de norm(".. watt .. "), en werkt dus correct")
            end
        else
            --dz.data.prevhistwaarde.reset()  
            dz.data.sensors[name]  = 0
            dz.data.pvktimer[name] = '0' 
            dz.data.sendnotification[name] = false
        end
    end
}


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

Re: Persistent data as a variable

Post by waaren » Tuesday 11 December 2018 23:31

pvklink wrote:
Tuesday 11 December 2018 9:25
Last thing to do, is to publish time of the lines below not as a number but als actual time, is that possible?
My experience is dat dat/time conversion are terrible

dz.log("00 Notificatie.huidige tijd..............= " .. tostring(now))
dz.log("00 Notificatie.timer.....................= " .. tostring(dz.data.pvktimer[name]))
To get a time string you can change this to

Code: Select all

        dz.log("00 Notificatie.huidige tijd..............= " .. os.date("%X", now))
        dz.log("00 Notificatie.timer.....................= " ..os.date("%X",dz.data.pvktimer[name]))
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

pvklink
Posts: 136
Joined: Wednesday 12 November 2014 16:01
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Persistent data as a variable

Post by pvklink » Wednesday 12 December 2018 0:30

Ok, this is much more easier then expected! THANKS agaian!

Almost ready with converting all my LUA scripts
Never asked the question if dzventz is a beter scripting language, but it looks more powerful...

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

Re: Persistent data as a variable

Post by waaren » Wednesday 12 December 2018 1:49

pvklink wrote:Ok, this is much more easier then expected! THANKS agaian!

Almost ready with converting all my LUA scripts
Never asked the question if dzventz is a beter scripting language, but it looks more powerful...
dzVents is 100% Lua
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

pvklink
Posts: 136
Joined: Wednesday 12 November 2014 16:01
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Persistent data as a variable

Post by pvklink » Wednesday 12 December 2018 11:59

Got it almost working, one strange thing, see output:

us: dzVents: Info: Notificatie-----------------------------------------
2018-12-12 10:55:27.646 Status: dzVents: Info: 00 Notificatie.norm(in.min)............= 5
2018-12-12 10:55:27.647 Status: dzVents: Info: 00 Notificatie.huidige tijd..............= 10:55:27
2018-12-12 10:55:27.647 Status: dzVents: Info: 00 Notificatie.eind tijd.................= 10:55:57
2018-12-12 10:55:27.647 Status: dzVents: Info: 00 Notificatie.resterende tijd.........= 01:00:30
2018-12-12 10:55:27.647 Status: dzVents: Info: 00 Notificatie.verzonden..............= false
2018-12-12 10:55:27.647 Status: dzVents: Info: 00 Notificatie.teller.....................= 77

The difference between curent time and timer was at this moment 30 seconds
At line 4 you see: 01:00:30, an hour and 30 seconds!
It works great, it does give a notification at 10:55:57, so it must be the presentation of the variable

I have all my LUA scripts rewritten, i am finished! Thanks @Waaren, no more LUA for me...
ANd they work with my raspberry pi google home assistent!

Code: Select all

-- test met stroomlint
-- boven norm dan werkt t lint 
-- onder norm en geijk aan switchoutminutes dan is het lint mogelijk kapot of is toch de thermostaat aan. dan is norm niet juist gekozen
-- evt kijken naar tijdstip van teller =1 en dan waarde in uren
-- ipv huidwaarde en prevwaarde de laatste 5 waarde nemen om pieken te voorkomen, historie op 5 zetten en die nemen
-- https://github.com/dannybloe/dzVents

local USAGE_SENSORS = {
    ['buro_kwh'] = 'buro_kwh',                -- Adjust to your needs. Between every line you need to add a ",".
    --['Verbruikwarmtelint'] = 'Verbruikwarmtelint',                -- Adjust to your needs. Between every line you need to add a ",".
}

local USAGE_SWITCHES = {
    ['buro_kwh'] = 'buro',          -- Name of device that switch kw_1 On/ Off 
    --['Verbruikwarmtelint'] = 'stroomlint',          -- Name of device that switch kw_1 On/ Off 
} 

local USAGE_Norm = {
    ['buro_kwh'] = 110,                    -- There is powerconsumption
    --['Verbruikwarmtelint'] = 1,                    -- There is powerconsumption
}

local USAGE_DurTimer = {
    ['buro_kwh'] = 300,                   --in seconden, tijd dat gemiddeld gebruik onder de norm zit, na deze periode evt n notificatie, 300 is 5 minuten
    --['Verbruikwarmtelint'] = 200,                  
}

local USAGE_Notify = {
    ['buro_kwh'] = 'Yes',
   -- ['Verbruikwarmtelint'] = 'Yes',
}

local USAGE_Pushoverreceivers = {
    ['buro_kwh'] = 'galaxya52017',
    --['Verbruikwarmtelint'] = 'galaxya52017',
}

local USAGE_Pushoversound = {
    ['buro_kwh'] = 'siren',
    --['Verbruikwarmtelint'] = 'siren',
}

return {
    on = {devices = USAGE_SENSORS,},

    logging = { level   = domoticz.LOG_DEBUG ,marker  = actOnIdleDevices },
              
    data    =   {
                    prevhistwaarde  = { history = true, maxHours = 1 },     --
                    sendnotification = {initial = {}}, 
                    pvktimer = {initial = {}}, 
                    sensors         = { initial  = {} },
                },
            
    execute = function(dz, device)
 
        if dz.data.prevhistwaarde.getOldest() == nil then  -- Check if history is already filled (initial does not work with history persistent data
          dz.data.prevhistwaarde.add(0)  -- eerste waarde op 0 gezet
        end

        local name          = device.name
        local name2         = USAGE_SWITCHES[name]

        local notify        = USAGE_Notify[USAGE_SENSORS[name]]
        local receivers     = USAGE_Pushoverreceivers[USAGE_SENSORS[name]]
        local sound         = USAGE_Pushoversound[USAGE_SENSORS[name]]
        
        local norm          = USAGE_Norm[USAGE_SENSORS[name]]
        local DurTimer      = USAGE_DurTimer[USAGE_SENSORS[name]]
        local huidwaarde    = dz.utils.round(device.usage,1)
        local prevwaarde    = dz.utils.round(dz.data.prevhistwaarde.getLatest().data,1)             --local prevwaarde5   = dz.utils.round(dz.data.prevhistwaarde.get(5).data,1)
        local gemwaarden    = dz.utils.round(dz.data.prevhistwaarde.avgSince('00:05:00'),1)
        local avgwaarde     = dz.utils.round(dz.data.prevhistwaarde.avg(),1)
        local minwaarde     = dz.utils.round(dz.data.prevhistwaarde.min(),1)
        local maxwaarde     = dz.utils.round(dz.data.prevhistwaarde.max(),1)
        local now           = os.time(os.date('*t'))                      
        dz.data.prevhistwaarde.add(huidwaarde)

        if dz.data.sensors[name] == nil then 
            dz.data.sensors[name] = 0       -- eerste waarde teller op 0 gezet  -- geen notify gedaan
        end
        if dz.data.sendnotification[name] == nil then 
            dz.data.sendnotification[name] = false       -- eerste waarde teller op false gezet  -- geen notify gedaan
        end
        if dz.data.pvktimer[name] == nil then 
            dz.data.pvktimer[name] = '0'       --eerste waarde op 0 gezet
        end

        for i = dz.data.prevhistwaarde.size,1,-1 do 
            item = dz.data.prevhistwaarde.get(i)
            dz.log("00 History data van " .. item.time.rawDate .. " " .. item.time.rawTime .. " = " .. item.data .. " norm")
        end
        dz.log("Deviceinfo-----------------------------------------")
        dz.log("00 Kwh-device............................= " .. name)
        dz.log("00 Switch-device.........................= " .. name2)
        dz.log("Verbruikinfo---------------------------------------")
        dz.log("00 Verbruik.norm.........................= " .. norm)
        dz.log("00 Verbruik.gemid. 5min..................= " .. gemwaarden)
        dz.log("00 Verbruik.huidige waarde...............= " .. huidwaarde)
        dz.log("00 Verbruik.vorige waarde................= " .. prevwaarde)
        dz.log("00 Verbruik.gemid. hist..................= " .. avgwaarde)
        dz.log("00 Verbruik.minimumwaarde................= " .. minwaarde)
        dz.log("00 Verbruik.maximumwaarde................= " .. maxwaarde)
        dz.log("Notificatie-----------------------------------------")
        dz.log("00 Notificatie.norm(in.min)..............= " .. DurTimer/60)
        dz.log("00 Notificatie.huidige tijd..............= " .. os.date("%X", now))

        if tonumber(dz.data.pvktimer[name]) == 0 then
            dz.log("00 Notificatie.eind tijd.................= " .. 'n.v.t.')
        else
            dz.log("00 Notificatie.eind tijd.................= " .. os.date("%X",dz.data.pvktimer[name]))
        end

        if now <= tonumber(dz.data.pvktimer[name]) then
            dz.log("00 Notificatie.resterende tijd...........= " .. os.date("%X",dz.data.pvktimer[name] - now))
        else
            dz.log("00 Notificatie.resterende tijd...........= " .. 'n.v.t.')
        end

        dz.log("00 Notificatie.verzonden.................= " .. tostring(dz.data.sendnotification[name]))
        dz.log("00 Notificatie.teller....................= " .. tostring(dz.data.sensors[name]))
        dz.log("00 Notificatie.aan.......................= " .. notify)
        dz.log("00 Notificatie.receivers.................= " .. receivers)
        dz.log("00 Notificatie.sound.....................= " .. sound)
        dz.log("---------------------------------------------------")

        --dz.data.prevhistwaarde.add(huidwaarde)
        
         if dz.devices(name2).state == 'On' then
            if  gemwaarden <= norm then
                dz.log("01 Sensor info         = Gemiddeld verbruik van device: " .. name ..  " met huidig gemiddeld verbruik: (".. gemwaarden ..") is onder de norm(".. norm .. ")")

                if dz.data.sensors[name] == 0 then                        
	                dz.data.pvktimer[name] = tostring(now+DurTimer)
				end                                                     
                dz.data.sensors[name] = dz.data.sensors[name] + 1

                if tonumber(now)  < ( tonumber(dz.data.pvktimer[name])) then         
                    dz.log("02 Sensor info         = Timerperiode(".. tostring(DurTimer/60) .. "Min) is nog niet verstreken, huidige timer is set at: (" .. dz.data.pvktimer[name] .. ")")           --TOEVOEGING
                else       
                    if (dz.data.sendnotification[name] == false) then
                        dz.log("03 Sensor info         = Gemiddeld verbruik van device: " .. name ..  " is lange tijd onder de norm, deze is defect!")
                        
                        if (notify == 'Yes') then 
                            dz.notify('03 Sensor info         = Gemiddeld verbruik('.. gemwaarden .. ') van device:' .. name2 .. ' is lange tijd('.. tostring(DurTimer/60) .. 'min), onder de norm('.. norm .. '), deze is defect!',
                            name,PRIORITY_NORMAL,sound,receivers,dz.NSS_PUSHOVER)
                        end
                        
                        dz.data.sendnotification[name] = true
                    else
                        dz.log("04 Sensor info         = Gemiddeld verbruik(".. gemwaarden .. ") van device:" .. name2 .. " is lange tijd(".. tostring(DurTimer/60) .. "min), onder de norm(".. norm .. "), deze is defect! melding is reeds verzonden")
                    end
                end
            elseif gemwaarden > norm then 
                dz.data.sensors[name]  = 0
                dz.data.pvktimer[name] = '0' 
                dz.data.sendnotification[name] = false
                dz.log("05 Sensor info         = Gemiddeld verbruik van device: " .. name ..  " met huidig gemiddeld verbruik: (".. gemwaarden ..") is boven de norm(".. norm .. "), en werkt dus correct")
            end
        else
            --dz.data.prevhistwaarde.reset()  
            dz.data.sensors[name]  = 0
            dz.data.pvktimer[name] = '0' 
            dz.data.sendnotification[name] = false
        end
    end
}


Post Reply

Who is online

Users browsing this forum: Derik and 2 guests