Page 1 of 3

Updating incremental counter with TIME value / json

Posted: Monday 04 June 2018 1:26
by FrankVZ
Is there a way, to store a value - an user variable - into a dummy counter, that measures time?

I tried a lot of different settings, but without any luck

I would like to create a simple run time counter, since I have to replace a special device every 8000 running hours.

So I made a simple blockly, that should write a new value of 1 minute to an incremental dummy counter.

But whatever I try, the counter does not show any activity.

I also tried different json commands, in order to increase the counter, but without any luck too...
e.g. http://<ip>:<port>/json.htm?type=command&param=udevice&idx=88&nvalue=59
The result is always "ok", but no sign/values change within the counter (still got the question-marks).

If I set the counter to "counter" the value increases. But this gives me only the result in minutes, and hours would be sufficient :)

The device 89 is a dummy hardware counter, with the type "time" (instead of count, energy, gas, water etc).

What would be the correct way, to increase the the counter every minute?

Re: Updating incremental counter with TIME value / json

Posted: Monday 04 June 2018 14:11
by FrankVZ
Have been searching too long today, and cant find answers.....

Maybe another idea to implement the runtime counter:

Running a python script from within blocky.

Script starts from within Blockly and stops if device started, stopped of day change (after 24:00hrs).
The variables should be then
TimeDeviceStart
TimeDeviceStop

Script should contain command line variables and would be like (e.g.)

RunTimeCalc.py <deviceIdx> <TimeDeviceStart> <TimeDeviceStop>
(no need to create a script for every counter needed)

The scripts might receive the data with a json command
http://localhost:<port>/json.htm?type=command&param=getuservariable&idx=<deviceIdx>

After calculation TimeDeviceStop-TimeDeviceStrat I would like to write the result into the Domoticz incremental counter (the hours / the rest are left-over minutes and must be restored to the runtime variable).
This can be done with a json http call too. E.g.
requests.get("localhost:<port>/json.htm?type=command&param=udevice&idx=%s&svalue=%d" % (server, port, deviceIdx, value))

But I have really no idea how to start with this script since I'm not familiar with Python (or other recent scripting languages) and the Wiki's didnt bring the right examples - to understand the script anywat :).

I googled a simple calculation in Python that does the job if the CMD is used (and tested it).....
def getime(prom):
"""Prompt for input, return minutes since midnight"""
s = raw_input('Enter time-%s (hh:mm): ' % prom)
sh, sm = s.split(':')
return int(sm) + 60 * int(sh)
time1 = getime('1')
time2 = getime('2')
diff = time2 - time1
print "Difference: %d hours and %d minutes" % (diff//60, diff%60)

How do I get the values from Blockly in Python script with parameters from the command line and how to export the results afterwards into the timer?
Is there anyone able to help or give e good directions (for a new-be)?

Re: Updating incremental counter with TIME value / json

Posted: Monday 04 June 2018 14:53
by freijn
What about this :

Have a python script started by cron every min
have the python script checking if the device runs if yes the + the number minutes
have python increment the device counter
You could update a text device with a calculated hours from min to hours and even calculate hours to go

main message : use a single script and do not jump from blocky into python and so.. :-)

Re: Updating incremental counter with TIME value / json

Posted: Monday 04 June 2018 16:37
by waaren
FrankVZ wrote: Monday 04 June 2018 1:26 Is there a way, to store a value - an user variable - into a dummy counter, that measures time?

I tried a lot of different settings, but without any luck
I would like to create a simple run time counter, since I have to replace a special device every 8000 running hours.
So I made a simple blockly, that should write a new value of 1 minute to an incremental dummy counter.
But whatever I try, the counter does not show any activity.
I also tried different json commands, in order to increase the counter, but without any luck too...
e.g. http://<ip>:<port>/json.htm?type=command&param=udevice&idx=88&nvalue=59
The result is always "ok", but no sign/values change within the counter (still got the question-marks).
What would be the correct way, to increase the the counter every minute?
I am sure there are more ways to get what you want but one method is to use the information already available in the domoticz database.

Implemented in dzVents it could be something like this:
Spoiler: show

Code: Select all

--[[ getUptime.lua for [ dzVents >= 2.4 ] 
]]--

