Managed counter not updating

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

Moderator: leecollings

Post Reply
Kranendijk
Posts: 60
Joined: Wednesday 29 May 2019 14:39
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Location: Zwolle
Contact:

Managed counter not updating

Post by Kranendijk »

Starting to lose it while I was thinking I got it :D
I have a script pulling the monthly readings from my P1 meter and updating a managed counter to reflect the kWh delivery of my solar panels. I thought it worked, but it seems to fetch only 0 amounts while there are tonnes (well few kWh's) generated. Where am I thinking wrong? r1 and r2 should be the values to fetch?

Script
Spoiler: show

Code: Select all

-- Hier de managed counter vullen, die komt niet op het dashboard
-- Per week/maand/jaar

local httpResponses = "monthTotal"

return {
    on      =   {   
                    timer           =   { "every 1 hours" },
                    httpResponses   =   { httpResponses .. "*" } 
                },

    logging =   {   
                    level           =   domoticz.LOG_DEBUG, -- set to LOG_ERROR when script works as expected
                    marker          =   httpResponse   
                },
                
    execute = function(dz, item)
        -- ****************************** Your settings below this line ***************************************************
        usageDevice = dz.devices(1)          -- Replace xxxx with ID of energyDevice you want to track
        monthTotal = dz.devices(534)        -- Create as virtual managed counter (energy) and change yyyy to the ID of the new device
        -- ****************************** No changes required below this line *********************************************
        
        local function logWrite(str,level)
            dz.log(tostring(str),level or dz.LOG_DEBUG)
        end
        
        local function triggerJSON(id, period, delay)
            local delay = delay or 0
            local  URLString   =    dz.settings['Domoticz url'] .. "/json.htm?type=graph&sensor=counter&range=" .. 
                                    period .. "&idx=" .. id 
            dz.openURL({    url = URLString,
                            method = "GET",
                            callback = httpResponses .. "_" .. period}).afterSec(delay)                      
        end
        
        local function calculateMonthTotal(rt)
            local monthTotal = 0
            local currentMonth = dz.time.rawDate:sub(1,7)
            for id, result in  ipairs(rt) do 
                if rt[id].d:sub(1,7) == currentMonth then
                    logWrite(rt[id].d .. " ==>> " .. rt[id].r1)
                    monthTotal = monthTotal + rt[id].r1
                end
            end
            for id, result in  ipairs(rt) do 
                if rt[id].d:sub(1,7) == currentMonth then
                    logWrite(rt[id].d .. " ==>> " .. rt[id].r2)
                    monthTotal = monthTotal + rt[id].r2
                end
            end
            return monthTotal * 1000
        end    
        
        if not item.isHTTPResponse then
            triggerJSON(usageDevice.id, "month")
        elseif item.ok then                                      -- statusCode == 2xx
            monthTotal.update(0,calculateMonthTotal(item.json.result))
        else
            logWrite("Could not get (good) data from domoticz. Error (" .. (item.statusCode or 999) .. ")"  ,dz.LOG_ERROR)
            logWrite(item.data)
        end
    end
}
And the log:
Spoiler: show

Code: Select all

2021-02-02 22:32:00.558 Status: EventSystem: Script event triggered: /home/pi/domoticz/dzVents/runtime/dzVents.lua
2021-02-02 22:32:00.670 Status: dzVents: Info: Handling httpResponse-events for: "monthTotal_month"
2021-02-02 22:32:00.703 Status: dzVents: Info: ------ Start internal script: 034StroomPanelenMaand: HTTPResponse: "monthTotal_month"
2021-02-02 22:32:00.719 Status: dzVents: Debug: Processing device-adapter for StroomPanelenMaand: Counter device adapter
2021-02-02 22:32:00.720 Status: dzVents: Debug: 2021-02-01 ==>> 0.000
2021-02-02 22:32:00.720 Status: dzVents: Debug: 2021-02-02 ==>> 0.000
2021-02-02 22:32:00.720 Status: dzVents: Debug: 2021-02-01 ==>> 0.000
2021-02-02 22:32:00.720 Status: dzVents: Debug: 2021-02-02 ==>> 0.000
2021-02-02 22:32:00.720 Status: dzVents: Info: ------ Finished 034StroomPanelenMaand
Weird, isn't it?
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Managed counter not updating

Post by waaren »

Kranendijk wrote: Tuesday 02 February 2021 22:37 Starting to lose it while I was thinking I got it :D
I have a script pulling the monthly readings from my P1 meter and updating a managed counter to reflect the kWh delivery of my solar panels. I thought it worked, but it seems to fetch only 0 amounts while there are tonnes (well few kWh's) generated. Where am I thinking wrong? r1 and r2 should be the values to fetch?

Weird, isn't it?
Could well be that these are the real values. The r1 and r2 of the p1 device are the surplus of what you produced - what you used. Only if that's positive (so produced more then what you used these will have a value > 0.
In January and February r1+r2 will be 0 most of the time during workdays.

I added some extra loglines in the below function so you will also see the ignored values (because not in the current month)
Replace your function with this one. No changes required in the rest of your script.

Code: Select all

        local function calculateMonthTotal(rt)
            local monthTotal = 0
            local currentMonth = dz.time.rawDate:sub(1,7)
            
            for _, result in  ipairs(rt) do
                
                if result.d:sub(1,7) == currentMonth then
                    dz.log('Counting _________________ ' .. result.d .. " r1 ==>> " .. result.r1 ..", r2 ==>> " .. result.r2 , dz.LOG_DEBUG)
                    monthTotal = monthTotal + result.r1 +  result.r2 
                else
                    dz.log('Ignoring ' .. result.d .. " r1 ==>> " .. result.r1 ..", r2 ==>> " .. result.r2 , dz.LOG_DEBUG)
                    
                end
            end
            
            return monthTotal * 1000
        end 

My results using this function with a household of two and 20 solarpanels, west oriented:
Spoiler: show

Code: Select all


2021-02-03 00:41:58.991  Status: dzVents: Debug: Ignoring 2021-01-03 r1 ==>> 0.000, r2 ==>> 0.000
2021-02-03 00:41:58.991  Status: dzVents: Debug: Ignoring 2021-01-04 r1 ==>> 0.000, r2 ==>> 0.000
2021-02-03 00:41:58.991  Status: dzVents: Debug: Ignoring 2021-01-05 r1 ==>> 0.000, r2 ==>> 0.000
2021-02-03 00:41:58.991  Status: dzVents: Debug: Ignoring 2021-01-06 r1 ==>> 0.000, r2 ==>> 0.026
2021-02-03 00:41:58.992  Status: dzVents: Debug: Ignoring 2021-01-07 r1 ==>> 0.000, r2 ==>> 0.000
2021-02-03 00:41:58.992  Status: dzVents: Debug: Ignoring 2021-01-08 r1 ==>> 0.000, r2 ==>> 0.232
2021-02-03 00:41:58.992  Status: dzVents: Debug: Ignoring 2021-01-09 r1 ==>> 1.785, r2 ==>> 0.000
2021-02-03 00:41:58.992  Status: dzVents: Debug: Ignoring 2021-01-10 r1 ==>> 0.481, r2 ==>> 0.000
2021-02-03 00:41:58.992  Status: dzVents: Debug: Ignoring 2021-01-11 r1 ==>> 0.000, r2 ==>> 0.000
2021-02-03 00:41:58.992  Status: dzVents: Debug: Ignoring 2021-01-12 r1 ==>> 0.000, r2 ==>> 1.090
2021-02-03 00:41:58.993  Status: dzVents: Debug: Ignoring 2021-01-13 r1 ==>> 0.000, r2 ==>> 1.355
2021-02-03 00:41:58.993  Status: dzVents: Debug: Ignoring 2021-01-14 r1 ==>> 0.000, r2 ==>> 0.055
2021-02-03 00:41:58.993  Status: dzVents: Debug: Ignoring 2021-01-15 r1 ==>> 0.000, r2 ==>> 0.000
2021-02-03 00:41:58.993  Status: dzVents: Debug: Ignoring 2021-01-16 r1 ==>> 0.000, r2 ==>> 0.000
2021-02-03 00:41:58.993  Status: dzVents: Debug: Ignoring 2021-01-17 r1 ==>> 0.502, r2 ==>> 0.000
2021-02-03 00:41:58.993  Status: dzVents: Debug: Ignoring 2021-01-18 r1 ==>> 0.000, r2 ==>> 0.055
2021-02-03 00:41:58.993  Status: dzVents: Debug: Ignoring 2021-01-19 r1 ==>> 0.000, r2 ==>> 0.000
2021-02-03 00:41:58.994  Status: dzVents: Debug: Ignoring 2021-01-20 r1 ==>> 0.000, r2 ==>> 0.080
2021-02-03 00:41:58.994  Status: dzVents: Debug: Ignoring 2021-01-21 r1 ==>> 0.000, r2 ==>> 0.634
2021-02-03 00:41:58.994  Status: dzVents: Debug: Ignoring 2021-01-22 r1 ==>> 0.000, r2 ==>> 1.015
2021-02-03 00:41:58.994  Status: dzVents: Debug: Ignoring 2021-01-23 r1 ==>> 0.242, r2 ==>> 0.000
2021-02-03 00:41:58.994  Status: dzVents: Debug: Ignoring 2021-01-24 r1 ==>> 0.376, r2 ==>> 0.000
2021-02-03 00:41:58.994  Status: dzVents: Debug: Ignoring 2021-01-25 r1 ==>> 0.000, r2 ==>> 2.221
2021-02-03 00:41:58.995  Status: dzVents: Debug: Ignoring 2021-01-26 r1 ==>> 0.000, r2 ==>> 2.044
2021-02-03 00:41:58.995  Status: dzVents: Debug: Ignoring 2021-01-27 r1 ==>> 0.000, r2 ==>> 0.014
2021-02-03 00:41:58.995  Status: dzVents: Debug: Ignoring 2021-01-28 r1 ==>> 0.000, r2 ==>> 0.000
2021-02-03 00:41:58.995  Status: dzVents: Debug: Ignoring 2021-01-29 r1 ==>> 0.000, r2 ==>> 0.465
2021-02-03 00:41:58.995  Status: dzVents: Debug: Ignoring 2021-01-30 r1 ==>> 0.318, r2 ==>> 0.000
2021-02-03 00:41:58.995  Status: dzVents: Debug: Ignoring 2021-01-31 r1 ==>> 2.722, r2 ==>> 0.000
2021-02-03 00:41:58.995  Status: dzVents: Debug: Counting _________________ 2021-02-01 r1 ==>> 0.000, r2 ==>> 0.000
2021-02-03 00:41:58.996  Status: dzVents: Debug: Counting _________________ 2021-02-02 r1 ==>> 0.000, r2 ==>> 0.166
2021-02-03 00:41:58.996  Status: dzVents: Debug: Counting _________________ 2021-02-03 r1 ==>> 0.000, r2 ==>> 0.000

Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Kranendijk
Posts: 60
Joined: Wednesday 29 May 2019 14:39
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Location: Zwolle
Contact:

Re: Managed counter not updating

Post by Kranendijk »

Thanks!
9 Panels here and south oriented.

Results do unfortunately not match the readings from my meter which I will include for the days this month. Weird? If it were like 20 Watts or so, then I could imagine, but it is 2.421 Watts, which is 2.4 kWh.

Log:
Spoiler: show
2021-02-03 09:55:00.461 Status: dzVents: Info: ------ Start internal script: 034StroomPanelenMaand:, trigger: "every 1 minutes"
2021-02-03 09:55:00.462 Status: dzVents: Debug: Processing device-adapter for StroomPanelenMaand: Counter device adapter
2021-02-03 09:55:00.462 Status: dzVents: Debug: OpenURL: url = http://127.0.0.1:8080/json.htm?type=gra ... onth&idx=1
2021-02-03 09:55:00.462 Status: dzVents: Debug: OpenURL: method = GET
2021-02-03 09:55:00.462 Status: dzVents: Debug: OpenURL: post data = nil
2021-02-03 09:55:00.463 Status: dzVents: Debug: OpenURL: headers = nil
2021-02-03 09:55:00.463 Status: dzVents: Debug: OpenURL: callback = monthTotal_month
2021-02-03 09:55:00.463 Status: dzVents: Info: ------ Finished 034StroomPanelenMaand
2021-02-03 09:55:00.472 Status: EventSystem: Script event triggered: /home/pi/domoticz/dzVents/runtime/dzVents.lua
2021-02-03 09:55:00.737 Status: dzVents: Info: Handling httpResponse-events for: "monthTotal_month"
2021-02-03 09:55:00.757 Status: dzVents: Info: ------ Start internal script: 034StroomPanelenMaand: HTTPResponse: "monthTotal_month"
2021-02-03 09:55:00.773 Status: dzVents: Debug: Processing device-adapter for StroomPanelenMaand: Counter device adapter
2021-02-03 09:55:00.773 Status: dzVents: Debug: Ignoring 2021-01-03 r1 ==>> 0.000, r2 ==>> 0.000
2021-02-03 09:55:00.773 Status: dzVents: Debug: Ignoring 2021-01-04 r1 ==>> 0.000, r2 ==>> 0.000
2021-02-03 09:55:00.773 Status: dzVents: Debug: Ignoring 2021-01-05 r1 ==>> 0.000, r2 ==>> 0.000
2021-02-03 09:55:00.773 Status: dzVents: Debug: Ignoring 2021-01-06 r1 ==>> 0.000, r2 ==>> 0.000
2021-02-03 09:55:00.773 Status: dzVents: Debug: Ignoring 2021-01-07 r1 ==>> 0.000, r2 ==>> 0.027
2021-02-03 09:55:00.774 Status: dzVents: Debug: Ignoring 2021-01-08 r1 ==>> 0.000, r2 ==>> 0.638
2021-02-03 09:55:00.774 Status: dzVents: Debug: Ignoring 2021-01-09 r1 ==>> 1.436, r2 ==>> 0.000
2021-02-03 09:55:00.774 Status: dzVents: Debug: Ignoring 2021-01-10 r1 ==>> 0.565, r2 ==>> 0.000
2021-02-03 09:55:00.774 Status: dzVents: Debug: Ignoring 2021-01-11 r1 ==>> 0.000, r2 ==>> 0.000
2021-02-03 09:55:00.774 Status: dzVents: Debug: Ignoring 2021-01-12 r1 ==>> 0.000, r2 ==>> 3.604
2021-02-03 09:55:00.774 Status: dzVents: Debug: Ignoring 2021-01-13 r1 ==>> 0.000, r2 ==>> 0.137
2021-02-03 09:55:00.774 Status: dzVents: Debug: Ignoring 2021-01-14 r1 ==>> 0.000, r2 ==>> 2.048
2021-02-03 09:55:00.774 Status: dzVents: Debug: Ignoring 2021-01-15 r1 ==>> 0.000, r2 ==>> 0.000
2021-02-03 09:55:00.774 Status: dzVents: Debug: Ignoring 2021-01-16 r1 ==>> 0.000, r2 ==>> 0.000
2021-02-03 09:55:00.774 Status: dzVents: Debug: Ignoring 2021-01-17 r1 ==>> 0.000, r2 ==>> 0.000
2021-02-03 09:55:00.774 Status: dzVents: Debug: Ignoring 2021-01-18 r1 ==>> 0.000, r2 ==>> 0.000
2021-02-03 09:55:00.774 Status: dzVents: Debug: Ignoring 2021-01-19 r1 ==>> 0.000, r2 ==>> 0.000
2021-02-03 09:55:00.774 Status: dzVents: Debug: Ignoring 2021-01-20 r1 ==>> 0.000, r2 ==>> 0.022
2021-02-03 09:55:00.774 Status: dzVents: Debug: Ignoring 2021-01-21 r1 ==>> 0.000, r2 ==>> 1.697
2021-02-03 09:55:00.774 Status: dzVents: Debug: Ignoring 2021-01-22 r1 ==>> 0.000, r2 ==>> 0.395
2021-02-03 09:55:00.774 Status: dzVents: Debug: Ignoring 2021-01-23 r1 ==>> 1.220, r2 ==>> 0.000
2021-02-03 09:55:00.774 Status: dzVents: Debug: Ignoring 2021-01-24 r1 ==>> 1.942, r2 ==>> 0.000
2021-02-03 09:55:00.774 Status: dzVents: Debug: Ignoring 2021-01-25 r1 ==>> 0.000, r2 ==>> 5.143
2021-02-03 09:55:00.774 Status: dzVents: Debug: Ignoring 2021-01-26 r1 ==>> 0.000, r2 ==>> 3.266
2021-02-03 09:55:00.774 Status: dzVents: Debug: Ignoring 2021-01-27 r1 ==>> 0.000, r2 ==>> 0.000
2021-02-03 09:55:00.774 Status: dzVents: Debug: Ignoring 2021-01-28 r1 ==>> 0.000, r2 ==>> 0.000
2021-02-03 09:55:00.774 Status: dzVents: Debug: Ignoring 2021-01-29 r1 ==>> 0.000, r2 ==>> 0.000
2021-02-03 09:55:00.774 Status: dzVents: Debug: Ignoring 2021-01-30 r1 ==>> 0.284, r2 ==>> 0.000
2021-02-03 09:55:00.774 Status: dzVents: Debug: Ignoring 2021-01-31 r1 ==>> 2.755, r2 ==>> 0.000
2021-02-03 09:55:00.774 Status: dzVents: Debug: Counting _________________ 2021-02-01 r1 ==>> 0.000, r2 ==>> 0.000
2021-02-03 09:55:00.774 Status: dzVents: Debug: Counting _________________ 2021-02-02 r1 ==>> 0.000, r2 ==>> 0.000
2021-02-03 09:55:00.774 Status: dzVents: Debug: Counting _________________ 2021-02-03 r1 ==>> 0.000, r2 ==>> 0.000
2021-02-03 09:55:00.775 Status: dzVents: Info: ------ Finished 034StroomPanelenMaand
2021-02-03 09:55:00.775 Status: EventSystem: Script event triggered: /home/pi/domoticz/dzVents/runtime/dzVents.lua
2021-02-03 09:55:06.771 Status: EventSystem: reset all events...
2021-02-03 09:55:06.778 Status: dzVents: Write file: /home/pi/domot
JSON call to device:
Spoiler: show
{
"d" : "2020-01-31",
"r1" : "0.000",
"r2" : "0.131",
"v" : "1.754",
"v2" : "6.793"
},
{
"d" : "2020-02-01",
"r1" : "0.474",
"r2" : "0.000",
"v" : "11.768",
"v2" : "0.000"
},
{
"d" : "2020-02-02",
"r1" : "0.001",
"r2" : "0.000",
"v" : "18.448",
"v2" : "0.000"
},
{
"d" : "2020-02-03",
"r1" : "0.000",
"r2" : "1.815",
"v" : "2.127",
"v2" : "4.844"
}
],
"status" : "OK",
"title" : "Graph counter month"
}
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Managed counter not updating

Post by waaren »

Kranendijk wrote: Wednesday 03 February 2021 10:01 Results do unfortunately not match the readings from my meter which I will include for the days this month. Weird? If it were like 20 Watts or so, then I could imagine, but it is 2.421 Watts, which is 2.4 kWh.
Your JSON only shows values from 2020
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Kranendijk
Posts: 60
Joined: Wednesday 29 May 2019 14:39
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Location: Zwolle
Contact:

Re: Managed counter not updating

Post by Kranendijk »

Crap, you are right. Now I'm feeling really-really stupid... Checking JSON in 2021 leaves me with 0 Watts :oops:
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests