Compute minutes virtual switch  [Solved]

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

Moderator: leecollings

Post Reply
javalin
Posts: 71
Joined: Tuesday 30 April 2019 16:06
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Location: Portugal
Contact:

Compute minutes virtual switch

Post by javalin »

Hello, I would like to compute the total number of minutes that a virtual switch has been 'on' during a day. I have made a script that manages to download the json data from the database, I suppose that now it would be necessary to create another function that compares the 'on' and 'off' states to calculate the range time that it has been working and accumulate it in a counter. The result can be given in a text variable, for example. Here is the code I have developed, some help is welcome.

Code: Select all

local activeDevice = 592
local scriptVar = 'Compute total minutes'
return
{
    on =
    {
        timer = 
        {
            'every 1 minutes', -- for DEBUG
        },

        httpResponses =
        {
            scriptVar .. '*',
        }, 
    },

    logging =
    {
        level = domoticz.LOG_DEBUG ,
        marker = scriptVar,
    },

    execute = function(dz, item)
        if not(dz.devices(activeDevice).active) then return end
        local AC_room = dz.devices(590)
        
        local function triggerJSON(id, delay)
        url = dz.settings['Domoticz url'] .. '/json.htm?type=lightlog&idx=' .. id
        dz.openURL({ url = url, callback = scriptVar .. '_' .. id}).afterSec(delay or 0)
        end

         -- main
        if item.isTimer then
        triggerJSON(AC_room.id)
        end
        
        if item.isHTTPResponse then
            if item.isJSON then 
                rt = item.json.result
                --dz.utils.dumpTable(item.json)                           -- dump the string represenation of the complete table to log 
                local time = rt[1]["Date"] 
                dz.log(time,dz.LOG_DEBUG)
            end
        end

    end
}
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Compute minutes virtual switch

Post by waaren »

javalin wrote: Saturday 26 December 2020 20:12 some help is welcome.
some help below

Code: Select all


local activeDevice = 592
local scriptVar = 'Compute total minutes'

return
{
    on =
    {
        timer =
        {
            'every 1 minutes', -- for DEBUG
        },

        httpResponses =
        {
            scriptVar .. '*',
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG ,
        marker = scriptVar,
    },

    data =
    {
        state =
        {
            initial = {},
        },
    },

    execute = function(dz, item)
        if not(dz.devices(activeDevice).active) then return end
        local AC_room = dz.devices(590)

        local function makeTimeStamp(dateString, pattern)
            local pattern = pattern or  "(%d+)%-(%d+)%-(%d+)%s(%d+):(%d+):(%d+)"
            local year, month, day, hour, minute, seconds= dateString:match(pattern)
            local convertedTimestamp = os.time({year = ('20' .. year), month = month, day = day, hour = hour, min = minute, sec = seconds})
            return convertedTimestamp
        end

        local function triggerJSON(id, delay)
            url = dz.settings['Domoticz url'] .. '/json.htm?type=lightlog&idx=' .. id
            dz.openURL({ url = url, callback = scriptVar .. '_' .. id}).afterSec(delay or 0)
        end

        -- main
        if item.isTimer or item.isDevice then
            if dz.data.state[dz.time.rawDate] == nil then
                dz.data.state[dz.time.rawDate] = AC_room.state  -- keep state at first run of the day
            end
            triggerJSON(AC_room.id)
            return
        end

        if item.isJSON then
            rt = item.json.result
            for _, record in ipairs(rt) do
                 dz.log('action ' .. record.Status .. ', at ' .. record.Date .. ' =>> timeStamp: ' .. makeTimeStamp(record.Date),dz.LOG_DEBUG)
            end
        else
            dz.log('Problem retrieving data..',dz.LOG_ERROR)
            dz.log(item,dz.LOG_DEBUG)
        end
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
javalin
Posts: 71
Joined: Tuesday 30 April 2019 16:06
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Location: Portugal
Contact:

Re: Compute minutes virtual switch

Post by javalin »


some help below
Thank you waaren, I get this error on Log Debug: (time result cannot be represented in this installation)

2020-12-27 16:57:01.444 Error: dzVents: Error: (3.0.19) Compute total minutes: An error occurred when calling event handler compute_minutes_waaren
2020-12-27 16:57:01.444 Error: dzVents: Error: (3.0.19) Compute total minutes: ...ipts/dzVents/generated_scripts/compute_minutes_waaren.lua:41: time result cannot be represented in this installation


Line 41 is:

Code: Select all

            local convertedTimestamp = os.time({year = ('20' .. year), month = month, day = day, hour = hour, min = minute, sec = seconds})
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Compute minutes virtual switch

Post by waaren »

javalin wrote: Sunday 27 December 2020 18:01 I get this error on Log Debug: (time result cannot be represented in this installation)
Can you try this one? It' s not a complete solution but should point you in the right direction.

Code: Select all

local activeDevice = 592
local scriptVar = 'Compute total minutes'

return
{
    on =
    {
        timer =
        {
            'every 1 minutes', -- for DEBUG
        },

        httpResponses =
        {
            scriptVar .. '*',
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG ,
        marker = scriptVar,
    },

    data =
    {
        state =
        {
            initial = {},
        },
    },

    execute = function(dz, item)
        if not(dz.devices(activeDevice).active) then return end
        local AC_room = dz.devices(590)

        local function makeTimeStamp(dateString, pattern)
            local pattern = pattern or  "(%d+)%-(%d+)%-(%d+)%s(%d+):(%d+):(%d+)"
            local year, month, day, hour, minute, seconds= dateString:match(pattern)
            return os.time({year = year, month = month, day = day, hour = hour, min = minute, sec = seconds})
        end

        local function triggerJSON(id, delay)
            url = dz.settings['Domoticz url'] .. '/json.htm?type=lightlog&idx=' .. id
            dz.openURL({ url = url, callback = scriptVar .. '_' .. id}).afterSec(delay or 0)
        end

        -- main
        if item.isTimer or item.isDevice then
            if dz.data.state[dz.time.rawDate] == nil then
                dz.data.state[dz.time.rawDate] = AC_room.state  -- keep state at first run of the day
            end
            triggerJSON(AC_room.id)
            return
        end

        if item.isJSON then
            rt = item.json.result
            for _, record in ipairs(rt) do
                 dz.log('action ' .. record.Status .. ', at ' .. record.Date .. ' =>> timeStamp: ' .. makeTimeStamp(record.Date),dz.LOG_DEBUG)
            end
        else
            dz.log('Problem retrieving data..',dz.LOG_ERROR)
            dz.log(item,dz.LOG_DEBUG)
        end
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
javalin
Posts: 71
Joined: Tuesday 30 April 2019 16:06
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Location: Portugal
Contact:

Re: Compute minutes virtual switch

Post by javalin »

Can you try this one? It' s not a complete solution but should point you in the right direction.
Thank you very much waaren, you give the timeStamp for each state log, that was the tricky point.
I understand the behavior of the script, in spite of everything, I will have to look more closely.
So, I have decided to continue the script with the code I show bellow and it seems to work. The most complicated thing has been to manage the various Log scenarios:
- Case 1: we have a log with successives 'On' and 'Off'. (easy)
- Case 2: we have a log with two consecutive 'Off' states.
- Case 3: we have a log with two consecutive 'On' states.
- Case 4: we have a log with an 'Off' status at the beginning.
- Case 5: we run the script with the switch in the 'On' state.

I think I have managed to solve the situations 1,2,3,4 and I still have the 5 under study. But I think I will need the current time to finish it as I think.

Code: Select all

local activeDevice = 592
local scriptVar = 'Compute total minutes'

return
{
    on =
    {
        timer =
        {
            'every 1 minutes', -- for DEBUG
        },

        httpResponses =
        {
            scriptVar .. '*',
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG ,
        marker = scriptVar,
    },

    data =    --persistent data?
    {
        state =
        {
            initial = {},
        },
    },

    execute = function(dz, item)
        if not(dz.devices(activeDevice).active) then return end
        local AC_room = dz.devices(13)
        local counter = 0
        local lastTimeStamp = 0
        local function makeTimeStamp(dateString, pattern)
            local pattern = pattern or  "(%d+)%-(%d+)%-(%d+)%s(%d+):(%d+):(%d+)"  -- must be this format!! "2016-12-12 07:35:00"
            local year, month, day, hour, minute, seconds= dateString:match(pattern)
            return os.time({year = year, month = month, day = day, hour = hour, min = minute, sec = seconds})
        end

        local function triggerJSON(id, delay)
            url = dz.settings['Domoticz url'] .. '/json.htm?type=lightlog&idx=' .. id
            dz.openURL({ url = url, callback = scriptVar .. '_' .. id}).afterSec(delay or 0)
        end

        -- main
        if item.isTimer or item.isDevice then
            if dz.data.state[dz.time.rawDate] == nil then   
                dz.data.state[dz.time.rawDate] = AC_room.state  -- keep state at first run of the day, will be something like: 2016-12-12T20:00:00Z
            end
            triggerJSON(AC_room.id)
            return
        end

        if item.isJSON then
            rt = item.json.result
            local leng = 0
            for k, v in pairs(rt) do
            	leng = leng+1
            end
            dz.log('Lenght ' ..leng,dz.LOG_DEBUG)
            
            for _, record in ipairs(rt) do
                 dz.log('action ' .. record.Status .. ', at ' .. record.Date .. ' =>> timeStamp: ' .. makeTimeStamp(record.Date),dz.LOG_DEBUG)
                 if _>=2 and item.json.result[_].Data == item.json.result[_-1].Data and record.Status == "Off" then --Ok!, avoid bug for 2 successives 'Off' in Log  
                 counter = counter - lastTimeStamp + makeTimeStamp(record.Date)
                 elseif _>=2 and item.json.result[_].Data == item.json.result[_-1].Data and record.Status == "On" then --Don´t have tested yet, avoid bug for 2 successives 'On' in Log
                 counter = counter
                 elseif _ == leng and record.Status == "Off" then --Ok!, avoid bug if you have an 'Off' as first state in Log (older state, not last state) 
                 counter = counter
                 elseif _ == leng and record.Status == "On" then --Dont work, may be it needed to call to actual timeStamp. Need to resolve bug if srcipt runs at same time switch has 'On' state
                 counter = counter
                 elseif record.Status == "Off" then
                 counter = counter + makeTimeStamp(record.Date)
                 elseif record.Status == "On" then
                 counter = counter - makeTimeStamp(record.Date)
                 end
                 lastTimeStamp = makeTimeStamp(record.Date)
            end
            
            minutes= counter/60  
            seconds= counter%60
            if seconds < 10 then
            dz.log('Number of minutes ' ..math.floor(minutes)..':0'..seconds..'s',dz.LOG_DEBUG)   
            else
            dz.log('Number of minutes ' ..math.floor(minutes)..':'..seconds..'s',dz.LOG_DEBUG)
            end
            dz.log('Counter ' .. dz.utils.round(counter,2),dz.LOG_DEBUG)

        else
            dz.log('Problem retrieving data..',dz.LOG_ERROR)
            dz.log(item,dz.LOG_DEBUG)
        end
    end
}
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Compute minutes virtual switch

Post by waaren »

javalin wrote: Monday 28 December 2020 21:54 I think I have managed to solve the situations 1,2,3,4 and I still have the 5 under study. But I think I will need the current time to finish it as I think.
Good progress !

If you need the amount of records in an array type Lua table you can use #tablename
You normally only use the underscore as a placeholder for a varname if you don't intend to use it in your code
Current time in dzVents scripts is dz.time (or dz.time.dDate or os.time() if you only use current time as timestamp)

Code: Select all

local activeDevice = 592
local scriptVar = 'Compute total minutes'

return
{
    on =
    {
        timer =
        {
            'every 1 minutes', -- for DEBUG
        },

        httpResponses =
        {
            scriptVar .. '*',
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG ,
        marker = scriptVar,
    },

    data =    --persistent data?
    {
        state =
        {
            initial = {},
        },
    },

    execute = function(dz, item)
        if not(dz.devices(activeDevice).active) then return end
        local AC_room = dz.devices(13)
        local counter = 0
        local lastTimeStamp = 0
        local function makeTimeStamp(dateString, pattern)
            local pattern = pattern or  "(%d+)%-(%d+)%-(%d+)%s(%d+):(%d+):(%d+)"  -- must be this format!! "2016-12-12 07:35:00"
            local year, month, day, hour, minute, seconds= dateString:match(pattern)
            return os.time({year = year, month = month, day = day, hour = hour, min = minute, sec = seconds})
        end

        local function triggerJSON(id, delay)
            url = dz.settings['Domoticz url'] .. '/json.htm?type=lightlog&idx=' .. id
            dz.openURL({ url = url, callback = scriptVar .. '_' .. id}).afterSec(delay or 0)
        end

        -- main
        if item.isTimer or item.isDevice then
            if dz.data.state[dz.time.rawDate] == nil then   
                dz.data.state[dz.time.rawDate] = AC_room.state  -- keep state at first run of the day, will be something like: 2016-12-12T20:00:00Z
            end
            triggerJSON(AC_room.id)
            return
        end

        if item.isJSON then
            rt = item.json.result

            dz.log('Lenght ' ..#rt,dz.LOG_DEBUG)
            
            for index, record in ipairs(rt) do
                 dz.log('action ' .. record.Status .. ', at ' .. record.Date .. ' =>> timeStamp: ' .. makeTimeStamp(record.Date),dz.LOG_DEBUG)
                 if index >= 2 and record.Data == rt[index - 1].Data and record.Status == "Off" then -- Ok!, avoid bug for 2 successives 'Off' in Log  
					counter = counter - lastTimeStamp + makeTimeStamp(record.Date)
                 elseif index >=2 and record.Data == rt[index - 1].Data and record.Status == "On"  then --Don´t have tested yet, avoid bug for 2 successives 'On' in Log
					counter = counter
                 elseif index == #rt and record.Status == "Off" then --Ok!, avoid bug if you have an 'Off' as first state in Log (older state, not last state) 
					counter = counter
                 elseif index == #rt and record.Status == "On" then --Dont work, may be it needed to call to actual timeStamp. Need to resolve bug if script runs at same time switch has 'On' state
					counter = counter
                 elseif record.Status == "Off" then
					counter = counter + makeTimeStamp(record.Date)
                 elseif record.Status == "On" then
					counter = counter - makeTimeStamp(record.Date)
                 end
                 lastTimeStamp = makeTimeStamp(record.Date)
            end
            
            minutes = counter / 60  
            seconds = counter % 60
            if seconds < 10 then
				dz.log('Number of minutes ' ..math.floor(minutes) .. ':0' .. seconds .. 's',dz.LOG_DEBUG)   
            else
				dz.log('Number of minutes ' .. math.floor(minutes) .. ':' .. seconds .. 's',dz.LOG_DEBUG)
            end

			dz.log('Counter ' .. dz.utils.round(counter,2),dz.LOG_DEBUG)

        else
            dz.log('Problem retrieving data..',dz.LOG_ERROR)
            dz.log(item,dz.LOG_DEBUG)
        end
    end
}

Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
javalin
Posts: 71
Joined: Tuesday 30 April 2019 16:06
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Location: Portugal
Contact:

Re: Compute minutes virtual switch

Post by javalin »

If you need the amount of records in an array type Lua table you can use #tablename
You normally only use the underscore as a placeholder for a varname if you don't intend to use it in your code
Current time in dzVents scripts is dz.time (or dz.time.dDate or os.time() if you only use current time as timestamp)
Thanks for the tips waaren. It is easier for me to understand the script with this format.
I have already solved 'Case 5' using the Current time, so the script can work with any type of Log and I have added an output for a text variable. I'll probably try to create a counter to store the result for each day. I think it will also be possible.

Why the script needs to store in a persistent variables called 'state' the first state of each day? I can't guess why

Code: Select all

 dz.data.state[dz.time.rawDate] = switch.state
This is the new code:

Code: Select all

local scriptVar = 'Compute total minutes'

return
{
    on =
    {
        timer =
        {
            'every 15 minutes', -- for DEBUG
        },

        httpResponses =
        {
            scriptVar .. '*',
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG ,
        marker = scriptVar,
    },

    data =    --persistent data?
    {
        state =
        {
            initial = {},
        },
    },

    execute = function(dz, item)
        local switch = dz.devices(589)
        local text = dz.devices(637) 
        local counter = 0
        local lastTimeStamp = 0
        local function makeTimeStamp(dateString, pattern)
            local pattern = pattern or  "(%d+)%-(%d+)%-(%d+)%s(%d+):(%d+):(%d+)"  -- must be this format!! "2016-12-12 07:35:00"
            local year, month, day, hour, minute, seconds= dateString:match(pattern)
            return os.time({year = year, month = month, day = day, hour = hour, min = minute, sec = seconds})
        end

        local function triggerJSON(id, delay)
            url = dz.settings['Domoticz url'] .. '/json.htm?type=lightlog&idx=' .. id
            dz.openURL({ url = url, callback = scriptVar .. '_' .. id}).afterSec(delay or 0)
        end

        -- main
        if item.isTimer or item.isDevice then
            if dz.data.state[dz.time.rawDate] == nil then   --["rawDate"]="2020-12-28" and ["rawDateTime"]="2020-12-28 22:56:01"
                dz.data.state[dz.time.rawDate] = switch.state  -- keep state at first run of the day, will be something like: 2016-12-12T20:00:00Z
            end
            triggerJSON(switch.id)
            return
        end

        if item.isJSON then
            rt = item.json.result

            dz.log('Lenght ' ..#rt,dz.LOG_DEBUG)
            dz.log(dz.time.dDate,dz.LOG_DEBUG)
            for index, record in ipairs(rt) do
                 dz.log('action ' .. record.Status .. ', at ' .. record.Date .. ' =>> timeStamp: ' .. makeTimeStamp(record.Date),dz.LOG_DEBUG)
                 if index == 1 and record.Status == "On" then --Ok!, avoid bug if script runs at same time switch has 'On' state
					counter = dz.time.dDate - makeTimeStamp(record.Date)
                 elseif index >= 2 and record.Data == rt[index - 1].Data and record.Status == "Off" then -- Ok!, avoid bug for 2 successives 'Off' in Log  
					counter = counter - lastTimeStamp + makeTimeStamp(record.Date)
                 elseif index >=2 and record.Data == rt[index - 1].Data and record.Status == "On"  then --Don´t have tested yet, avoid bug for 2 successives 'On' in Log
					counter = counter + lastTimeStamp - makeTimeStamp(record.Date)
                 elseif index == #rt and record.Status == "Off" then --Ok!, avoid bug if you have an 'Off' as first state in Log (older state, not last state) 
					counter = counter
                 elseif record.Status == "Off" then
					counter = counter + makeTimeStamp(record.Date)
                 elseif record.Status == "On" then
					counter = counter - makeTimeStamp(record.Date)
                 end
                 lastTimeStamp = makeTimeStamp(record.Date)
            end
            hours = math.floor(counter/60/60)
            minutes = math.floor(counter/60%60) 
            seconds = counter % 60
            if minutes < 10 then
                if seconds < 10 then
			    	dz.log('Today: ' ..hours ..':0'..minutes .. ':0' .. seconds .. 's',dz.LOG_DEBUG)   
                    text.updateText('Today: ' ..hours..':0'..minutes .. ':0' .. seconds .. 's')
                else
			    	dz.log('Today: ' ..hours ..':0'..minutes .. ':' .. seconds .. 's',dz.LOG_DEBUG)
                    text.updateText('Today: ' ..hours ..':0'..minutes .. ':' .. seconds .. 's') 
                end
            else
                if seconds < 10 then  
     			    dz.log('Today: ' ..hours ..':'..minutes .. ':0' .. seconds .. 's',dz.LOG_DEBUG)   
                    text.updateText('Today: ' ..hours..':'..minutes .. ':0' .. seconds .. 's')
                else
			    	dz.log('Today: ' ..hours ..':'..minutes .. ':' .. seconds .. 's',dz.LOG_DEBUG)
                    text.updateText('Today: ' ..hours ..':'..minutes .. ':' .. seconds .. 's')  
                end    
            end
			dz.log('Counter ' .. dz.utils.round(counter,2),dz.LOG_DEBUG)

        else
            dz.log('Problem retrieving data..',dz.LOG_ERROR)
            dz.log(item,dz.LOG_DEBUG)
        end
    end
}

Last edited by javalin on Tuesday 29 December 2020 23:03, edited 3 times in total.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Compute minutes virtual switch

Post by waaren »

javalin wrote: Tuesday 29 December 2020 22:10 Why the script needs to store in a persistent variables called 'state' the first state of each day? I can't guess why

Code: Select all

 dz.data.state[dz.time.rawDate] = switch.state
I added this because it was not clear to me if you were looking for the "On" time of today or for a longer period.
The records in LightingLog are not kept forever and in theory this means that you will not find a time in LigtingLog for a last state change. In such a case you might be helped by the state at the beginning of a day.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
javalin
Posts: 71
Joined: Tuesday 30 April 2019 16:06
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Location: Portugal
Contact:

Re: Compute minutes virtual switch

Post by javalin »

I added this because it was not clear to me if you were looking for the "On" time of today or for a longer period.
I look for the "On" time of today.

Curiously now I have realized that I have the Log with statuses from yesterday:

Code: Select all

 2020-12-29 22:30:01.461 Status: dzVents: Info: Compute total minutes: ------ Start internal script: compute_minutes_clean: HTTPResponse: "Compute total minutes_589"
2020-12-29 22:30:01.470 Status: dzVents: Debug: Compute total minutes: Processing device-adapter for AC Maria: Switch device adapter
2020-12-29 22:30:01.471 Status: dzVents: Debug: Compute total minutes: Processing device-adapter for Counter time relay: Text device
2020-12-29 22:30:01.471 Status: dzVents: Debug: Compute total minutes: Lenght 31
2020-12-29 22:30:01.471 Status: dzVents: Debug: Compute total minutes: 1609281001
2020-12-29 22:30:01.471 Status: dzVents: Debug: Compute total minutes: action Off, at 2020-12-29 12:03:16 =>> timeStamp: 1609243396
2020-12-29 22:30:01.472 Status: dzVents: Debug: Compute total minutes: action On, at 2020-12-29 12:03:07 =>> timeStamp: 1609243387
2020-12-29 22:30:01.472 Status: dzVents: Debug: Compute total minutes: action Off, at 2020-12-29 12:01:28 =>> timeStamp: 1609243288
2020-12-29 22:30:01.472 Status: dzVents: Debug: Compute total minutes: action On, at 2020-12-29 12:01:23 =>> timeStamp: 1609243283
2020-12-29 22:30:01.472 Status: dzVents: Debug: Compute total minutes: action Off, at 2020-12-29 12:00:17 =>> timeStamp: 1609243217
2020-12-29 22:30:01.472 Status: dzVents: Debug: Compute total minutes: action On, at 2020-12-29 12:00:01 =>> timeStamp: 1609243201
2020-12-29 22:30:01.472 Status: dzVents: Debug: Compute total minutes: action Off, at 2020-12-29 11:59:59 =>> timeStamp: 1609243199
2020-12-29 22:30:01.472 Status: dzVents: Debug: Compute total minutes: action On, at 2020-12-29 11:59:50 =>> timeStamp: 1609243190
2020-12-29 22:30:01.472 Status: dzVents: Debug: Compute total minutes: action Off, at 2020-12-29 10:56:56 =>> timeStamp: 1609239416
2020-12-29 22:30:01.473 Status: dzVents: Debug: Compute total minutes: action On, at 2020-12-29 10:56:38 =>> timeStamp: 1609239398
2020-12-29 22:30:01.473 Status: dzVents: Debug: Compute total minutes: action Off, at 2020-12-29 07:56:29 =>> timeStamp: 1609228589
2020-12-29 22:30:01.473 Status: dzVents: Debug: Compute total minutes: action On, at 2020-12-29 07:00:18 =>> timeStamp: 1609225218
2020-12-29 22:30:01.473 Status: dzVents: Debug: Compute total minutes: action Off, at 2020-12-29 06:55:18 =>> timeStamp: 1609224918
2020-12-29 22:30:01.473 Status: dzVents: Debug: Compute total minutes: action On, at 2020-12-29 04:23:26 =>> timeStamp: 1609215806
2020-12-29 22:30:01.473 Status: dzVents: Debug: Compute total minutes: action Off, at 2020-12-29 03:53:26 =>> timeStamp: 1609214006
2020-12-29 22:30:01.473 Status: dzVents: Debug: Compute total minutes: action On, at 2020-12-28 23:00:12 =>> timeStamp: 1609196412
2020-12-29 22:30:01.473 Status: dzVents: Debug: Compute total minutes: action Off, at 2020-12-28 22:30:12 =>> timeStamp: 1609194612
2020-12-29 22:30:01.473 Status: dzVents: Debug: Compute total minutes: action On, at 2020-12-28 21:47:51 =>> timeStamp: 1609192071
2020-12-29 22:30:01.474 Status: dzVents: Debug: Compute total minutes: action Off, at 2020-12-28 21:37:05 =>> timeStamp: 1609191425
2020-12-29 22:30:01.474 Status: dzVents: Debug: Compute total minutes: action On, at 2020-12-28 21:17:36 =>> timeStamp: 1609190256
2020-12-29 22:30:01.474 Status: dzVents: Debug: Compute total minutes: action Off, at 2020-12-28 21:17:11 =>> timeStamp: 1609190231
2020-12-29 22:30:01.474 Status: dzVents: Debug: Compute total minutes: action On, at 2020-12-28 21:06:01 =>> timeStamp: 1609189561
2020-12-29 22:30:01.474 Status: dzVents: Debug: Compute total minutes: action Off, at 2020-12-28 21:00:20 =>> timeStamp: 1609189220
2020-12-29 22:30:01.474 Status: dzVents: Debug: Compute total minutes: action On, at 2020-12-28 20:33:01 =>> timeStamp: 1609187581
2020-12-29 22:30:01.474 Status: dzVents: Debug: Compute total minutes: action Off, at 2020-12-28 20:30:10 =>> timeStamp: 1609187410
2020-12-29 22:30:01.474 Status: dzVents: Debug: Compute total minutes: action On, at 2020-12-28 20:30:02 =>> timeStamp: 1609187402
2020-12-29 22:30:01.474 Status: dzVents: Debug: Compute total minutes: action Off, at 2020-12-28 14:06:01 =>> timeStamp: 1609164361
2020-12-29 22:30:01.475 Status: dzVents: Debug: Compute total minutes: action On, at 2020-12-28 13:45:01 =>> timeStamp: 1609163101
2020-12-29 22:30:01.475 Status: dzVents: Debug: Compute total minutes: action Off, at 2020-12-28 07:46:50 =>> timeStamp: 1609141610
2020-12-29 22:30:01.475 Status: dzVents: Debug: Compute total minutes: action On, at 2020-12-28 07:42:09 =>> timeStamp: 1609141329
2020-12-29 22:30:01.475 Status: dzVents: Debug: Compute total minutes: action Off, at 2020-12-28 06:41:43 =>> timeStamp: 1609137703
2020-12-29 22:30:01.475 Status: dzVents: Debug: Compute total minutes: Today: 10:28:22s 
Normaly my switches will never have yesterday's Log because every midnight I run a script to cleanup database that deletes everything older than a day in LightingLog. But for some reason yestarday didn´t run and it must be the reason for this long Log.

EDIT:
I have run now manually the srcipt to cleanup the database and now the LOG_DEBUG is correct:

Code: Select all

 2020-12-29 22:53:01.238 Status: dzVents: Info: Compute total minutes: ------ Start internal script: compute_minutes_clean: HTTPResponse: "Compute total minutes_589"
2020-12-29 22:53:01.258 Status: dzVents: Debug: Compute total minutes: Processing device-adapter for AC Maria: Switch device adapter
2020-12-29 22:53:01.259 Status: dzVents: Debug: Compute total minutes: Processing device-adapter for Counter time relay: Text device
2020-12-29 22:53:01.259 Status: dzVents: Debug: Compute total minutes: Lenght 15
2020-12-29 22:53:01.259 Status: dzVents: Debug: Compute total minutes: 1609282381
2020-12-29 22:53:01.259 Status: dzVents: Debug: Compute total minutes: action Off, at 2020-12-29 12:03:16 =>> timeStamp: 1609243396
2020-12-29 22:53:01.259 Status: dzVents: Debug: Compute total minutes: action On, at 2020-12-29 12:03:07 =>> timeStamp: 1609243387
2020-12-29 22:53:01.259 Status: dzVents: Debug: Compute total minutes: action Off, at 2020-12-29 12:01:28 =>> timeStamp: 1609243288
2020-12-29 22:53:01.260 Status: dzVents: Debug: Compute total minutes: action On, at 2020-12-29 12:01:23 =>> timeStamp: 1609243283
2020-12-29 22:53:01.260 Status: dzVents: Debug: Compute total minutes: action Off, at 2020-12-29 12:00:17 =>> timeStamp: 1609243217
2020-12-29 22:53:01.260 Status: dzVents: Debug: Compute total minutes: action On, at 2020-12-29 12:00:01 =>> timeStamp: 1609243201
2020-12-29 22:53:01.260 Status: dzVents: Debug: Compute total minutes: action Off, at 2020-12-29 11:59:59 =>> timeStamp: 1609243199
2020-12-29 22:53:01.260 Status: dzVents: Debug: Compute total minutes: action On, at 2020-12-29 11:59:50 =>> timeStamp: 1609243190
2020-12-29 22:53:01.260 Status: dzVents: Debug: Compute total minutes: action Off, at 2020-12-29 10:56:56 =>> timeStamp: 1609239416
2020-12-29 22:53:01.260 Status: dzVents: Debug: Compute total minutes: action On, at 2020-12-29 10:56:38 =>> timeStamp: 1609239398
2020-12-29 22:53:01.260 Status: dzVents: Debug: Compute total minutes: action Off, at 2020-12-29 07:56:29 =>> timeStamp: 1609228589
2020-12-29 22:53:01.260 Status: dzVents: Debug: Compute total minutes: action On, at 2020-12-29 07:00:18 =>> timeStamp: 1609225218
2020-12-29 22:53:01.261 Status: dzVents: Debug: Compute total minutes: action Off, at 2020-12-29 06:55:18 =>> timeStamp: 1609224918
2020-12-29 22:53:01.261 Status: dzVents: Debug: Compute total minutes: action On, at 2020-12-29 04:23:26 =>> timeStamp: 1609215806
2020-12-29 22:53:01.261 Status: dzVents: Debug: Compute total minutes: action Off, at 2020-12-29 03:53:26 =>> timeStamp: 1609214006
2020-12-29 22:53:01.261 Status: dzVents: Debug: Compute total minutes: Today: 3:29:00s
2020-12-29 22:53:01.261 Status: dzVents: Debug: Compute total minutes: Counter 12540.0
2020-12-29 22:53:01.262 Status: dzVents: Info: Compute total minutes: ------ Finished compute_minutes_clean
javalin
Posts: 71
Joined: Tuesday 30 April 2019 16:06
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Location: Portugal
Contact:

Re: Compute minutes virtual switch  [Solved]

Post by javalin »

Ok, solved :)
To use only daily data (today) from LightingLog I have wrote this line:

Code: Select all

  if record.Date:sub(1,10) == dz.time.rawDate then
So bucle 'for' will be something like this:

Code: Select all

        if item.isJSON then
            rt = item.json.result
            for index, record in ipairs(rt) do
                if record.Date:sub(1,10) == dz.time.rawDate then
                   dz.log('action ' .. record.Status .. ', at ' .. record.Date .. ' =>> timeStamp: ' .. makeTimeStamp(record.Date),dz.LOG_DEBUG)
                   if index == 1 and record.Status == "On" then --Ok!, avoid bug if script runs at same time switch has 'On' state
				      counter = dz.time.dDate - makeTimeStamp(record.Date)
                   elseif index >= 2 and record.Data == rt[index - 1].Data and record.Status == "Off" then -- Ok!, avoid bug for 2 successives 'Off' in Log  
					  counter = counter - lastTimeStamp + makeTimeStamp(record.Date)
                   elseif index >=2 and record.Data == rt[index - 1].Data and record.Status == "On"  then --Don´t have tested yet, avoid bug for 2 successives 'On' in Log
					  counter = counter + lastTimeStamp - makeTimeStamp(record.Date)
                   elseif index == #rt and record.Status == "Off" then --Ok!, avoid bug if you have an 'Off' as first state in Log (older state, not last state) 
					  counter = counter
                   elseif record.Status == "Off" then
					  counter = counter + makeTimeStamp(record.Date)
                   elseif record.Status == "On" then
					  counter = counter - makeTimeStamp(record.Date)
                   end
                lastTimeStamp = makeTimeStamp(record.Date)
                end
            end
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 1 guest