return {
    on      =   {   timer           =   { "every day" },                        -- Triggers the getJsonPart
                    devices      =      { "myActivator" },                      -- Test and develop switch
                    httpResponses   =   { "getUptimeHstory" }            -- Trigger the handle Json part
                },
                
    logging =   {   level     =   domoticz.LOG_DEBUG,
                    marker    =   "getUptime"    },

    data    =   {   hours                = { initial = 0    },
                    lastIndex            = { initial = 0  }},
 
        
    execute = function(dz,trigger)

        local myDevice     = dz.devices(nnn)          -- name enclosed in quotes or number without 
        local myTextDevice = dz.devices("hourCounter")          -- name enclosed in quotes or number without 
        
        local function updateSensor()
            if myTextDevice then
                local myText = "\nRuntime in hours for device " .. myDevice.name .. " ==>>  ".. dz.utils.round(dz.data.hours)  
                if myTextDevice.text ~= myText then
                    myTextDevice.updateText( myText )
                end    
            end  
        end
        
        
        local function triggerJSON()
            local  URLString   = dz.settings['Domoticz url'] .. "/json.htm?type=lightlog&idx=" .. myDevice.idx
             
            dz.openURL({    url = URLString,
                            method = "GET",
                            callback = "getUptimeHstory" })                      
        end

        
        if trigger.isTimer or trigger.isDevice then
            triggerJSON()
        elseif trigger.ok then                                      -- statusCode == 2xx
            local Time = require('Time')
            
            local rt ={}
            rt = trigger.json.result

            local t1,t2, deltaTime
            local lastIndexOff = tonumber(dz.data.lastIndex)
            local lastIndexOn = 1
            

            for i=#rt,1,-1 do                                        -- Loop through the result
                t1 = Time(rt[i].Date)                                
                t2 = t1
                
                if rt[i].Data == "Off" and (tonumber(rt[i].idx) > lastIndexOff) then
                    lastIndexOff = tonumber(rt[i].idx)
                    dz.log("Outer idx: " .. rt[i].idx .. ", State: " .. rt[i].Data .. ", Date/time: " .. rt[i].Date  ,dz.LOG_DEBUG)
                    for j=i+1,#rt,1 do
                        if rt[j].Data == "On" and (tonumber(rt[j].idx) > lastIndexOn) then
                            lastIndexOn = tonumber(rt[j].idx)
                            dz.log("Inner idx: " .. rt[j].idx .. ", State: " .. rt[j].Data .. ", Date/time: " .. rt[j].Date  ,dz.LOG_DEBUG)
                            t2 = Time(rt[j].Date)
                            deltaTime =  t1.compare(t2).secs   
                            dz.log( "\nidx: " .. rt[j].idx .. ", State: " .. rt[j].Data .. ", Date/time: " .. rt[j].Date .. 
                                    ", Delta time: " .. deltaTime ..
                                    "\nidx: " .. rt[i].idx .. ", State: " .. rt[i].Data .. ", Date/time: " .. rt[i].Date 
                                    ,dz.LOG_DEBUG)
                            dz.data.hours = dz.data.hours + deltaTime / 3600
                            break
                        end
                    end
                end    
             end
             dz.log("Current amount of hours on: " .. dz.utils.round(dz.data.hours,1) ,dz.LOG_INFO)
             updateSensor()
             dz.data.lastIndex = lastIndexOff
        else
            dz.log("getRepsonse() : Could not get data from domoticz" ,dz.LOG_ERROR)
        end
    end
}
Script uses the information in lightlog and calculates the delta between the switchOff and switchOn states.
I now trigger the script once a day but can be done less frequent as long it's is more frequent than the setting in [SETTINGS] [SETUP] [LOG_HISTORY]
It keeps track which entries are already processed by means of the index of the log-entries.
It now writes the amount of hours into a text device but can do a similar action for all type of devices.

[EDIT] now also handles multiple consecutive "Off" / "On" states

Re: Updating incremental counter with TIME value / json

Posted: Tuesday 05 June 2018 10:00
by waaren
Or with a more realtime approach but still not having to execute every minute..

Code: Select all

 --[[ getUptime.lua for [ dzVents >= 2.4 ] 
]]--

return {
    on      =   {   devices      =      { nnn }},      -- name enclosed in quotes or IDX number without quotes              
                
    logging =   {   level     =   domoticz.LOG_DEBUG,
                    marker    =   "getUptime"    },

    data    =   {   lastState       = { initial = "Off"   },
                    secondsOn       = { initial = 0 },
                    lastSwitchTime  = { initial = "0" }},

    execute = function(dz,trigger)
        local myTextDevice = dz.devices("hourCounter")          -- name enclosed in quotes or number without 
                                                                -- ( or remove if you don't want textDevice ) 
        local function updateSensor()
            if myTextDevice then
                local myText = "\nRuntime in hours for device " .. trigger.name .. " ==>>  ".. dz.utils.round( tonumber(dz.data.secondsOn) / 3600 )  
                if myTextDevice.text ~= myText then
                    myTextDevice.updateText( myText )
                end    
            end  
        end
  
        if trigger.state ~= dz.data.lastState then
            if trigger.state == "Off" then 
                local Time = require('Time')    
                t1 = Time(dz.time.rawDate .." " .. dz.time.rawTime)
                t2 = Time(dz.data.lastSwitchTime)
                deltaTime =  t1.compare(t2).secs              
                dz.data.secondsOn       =   dz.data.secondsOn + deltaTime  
                updateSensor()
            end   
            dz.data.lastState       =   trigger.state
            dz.data.lastSwitchTime  =   dz.time.rawDate .. " " .. dz.time.rawTime
        end
        dz.log("State ==>> " .. trigger.state .. "; Date Time ==>> " .. dz.time.rawDate .." " .. 
                       dz.time.rawTime .. "; secondsOn ===>> " .. dz.data.secondsOn ,dz.LOG_DEBUG)
            
    end
}

Re: Updating incremental counter with TIME value / json

Posted: Tuesday 05 June 2018 17:32
by FrankVZ
Thanks Waaren for your effort!

I was hoping to test it today, but I noticed that dzVents and lua scripts didnt work (running the latest beta from jadahl). A problem with the default location of the dzVents folder and the actual location at my Synology. (oh man, this proces has a steep learning curve :( )

Anyway, took me a day, but now dzVents works so I'll try your suggestion! (re-installing older stable version, copy files, reinstall (not update) beta, all in CLI /SSH (since WinSCP is not useible for this)... a lot learned and I hope I'll remember.

I do agree that using the existing logs is much better, instead of creating them ourselves. But I read somewhere that these logs are not accessible, so I skipped this in my suggestion.... but now they are..... Great!

I'll keep it updated. As you may notice, I dont give up, but wow - this is consuming time to learn. So no answer today. But if it works - the "feel good" moment of the week :)

Re: Updating incremental counter with TIME value / json

Posted: Tuesday 05 June 2018 18:11
by waaren
Understand. It also took me quite some time to find answers on the different settings and directories on Synology NAS.
That and the fact that I could not compile myself on that platform were the main reasons why I moved to Raspberry with berryboot (Debian Stretch). This has also challenges but far less.

I still have a domoticz instance running on my Synology for test purposes and do use WinSCP to work on that system. What problem do you face with it ?

Re: Updating incremental counter with TIME value / json

Posted: Thursday 07 June 2018 23:40
by FrankVZ
Well, my main problem with the syno is to run winSCP and login with root, in order to drag-and-drop files on the syno. Doing this by CLI (Putty) is quite difficult - mistakes are easily made.

But that has been done now, amd dzVents runs within Domoticz. Unfortunatly python not, because the option Python is not shown within the events editor. So I have to run python scripts with the task scheduler from DSM6.1

That has an advantage too: I noticed that your second script causes domoticz to crash at midnight (form that moment I get notifications from Syno that the python script is causing errors - domoticz is off line.

So I gave it another try: running the first script you did show above. With a minor mod.
That has a strange effect too: I'm sure using the correct Idx - but whatever I try: using my Idx 8 as trigger, there happens a lot, but no counting. Instead my Idx 52 gets activated, while idx 8 and 52 are not connnected in any way! (8=my light in the study, 52 is a circulationpump from my pond in the garden)..... So there must be ghosts in town.....
Absolutely no clue what happens.

So I want the runtime counters. Best would be to use the counters that are build-in.
To update these counters I know the json command works, but a command from within dzVents should work too.
But what is wrong in the script? I try 3 different ways (the updateSensor() function), and none of them show an update within the counter so doesnt work.
I think I dont understand the wiki enough (the correct syntax) so there might be things wrong...
Spoiler: show
--[[ getUptime.lua for [ dzVents >= 2.4 ]
]]--

return {
on = { devices = { 8 }}, -- name enclosed in quotes or IDX number without quotes

logging = { level = domoticz.LOG_DEBUG,
marker = "getUptime" },

data = { lastState = { initial = "Off" },
secondsOn = { initial = 0 },
lastSwitchTime = { initial = "0" }},

execute = function(dz,trigger)
local myTextDevice = dz.devices("TestRuntimedzVents" ) -- name enclosed in quotes or number without
local myCounterName = dz.devices("Vijver runtime counter gewoon" ) -- tijdelijk even toegevoegd
-- ( or remove if you don't want textDevice )

--[[
local URLString = '192.168.1.12:8084/json.htm?type=command&param=udevice&idx=89&nvalue=TestJson&svalue=200'
dz.openURL(url = URLString )
end
]]--

local function updateSensor()
if myTextDevice then
local myText = "\nRuntime in hours for device " .. trigger.name .. " ==>> ".. dz.utils.round( tonumber(dz.data.secondsOn) / 3600 )
if myTextDevice.text ~= myText then
myTextDevice.updateText( myText )
myCounterName.updateCounter( tonumber(dz.data.secondsOn) )
domoticz.openURL('192.168.1.12:8084/json.htm?type=command&param=udevice&idx=89&svalue=200' )
end
end
end

if trigger.state ~= dz.data.lastState then
if trigger.state == "Off" then
local Time = require('Time')
t1 = Time(dz.time.rawDate .." " .. dz.time.rawTime)
t2 = Time(dz.data.lastSwitchTime)
deltaTime = t1.compare(t2).secs
dz.data.secondsOn = dz.data.secondsOn + deltaTime
updateSensor()
end
dz.data.lastState = trigger.state
dz.data.lastSwitchTime = dz.time.rawDate .. " " .. dz.time.rawTime
end
dz.log("State ==>> " .. trigger.state .. "; Date Time ==>> " .. dz.time.rawDate .." " ..
dz.time.rawTime .. "; secondsOn ===>> " .. dz.data.secondsOn ,dz.LOG_DEBUG)

end
}

Re: Updating incremental counter with TIME value / json

Posted: Friday 08 June 2018 0:35
by waaren
FrankVZ wrote: Thursday 07 June 2018 23:40 Well, my main problem with the syno is to run winSCP and login with root, in order to drag-and-drop files on the syno. Doing this by CLI (Putty) is quite difficult - mistakes are easily made.
I have the line sudo su - in advanced shell shell area of the winSCP session config. No problems to drag and drop files on my Synology
FrankVZ wrote: Thursday 07 June 2018 23:40 ut that has been done now, amd dzVents runs within Domoticz. Unfortunatly python not, because the option Python is not shown within the events editor. So I have to run python scripts with the task scheduler from DSM6.1
Do you use a version from Jadahl with Python included ?
FrankVZ wrote: Thursday 07 June 2018 23:40 That has an advantage too: I noticed that your second script causes domoticz to crash at midnight (form that moment I get notifications from Syno that the python script is causing errors - domoticz is off line.
Do you have any logs on this as the seconds script is triggered by a device event and is not based on time. ?
FrankVZ wrote: Thursday 07 June 2018 23:40 So I gave it another try: running the first script you did show above. With a minor mod.
That has a strange effect too: I'm sure using the correct Idx - but whatever I try: using my Idx 8 as trigger, there happens a lot, but no counting. Instead my Idx 52 gets activated, while idx 8 and 52 are not connnected in any way! (8=my light in the study, 52 is a circulationpump from my pond in the garden)..... So there must be ghosts in town.....
Absolutely no clue what happens.
Here also; very much interrested in domoticz logging. Could you please try to switch debug logging on and revert with the logs ?
FrankVZ wrote: Thursday 07 June 2018 23:40 So I want the runtime counters. Best would be to use the counters that are build-in.
To update these counters I know the json command works, but a command from within dzVents should work too.
But what is wrong in the script? I try 3 different ways (the updateSensor() function), and none of them show an update within the counter so doesnt work.
I think I dont understand the wiki enough (the correct syntax) so there might be things wrong...
the updateSensor() function as coded now is only working for text devices. For a counter device you will need another function / command.
Something like updateCounter(value)

Re: Updating incremental counter with TIME value / json

Posted: Friday 08 June 2018 14:29
by FrankVZ
waaren wrote: Friday 08 June 2018 0:35 I have the line sudo su - in advanced shell shell area of the winSCP session config. No problems to drag and drop files on my Synology
I dont know why, but for some reason it is advised for the synology to use the sudo -i instead (for the newer DSM versions).
See https://www.synology-forum.nl/algemeen/ ... nscp-putty

I tried that method, but still worried that you are able to login with a root account, without using the password. Might be a more sensitive for vulnerabilities...

For temporary situations looks good and can I drag and drop with WinSCP too. Afterwarts, I put the settings back to their original state.
waaren wrote: Friday 08 June 2018 0:35 Do you use a version from Jadahl with Python included ?
Without the Python option, because I use Python for other packages on the synology too. Installing Jadahls version with Python sounds like conflicts because of different Python versions?
waaren wrote: Friday 08 June 2018 0:35 Do you have any logs on this as the seconds script is triggered by a device event and is not based on time. ?
After Domoticz goes offline at 0:00hrs (crash?), all the content of the logs is erased. The first entry of the log is just from the moment I restarted the service within DSM.
Checked for other logs, but nothing seen....

I'll check tonight what happens if I first download the domoticz.log file with WinSCP, before restarting the service. Maybe there is some info left....

It might have to do with the calculation of 0:00 - 23:59, what gives a negative number that is supposed to be an integer?
waaren wrote: Friday 08 June 2018 0:35 Here also; very much interrested in domoticz logging. Could you please try to switch debug logging on and revert with the logs ?
I try tomorrow. see if I can reproduce this...
waaren wrote: Friday 08 June 2018 0:35 the updateSensor() function as coded now is only working for text devices. For a counter device you will need another function / command.
Something like updateCounter(value)
I think I tried that in the previous posting :) Must be a syntax thing and I give it another try.

Re: Updating incremental counter with TIME value / json

Posted: Monday 11 June 2018 13:47
by FrankVZ
Hi,
About the crashes: still every day at midnight.
I have another Syno DS (ds218+), and installed Domoticz too, but the latest stable version. Then restored the database of the running machine (ds214+) with the beta software (be aware of backward compatability issues?). But the DS218 does not crash....

Rebooting the service after the crash shows an empty log, and only contains the data from the reboot.

See the log:
Spoiler: show
2018-06-09 23:50:00.408 dzVents: Debug: Dumping domoticz data to /usr/local/domoticz/var/scripts/dzVents/domoticzData.lua
2018-06-09 23:50:00.470 dzVents: Debug: Event triggers:
2018-06-09 23:50:00.470 dzVents: Debug: - Timer
2018-06-09 23:50:50.783 (DarkSky) Temp + Humidity + Baro (THB)
2018-06-09 23:50:50.790 (DarkSky) Wind (Wind/temperatuur)
2018-06-09 23:50:50.793 (DarkSky) UV (UV Index)
2018-06-09 23:50:50.797 (DarkSky) Rain (Regen)
2018-06-09 23:50:50.800 (DarkSky) General/Visibility (Zicht)
2018-06-09 23:50:50.802 (DarkSky) General/Custom Sensor (Ozone Sensor)
2018-06-09 23:50:50.832 dzVents: Debug: Dumping domoticz data to /usr/local/domoticz/var/scripts/dzVents/domoticzData.lua
2018-06-09 23:50:50.927 dzVents: Debug: Processing device-adapter for THB: Temperature+humidity+barometer device adapter
2018-06-09 23:50:50.927 dzVents: Debug: dzVents version: 2.4.6
2018-06-09 23:50:50.927 dzVents: Debug: Event triggers:
2018-06-09 23:50:50.927 dzVents: Debug: - Device: THB
2018-06-09 23:50:51.161 dzVents: Debug: Dumping domoticz data to /usr/local/domoticz/var/scripts/dzVents/domoticzData.lua
2018-06-09 23:50:51.258 dzVents: Debug: Processing device-adapter for Wind/temperatuur: Wind device adapter
2018-06-09 23:50:51.259 dzVents: Debug: Processing device-adapter for UV Index: UV device adapter
2018-06-09 23:50:51.260 dzVents: Debug: Processing device-adapter for Regen: Rain device
2018-06-09 23:50:51.261 dzVents: Debug: Processing device-adapter for Zicht: Visibility device
2018-06-09 23:50:51.262 dzVents: Debug: Processing device-adapter for Ozone Sensor: Custom sensor device adapter
2018-06-09 23:50:51.263 dzVents: Debug: dzVents version: 2.4.6
2018-06-09 23:50:51.263 dzVents: Debug: Event triggers:
2018-06-09 23:50:51.263 dzVents: Debug: - Device: Wind/temperatuur
2018-06-09 23:50:51.263 dzVents: Debug: - Device: UV Index
2018-06-09 23:50:51.263 dzVents: Debug: - Device: Regen
2018-06-09 23:50:51.263 dzVents: Debug: - Device: Zicht
2018-06-09 23:50:51.263 dzVents: Debug: - Device: Ozone Sensor
2018-06-09 23:51:00.237 dzVents: Debug: Dumping domoticz data to /usr/local/domoticz/var/scripts/dzVents/domoticzData.lua
2018-06-09 23:51:00.310 dzVents: Debug: Event triggers:
2018-06-09 23:51:00.310 dzVents: Debug: - Timer
2018-06-09 23:52:00.107 dzVents: Debug: Dumping domoticz data to /usr/local/domoticz/var/scripts/dzVents/domoticzData.lua
2018-06-09 23:52:00.180 dzVents: Debug: Event triggers:
2018-06-09 23:52:00.180 dzVents: Debug: - Timer
2018-06-09 23:53:00.528 dzVents: Debug: Dumping domoticz data to /usr/local/domoticz/var/scripts/dzVents/domoticzData.lua
2018-06-09 23:53:00.600 dzVents: Debug: Event triggers:
2018-06-09 23:53:00.600 dzVents: Debug: - Timer
2018-06-09 23:54:00.327 dzVents: Debug: Dumping domoticz data to /usr/local/domoticz/var/scripts/dzVents/domoticzData.lua
2018-06-09 23:54:00.400 dzVents: Debug: Event triggers:
2018-06-09 23:54:00.400 dzVents: Debug: - Timer
2018-06-09 23:55:00.238 dzVents: Debug: Dumping domoticz data to /usr/local/domoticz/var/scripts/dzVents/domoticzData.lua
2018-06-09 23:55:00.310 dzVents: Debug: Event triggers:
2018-06-09 23:55:00.310 dzVents: Debug: - Timer
2018-06-09 23:55:54.829 (DarkSky) Temp + Humidity + Baro (THB)
2018-06-09 23:55:54.836 (DarkSky) Wind (Wind/temperatuur)
2018-06-09 23:55:54.839 (DarkSky) UV (UV Index)
2018-06-09 23:55:54.843 (DarkSky) Rain (Regen)
2018-06-09 23:55:54.846 (DarkSky) General/Visibility (Zicht)
2018-06-09 23:55:54.849 (DarkSky) General/Custom Sensor (Ozone Sensor)
2018-06-09 23:55:54.879 dzVents: Debug: Dumping domoticz data to /usr/local/domoticz/var/scripts/dzVents/domoticzData.lua
2018-06-09 23:55:54.979 dzVents: Debug: Processing device-adapter for THB: Temperature+humidity+barometer device adapter
2018-06-09 23:55:54.979 dzVents: Debug: dzVents version: 2.4.6
2018-06-09 23:55:54.979 dzVents: Debug: Event triggers:
2018-06-09 23:55:54.979 dzVents: Debug: - Device: THB
2018-06-09 23:55:55.210 dzVents: Debug: Dumping domoticz data to /usr/local/domoticz/var/scripts/dzVents/domoticzData.lua
2018-06-09 23:55:55.307 dzVents: Debug: Processing device-adapter for Wind/temperatuur: Wind device adapter
2018-06-09 23:55:55.308 dzVents: Debug: Processing device-adapter for UV Index: UV device adapter
2018-06-09 23:55:55.309 dzVents: Debug: Processing device-adapter for Regen: Rain device
2018-06-09 23:55:55.310 dzVents: Debug: Processing device-adapter for Zicht: Visibility device
2018-06-09 23:55:55.312 dzVents: Debug: Processing device-adapter for Ozone Sensor: Custom sensor device adapter
2018-06-09 23:55:55.312 dzVents: Debug: dzVents version: 2.4.6
2018-06-09 23:55:55.312 dzVents: Debug: Event triggers:
2018-06-09 23:55:55.312 dzVents: Debug: - Device: Wind/temperatuur
2018-06-09 23:55:55.312 dzVents: Debug: - Device: UV Index
2018-06-09 23:55:55.312 dzVents: Debug: - Device: Regen
2018-06-09 23:55:55.312 dzVents: Debug: - Device: Zicht
2018-06-09 23:55:55.312 dzVents: Debug: - Device: Ozone Sensor
2018-06-09 23:56:00.551 dzVents: Debug: Dumping domoticz data to /usr/local/domoticz/var/scripts/dzVents/domoticzData.lua
2018-06-09 23:56:00.620 dzVents: Debug: Event triggers:
2018-06-09 23:56:00.620 dzVents: Debug: - Timer
2018-06-09 23:57:00.317 dzVents: Debug: Dumping domoticz data to /usr/local/domoticz/var/scripts/dzVents/domoticzData.lua
2018-06-09 23:57:00.390 dzVents: Debug: Event triggers:
2018-06-09 23:57:00.390 dzVents: Debug: - Timer
2018-06-09 23:58:00.118 dzVents: Debug: Dumping domoticz data to /usr/local/domoticz/var/scripts/dzVents/domoticzData.lua
2018-06-09 23:58:00.190 dzVents: Debug: Event triggers:
2018-06-09 23:58:00.190 dzVents: Debug: - Timer
2018-06-09 23:59:00.437 dzVents: Debug: Dumping domoticz data to /usr/local/domoticz/var/scripts/dzVents/domoticzData.lua
2018-06-09 23:59:00.510 dzVents: Debug: Event triggers:
2018-06-09 23:59:00.510 dzVents: Debug: - Timer
2018-06-10 00:00:00.339 dzVents: Debug: Dumping domoticz data to /usr/local/domoticz/var/scripts/dzVents/domoticzData.lua
2018-06-10 00:00:00.410 dzVents: Debug: Event triggers:
2018-06-10 00:00:00.410 dzVents: Debug: - Timer
2018-06-10 00:00:00.511 Sunrise: 05:13:00 SunSet: 21:57:00
2018-06-10 00:00:00.511 Day length: 16:44:00 Sun at south: 13:05:00
2018-06-10 00:00:00.511 Civil twilight start: 04:24:00 Civil twilight end: 22:46:00
2018-06-10 00:00:00.511 Nautical twilight start: 03:05:00 Nautical twilight end: 00:05:00
2018-06-10 00:00:00.511 There is no astronomical twilight in the space of 24 hours
2018-06-10 00:00:00.511 EventSystem: reset all events...
2018-06-10 00:00:00.513 dzVents: Write file: /usr/local/domoticz/var/scripts/dzVents/generated_scripts/RuntimeVijverPomp.lua
Any clues?

Re: Updating incremental counter with TIME value / json

Posted: Monday 11 June 2018 15:36
by FrankVZ
For the supplied script does almost do what is has to do :)

Since I dont understand the script yet (and took a looooot of time, now 2 days busy to find answeres) maybe you can help me out.

I would like to improve the script, being more "realtime" (and not only measuring the time between off and on.
So I thought to insert a timer that only runs if the device is triggered On and adds measured time to the regular counter.

But the I run into several problems:
The timer runs always, even the device is Off (or is it possible to disable the timertrigger somehow?).
I tried e.g.
Spoiler: show
return {
on = { devices = { 87 }, -- name enclosed in quotes or IDX number without quotes
devices = { [87] = {'every minute'}} -- one try, see http://www.domoticz.com/forum/viewtopic.php?t=18574
timer = {'every minute'} -- another try,
But in that case, with the original script I get several "nill" value errors. I suppose the script is not able anymore to determine where the trigger is coming from.
So I get errors with .lastState, lastSwitchState, trigger.state, dz.lastState or lastUpdate values (doesnt even show them in the db.log

But this makes me wondering too:
Where are the extensions like "laststate" and "lastswitchstate" coming from? Like the "lastSwitch time" is not handled like a local variable, otherwise it shouldnt be "nill" value?
I cant find the in the domoticz wiki. I even looked (with an DB sqlite editor) into the database, and could not find anything like it within the database structure, except for the field "LastUpdate" (starts with a capital instead of the used variable - dont know if that makes any difference).

Lighting.log shows within the Domoticz.db a "nValue" too and I noticed (in the database) that nvalue >0 = On ; nValue 0 = Off. Coincidence, or is that always the case? If so: using that variable should solve most issues I encounter (easier to get and use within dzvents I think)

The script so far (and sorry for the noob questions.....);
Spoiler: show
--[[ getUptime.lua for [ dzVents >= 2.4 ]
]]--

return {
on = { devices = { 87 }, -- name enclosed in quotes or IDX number without quotes
-- devices = { [87] = {'every minute'}}
timer = {'every minute'}

},

logging = { level = domoticz.LOG_DEBUG,
marker = "getUptime" },

data = { lastState = { initial = "Off" },
secondsOn = { initial = 0 },
lastSwitchTime = { initial = "0" } },

execute = function(dz,trigger)
local myCounterName = dz.devices("Vijver runtime counter gewoon" )
local MonitoredDevice = dz.devices( 87 )

local function updateCounter()
if myCounterName then
local myNumber = dz.utils.round( tonumber(dz.data.secondsOn) )
if myCounterName.value ~= myNumber then
myCounterName.updateCounter( dz.utils.round(( myNumber / 3600 ), 2 ) )
dz.log("updateCounter ==>> " .. "dz.trigger.state" .. "; myNumber" .. myNumber .. "; tekst secondsOn ===>> " .. dz.data.secondsOn ,dz.LOG_DEBUG)
end

dz.data.lastState = trigger.state
dz.data.lastSwitchTime = dz.time.rawDate .. " " .. dz.time.rawTime

end
end

dz.log("secondsAgo " .. dz.devices( 87 ).lastUpdate.secondsAgo)
dz.log("State " .. dz.devices( 87 ).state)
-- dz.log("lastState " .. dz.devices( 87 ).lastState) -- geeft nill foutmelding aan?

if (trigger.isTimer) then
if trigger.state ~= dz.data.lastState then
if trigger.state == "Off" then
local Time = require('Time')
t1 = Time(dz.time.rawDate .." " .. dz.time.rawTime)
t2 = Time(dz.data.lastSwitchTime)
deltaTime = t1.compare(t2).secs
dz.data.secondsOn = dz.data.secondsOn + deltaTime
updateCounter()
end
end
end

if (trigger.isDevice) then
if (MonitoredDevice.state == "On") and (MonitoredDevice.nvalue > 0) then
local Time = require('Time')
t1 = Time(dz.time.rawDate .." " .. dz.time.rawTime)
t2 = Time(dz.data.lastSwitchTime)
deltaTime = t1.compare(t2).secs
dz.data.secondsOn = dz.data.secondsOn + deltaTime
updateCounter()
dz.log("Devices state On verwerkt")
dz.log(trigger.state,dz.LOG_DEBUG)
dz.log(trigger.lastState,dz.LOG_DEBUG)
end
end

dz.log("lastSwitchTime ==>> " .. dz.data.lastSwitchTime .. "; Date Time ==>> " .. dz.time.rawDate .." " ..
dz.time.rawTime .. "; secondsOn ===>> " .. dz.data.secondsOn,dz.LOG_DEBUG)

end
}

Re: Updating incremental counter with TIME value / json

Posted: Monday 11 June 2018 16:36
by waaren
best to look in this excellent wiki

sent you a PM to clarify my script

Re: Updating incremental counter with TIME value / json

Posted: Thursday 14 June 2018 23:41
by FrankVZ
Hi, again a few days further, with a lot of trial and error :)

The system still crashes at exactly midnight (with I think is a dzVents issue, since the log alway stops there).
Today I moved the domoticz to another server (DS218+) that run the same version (and almost the same database) but never crashed.
Now I have to wait and see what happenes,

Thanks by the way for the effort you did put in my questions Waaren - great work. Most of the details I think I'm going to understand by now so it really did help a lot

But there are still a lot of question marks in between my ears, that are related to the syntax (how exactly has a line to be written, what functions/extensions etc are available for this situation (within your script there are also extensions used that I couldnt find in the wiki, how local is a local variable, etc . There are some hints within the wiki and the wiki might be a handy library for the more experienced user, but for a noob like me.....

Thanks anyway for the link, but I've read the wiki a 100 times by now (and also about lua scripting guides, and other forums, etc etc...).

For now I have the script working fine with logging data to a counter, so for me that part is solved :)

There is still one thing left that bothers me: within the script the run time is logged every 6 minutes. That makes 10 data entries within the database, and they are within the .db too.
But for some reason I only get graphs with an interval time of 1 hour instead of 6 minutes (I tried 10 minutes too). If this graphical resolution could be improved somehow.... that would be great.

Re: Updating incremental counter with TIME value / json

Posted: Thursday 15 August 2019 22:20
by piotrm
I'm a beginner in scripts. I tried to use the script to calculate the uptime of my heater, but without success. I gave IDX switch, I created Text Device "hourCounter", but after running the script I receive a message: " 1: return: Illegal number: { "

Could I ask you to explain how to use the script? Thank you in advance.
waaren wrote: Tuesday 05 June 2018 10:00 Or with a more realtime approach but still not having to execute every minute..

Code: Select all

 --[[ getUptime.lua for [ dzVents >= 2.4 ] 
]]--

return {
    on      =   {   devices      =      { nnn }},      -- name enclosed in quotes or IDX number without quotes              
                
    logging =   {   level     =   domoticz.LOG_DEBUG,
                    marker    =   "getUptime"    },

    data    =   {   lastState       = { initial = "Off"   },
                    secondsOn       = { initial = 0 },
                    lastSwitchTime  = { initial = "0" }},

    execute = function(dz,trigger)
        local myTextDevice = dz.devices("hourCounter")          -- name enclosed in quotes or number without 
                                                                -- ( or remove if you don't want textDevice ) 
        local function updateSensor()
            if myTextDevice then
                local myText = "\nRuntime in hours for device " .. trigger.name .. " ==>>  ".. dz.utils.round( tonumber(dz.data.secondsOn) / 3600 )  
                if myTextDevice.text ~= myText then
                    myTextDevice.updateText( myText )
                end    
            end  
        end
  
        if trigger.state ~= dz.data.lastState then
            if trigger.state == "Off" then 
                local Time = require('Time')    
                t1 = Time(dz.time.rawDate .." " .. dz.time.rawTime)
                t2 = Time(dz.data.lastSwitchTime)
                deltaTime =  t1.compare(t2).secs              
                dz.data.secondsOn       =   dz.data.secondsOn + deltaTime  
                updateSensor()
            end   
            dz.data.lastState       =   trigger.state
            dz.data.lastSwitchTime  =   dz.time.rawDate .. " " .. dz.time.rawTime
        end
        dz.log("State ==>> " .. trigger.state .. "; Date Time ==>> " .. dz.time.rawDate .." " .. 
                       dz.time.rawTime .. "; secondsOn ===>> " .. dz.data.secondsOn ,dz.LOG_DEBUG)
            
    end
}

Re: Updating incremental counter with TIME value / json

Posted: Thursday 15 August 2019 22:54
by waaren
piotrm wrote: Thursday 15 August 2019 22:20 after running the script I receive a message: " 1: return: Illegal number: { "
Did you save the script as a dzVents type in the internal editor ?

When not yet familiar with dzVents please start with reading Get started Before implementing. Special attention please for
"In Domoticz go to Setup > Settings > Other and in the section EventSystem make sure the checkbox 'dzVents disabled' is not checked. Also make sure that in the Security section in the settings you allow 127.0.0.1 to not need a password. dzVents uses that port to send certain commands to Domoticz. Finally make sure you have set your current location in Setup > Settings > System > Location, otherwise there is no way to determine nighttime/daytime state."

Re: Updating incremental counter with TIME value / json

Posted: Friday 16 August 2019 2:12
by piotrm
The problem was the lack of address 127.0.0.1. Now everything works, thanks for your help and information!

I would like to pass the results of the script to counter incremental (to have daily, monthly sums etc). Is that possible? I tried to edit the script, but unfortunately for now it is beyond my capabilities. I would appreciate your help.

Re: Updating incremental counter with TIME value / json

Posted: Friday 16 August 2019 10:28
by waaren
piotrm wrote: Friday 16 August 2019 2:12 I would like to pass the results of the script to counter incremental (to have daily, monthly sums etc). Is that possible?
Yes but can you please explain what you want to send to the counter incremental. Is that the total On time overall or should it reset to zero daily or something else ?

Re: Updating incremental counter with TIME value / json

Posted: Friday 16 August 2019 12:45
by piotrm
waaren wrote: Friday 16 August 2019 10:28
piotrm wrote: Friday 16 August 2019 2:12 I would like to pass the results of the script to counter incremental (to have daily, monthly sums etc). Is that possible?
Yes but can you please explain what you want to send to the counter incremental. Is that the total On time overall or should it reset to zero daily or something else ?
I would like to have statistics On time daily, monthly, annual. It should reset to zero daily.

Re: Updating incremental counter with TIME value / json

Posted: Friday 16 August 2019 19:28
by waaren
piotrm wrote: Friday 16 August 2019 12:45 I would like to have statistics On time daily, monthly, annual. It should reset to zero daily.
OK need some time to test if the counter incremental is best suited for this or that using a 'normal'counter is preferable. Might take a couple of days before I have these results.