Reporting maximum usage Watt's at any time

Moderator: leecollings

Post Reply
Bikey
Posts: 331
Joined: Sunday 22 February 2015 12:19
Target OS: Linux
Domoticz version: 2020.x
Location: Netherlands
Contact:

Reporting maximum usage Watt's at any time

Post by Bikey »

Hi guys, I have the following case:

To check what the spare capacity is my electrical home installation I would like to know what the maximum of instant power usage (in Watts) was for any given period. This gives me an insight in what spare capacity I have to charge my electric car, and at what speed.

Of course you can check this by looking at graph for the short log interval for e.g. the P1 meter, but that is limited to 7 days.

What I was thinking about is to have a script that regularly (e.g. daily) checks this logdata for a maximum value, and if it finds one that is higher then a previous found maximum, use that as the new maximum value. This then could be stored in a Texvariable together with the timestamp of the found highest usage.

My programming skill are very minimal, would anybody know how to program this? Or have another solution?

Thanks for your ideas and suggestions!
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Reporting maximum usage Watt's at any time

Post by waaren »

Bikey wrote: Wednesday 09 January 2019 19:43 What I was thinking about is to have a script that regularly (e.g. daily) checks this logdata for a maximum value, and if it finds one that is higher then a previous found maximum, use that as the new maximum value. This then could be stored in a Texvariable together with the timestamp of the found highest usage.
Something like this dzVents script ? Enter your device ID for your energydevice (P1) and of a new textDevice in the two lines that are clearly marked in the script. Script will start twice a day and update the Text device.
If you are not yet familiar with dzvents then please look at the 10 lines using dzVents with Domoticz for an intro to dzVents and howto get this script active in domoticz.

have Fun !

Code: Select all

-- getMaxWatt

local httpResponses = "getMaxWatt"
return {
    on      =   {   
                    timer           =   { "at 23:55","at 11:55"   }, -- twice a day to be sure  
                    httpResponses   =   { httpResponses           } 
                },
                
    logging =   {   
                    level           =   domoticz.LOG_ERROR,
                    marker          =   httpResponse   
                },
                    
    data    =   {   
                    maxWatt            =   { initial = 0          },
                    lastExecutionTime  =   { initial =  ""        },
                    maxTime            =   { initial =  ""        }
                },
                
    execute = function(dz, item)
        -- ****************************** Your settings below this line ***************************************************
        energyDevice = dz.devices(35)          -- Replace with ID of energyDevice you want to track
        textDevice   = dz.devices(1290)        -- Create as virtual text device and change 1290 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()
            local  URLString   = dz.settings['Domoticz url'] .. "/json.htm?type=graph&sensor=counter&range=day&idx=" .. energyDevice.id 
            dz.openURL({    url = URLString,
                            method = "GET",
                            callback = httpResponses })                      
        end
        
        local function convertTime(timeString)               -- based on incoming format yyyy-mm-dd hh:mm
            local year, month, day, hour, minute 
            _,_, year, month, day, hour, minute = string.find(timeString, "(%d+)-(%d+)-(%d+) (%d+):(%d+)")
            fmtString = os.date("%A, %d %B %Y (%H:%M)",os.time({year=year, month=month, day=day, hour=hour, min=minute })):gsub(" 0"," ")
            logWrite(fmtString)
            return fmtString                                 -- outgoing format: ddd d mmmm yyyy (hh:mm)
        end
        
        local function showResult(str)
            textDevice.updateText(str)
            logWrite(str,dz.LOG_FORCE)
        end
        
        local function getMax(rt)
            local max       = dz.data.maxWatt
            local maxWatt   = 0
            local maxTime   = dz.data.maxTime
            local keyDate   
            
            for key in ipairs(rt) do
                maxWatt = math.max(rt[key].v,rt[key].v2)                    -- low or high tariff
                if maxWatt > max then
                    logWrite("New max found on " .. convertTime(rt[key].d) .. " ==>> " .. maxWatt )
                    max = maxWatt
                    keyDate = rt[key].d
                end
            end
            if max <= dz.data.maxWatt then
                showResult("No new max found. maxWatt still " .. dz.data.maxWatt .. " Watt, measured on " .. dz.data.maxTime)
            else 
                dz.data.maxWatt = max
                dz.data.maxTime = convertTime(keyDate)
                showResult("New max found. maxWatt is now " .. dz.data.maxWatt .. " Watt, measured on " .. dz.data.maxTime)
            end    
        end    
        
        if not item.isHTTPResponse then
            triggerJSON()
            dz.data.lastExecutionTime = dz.time.rawDate .." " .. dz.time.rawTime    
        elseif item.ok then                                      -- statusCode == 2xx
            getMax(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
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
Egregius
Posts: 2582
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: v2024.7
Location: Beitem, BE
Contact:

Re: Reporting maximum usage Watt's at any time

Post by Egregius »

I don 't get the idea behind this. My peak power usage is way over the maximum current I can use. Sometimes I hit peaks over 11000W on a 40A/240V mono fase line wich should break at 9600W.
I think it would be better to adjust the charger in realtime, if it has an API to do that.
Or easier to cut other power usage while the total consumption goes above a certain level. I do that for some electrical heaters while I'm cooking, they are turned off and on depending on the total power consumption.
Bikey
Posts: 331
Joined: Sunday 22 February 2015 12:19
Target OS: Linux
Domoticz version: 2020.x
Location: Netherlands
Contact:

Re: Reporting maximum usage Watt's at any time

Post by Bikey »

You are talking about load balancing: that is actsully why I was looking at this. To see if there will be many cases where I will need that. These solutions often cost quite some money and mostly also need a monthly subscription, (unless you are building it yourself) so I was wondering if I really need that.
Bikey
Posts: 331
Joined: Sunday 22 February 2015 12:19
Target OS: Linux
Domoticz version: 2020.x
Location: Netherlands
Contact:

Re: Reporting maximum usage Watt's at any time

Post by Bikey »

waaren wrote: Thursday 10 January 2019 2:22
Bikey wrote: Wednesday 09 January 2019 19:43 What I was thinking about is to have a script that regularly (e.g. daily) checks this logdata for a maximum value, and if it finds one that is higher then a previous found maximum, use that as the new maximum value. This then could be stored in a Texvariable together with the timestamp of the found highest usage.
Something like this dzVents script ? Enter your device ID for your energydevice (P1) and of a new textDevice in the two lines that are clearly marked in the script. Script will start twice a day and update the Text device.
If you are not yet familiar with dzvents then please look at the 10 lines using dzVents with Domoticz for an intro to dzVents and howto get this script active in domoticz.

have Fun !

Code: Select all

-- getMaxWatt

local httpResponses = "getMaxWatt"
return {
    on      =   {   
                    timer           =   { "at 23:55","at 11:55"   }, -- twice a day to be sure  
                    httpResponses   =   { httpResponses           } 
                },
                
    logging =   {   
                    level           =   domoticz.LOG_ERROR,
                    marker          =   httpResponse   
                },
                    
    data    =   {   
                    maxWatt            =   { initial = 0          },
                    lastExecutionTime  =   { initial =  ""        },
                    maxTime            =   { initial =  ""        }
                },
                
    execute = function(dz, item)
        -- ****************************** Your settings below this line ***************************************************
        energyDevice = dz.devices(35)          -- Replace with ID of energyDevice you want to track
        textDevice   = dz.devices(1290)        -- Create as virtual text device and change 1290 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()
            local  URLString   = dz.settings['Domoticz url'] .. "/json.htm?type=graph&sensor=counter&range=day&idx=" .. energyDevice.id 
            dz.openURL({    url = URLString,
                            method = "GET",
                            callback = httpResponses })                      
        end
        
        local function convertTime(timeString)               -- based on incoming format yyyy-mm-dd hh:mm
            local year, month, day, hour, minute 
            _,_, year, month, day, hour, minute = string.find(timeString, "(%d+)-(%d+)-(%d+) (%d+):(%d+)")
            fmtString = os.date("%A, %d %B %Y (%H:%M)",os.time({year=year, month=month, day=day, hour=hour, min=minute })):gsub(" 0"," ")
            logWrite(fmtString)
            return fmtString                                 -- outgoing format: ddd d mmmm yyyy (hh:mm)
        end
        
        local function showResult(str)
            textDevice.updateText(str)
            logWrite(str,dz.LOG_FORCE)
        end
        
        local function getMax(rt)
            local max       = dz.data.maxWatt
            local maxWatt   = 0
            local maxTime   = dz.data.maxTime
            local keyDate   
            
            for key in ipairs(rt) do
                maxWatt = math.max(rt[key].v,rt[key].v2)                    -- low or high tariff
                if maxWatt > max then
                    logWrite("New max found on " .. convertTime(rt[key].d) .. " ==>> " .. maxWatt )
                    max = maxWatt
                    keyDate = rt[key].d
                end
            end
            if max <= dz.data.maxWatt then
                showResult("No new max found. maxWatt still " .. dz.data.maxWatt .. " Watt, measured on " .. dz.data.maxTime)
            else 
                dz.data.maxWatt = max
                dz.data.maxTime = convertTime(keyDate)
                showResult("New max found. maxWatt is now " .. dz.data.maxWatt .. " Watt, measured on " .. dz.data.maxTime)
            end    
        end    
        
        if not item.isHTTPResponse then
            triggerJSON()
            dz.data.lastExecutionTime = dz.time.rawDate .." " .. dz.time.rawTime    
        elseif item.ok then                                      -- statusCode == 2xx
            getMax(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
}
Looks good, wow!
Did you already have this or are you that fast in programming? ;-)
I’m going to look at this, Dzvents is new to me.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Reporting maximum usage Watt's at any time

Post by waaren »

Bikey wrote: Thursday 10 January 2019 7:48 Did you already have this ?
Not completely but almost all the building blocks are copy/pastes from scripts I wrote before and dzVents makes it a lot easier to work with domoticz devices and -data.
I’m going to look at this, Dzvents is new to me.
I am sure you are going to love it ! :)
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Bikey
Posts: 331
Joined: Sunday 22 February 2015 12:19
Target OS: Linux
Domoticz version: 2020.x
Location: Netherlands
Contact:

Re: Reporting maximum usage Watt's at any time

Post by Bikey »

waaren wrote: Thursday 10 January 2019 8:14
Bikey wrote: Thursday 10 January 2019 7:48 Did you already have this ?
Not completely but almost all the building blocks are copy/pastes from scripts I wrote before and dzVents makes it a lot easier to work with domoticz devices and -data.
I’m going to look at this, Dzvents is new to me.
I am sure you are going to love it ! :)
After some fiddling, I got the script working. Works completely as I hoped thanks!

Only a small bug I found: it rounded the minutes to "00". I found that the bug is in this line:
fmtString = os.date("%A, %d %B %Y (%H:%M)",os.time({year=year, month=month, day=day, hour=hour, minute=minute })):gsub(" 0"," ")

The "minute=minute" part should be "min=minute"

Perhaps this part is something you are also using in your scripts as well, so I hope have helped a little back ;-)
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Reporting maximum usage Watt's at any time

Post by waaren »

Bikey wrote: Saturday 12 January 2019 20:53 The "minute=minute" part should be "min=minute"
Perhaps this part is something you are also using in your scripts as well, so I hope have helped a little back ;-)
Thanks for the feedback and fix. Much appreciated !
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
lemassykoi
Posts: 15
Joined: Saturday 11 March 2017 23:51
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.1
Location: France
Contact:

Re: Reporting maximum usage Watt's at any time

Post by lemassykoi »

Hi,

I have a problem with this script :

Code: Select all

2020-01-29 05:24:02.270 Error: dzVents: Error: (2.5.5) An error occurred when calling event handler report_max_watts
2020-01-29 05:24:02.270 Error: dzVents: Error: (2.5.5) ...z/scripts/dzVents/generated_scripts/report_max_watts.lua:60: attempt to compare number with string
However, I tested var max and var maxWatt, and each has a correct number value.

What could be the problem ? Thanks
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Reporting maximum usage Watt's at any time

Post by waaren »

lemassykoi wrote: Wednesday 29 January 2020 5:26 I have a problem with this script :

Code: Select all

2020-01-29 05:24:02.270 Error: dzVents: Error: (2.5.5) An error occurred when calling event handler report_max_watts
2020-01-29 05:24:02.270 Error: dzVents: Error: (2.5.5) ...z/scripts/dzVents/generated_scripts/report_max_watts.lua:60: attempt to compare number with string
Can you show us the script that you use ? I do not see a comparison on line 60 in the version posted here.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
lemassykoi
Posts: 15
Joined: Saturday 11 March 2017 23:51
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.1
Location: France
Contact:

Re: Reporting maximum usage Watt's at any time

Post by lemassykoi »

I just commented your timer line to add mine (line 6)

Line 60 for me :

Code: Select all

 if maxWatt > max then
and when I tested it, there was maxWatt at around 11000 and max at 0
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Reporting maximum usage Watt's at any time

Post by waaren »

lemassykoi wrote: Wednesday 29 January 2020 20:02 Line 60 for me :

Code: Select all

 if maxWatt > max then
To debug can you please replace function getMax (starting at line 52 en ending at line 73 ) with this

Code: Select all

        local function getMax(rt)
            local max       = dz.data.maxWatt
            local maxWatt   = 0
            local maxTime   = dz.data.maxTime
            local keyDate   
            
            for key in ipairs(rt) do
                maxWatt = math.max(rt[key].v,rt[key].v2)                    -- low or high tariff
                
                if type(maxWatt) == 'string' or type(max) == 'string' then 
                    dz.log('max has type ' .. type(max) .. ' and value ' .. max, dz.LOG_ERROR )
                    dz.log('maxWatt has type ' .. type(maxWatt) .. ' and value ' .. maxWatt, dz.LOG_ERROR )
                    dz.utils.dumpTable(rt[key])
                elseif maxWatt > max then
                    logWrite("New max found on " .. convertTime(rt[key].d) .. " ==>> " .. maxWatt )
                    max = maxWatt
                    keyDate = rt[key].d
                end
            end
            if max <= dz.data.maxWatt then
                showResult("No new max found. maxWatt still " .. dz.data.maxWatt .. " Watt, measured on " .. dz.data.maxTime)
            else 
                dz.data.maxWatt = max
                dz.data.maxTime = convertTime(keyDate)
                showResult("New max found. maxWatt is now " .. dz.data.maxWatt .. " Watt, measured on " .. dz.data.maxTime)
            end    
        end    

Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
lemassykoi
Posts: 15
Joined: Saturday 11 March 2017 23:51
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.1
Location: France
Contact:

Re: Reporting maximum usage Watt's at any time

Post by lemassykoi »

Code: Select all

2020-01-30 08:22:02.799 Error: dzVents: Error: (2.5.5) maxWatt has type string and value 5346
2020-01-30 08:22:02.799 Error: dzVents: Error: (2.5.5) max has type number and value 0
2020-01-30 08:22:02.799 Error: dzVents: Error: (2.5.5) maxWatt has type string and value 5472
2020-01-30 08:22:02.799 Error: dzVents: Error: (2.5.5) max has type number and value 0
2020-01-30 08:22:02.799 Error: dzVents: Error: (2.5.5) maxWatt has type string and value 4345
2020-01-30 08:22:02.799 Error: dzVents: Error: (2.5.5) max has type number and value 0
2020-01-30 08:22:02.799 Error: dzVents: Error: (2.5.5) maxWatt has type string and value 4596
2020-01-30 08:22:02.800 Error: dzVents: Error: (2.5.5) max has type number and value 0
2020-01-30 08:22:02.800 Error: dzVents: Error: (2.5.5) maxWatt has type string and value 4476
2020-01-30 08:22:02.800 Error: dzVents: Error: (2.5.5) max has type number and value 0
2020-01-30 08:22:02.800 Error: dzVents: Error: (2.5.5) maxWatt has type string and value 4104
2020-01-30 08:22:02.800 Error: dzVents: Error: (2.5.5) max has type number and value 0
2020-01-30 08:22:02.800 Error: dzVents: Error: (2.5.5) maxWatt has type string and value 4176
2020-01-30 08:22:02.801 Error: dzVents: Error: (2.5.5) max has type number and value 0
2020-01-30 08:22:02.801 Error: dzVents: Error: (2.5.5) maxWatt has type string and value 5643
2020-01-30 08:22:02.801 Error: dzVents: Error: (2.5.5) max has type number and value 0
2020-01-30 08:22:02.801 Error: dzVents: Error: (2.5.5) maxWatt has type string and value 5376
2020-01-30 08:22:02.801 Error: dzVents: Error: (2.5.5) max has type number and value 0
2020-01-30 08:22:02.801 Error: dzVents: Error: (2.5.5) maxWatt has type string and value 3792
2020-01-30 08:22:02.801 Error: dzVents: Error: (2.5.5) max has type number and value 0
2020-01-30 08:22:02.801 Error: dzVents: Error: (2.5.5) maxWatt has type string and value 4392
2020-01-30 08:22:02.802 Error: dzVents: Error: (2.5.5) max has type number and value 0
2020-01-30 08:22:02.802 Error: dzVents: Error: (2.5.5) maxWatt has type string and value 4284
2020-01-30 08:22:02.802 Error: dzVents: Error: (2.5.5) max has type number and value 0
2020-01-30 08:22:02.802 Error: dzVents: Error: (2.5.5) maxWatt has type string and value 3828
2020-01-30 08:22:02.802 Error: dzVents: Error: (2.5.5) max has type number and value 0
2020-01-30 08:22:02.802 Error: dzVents: Error: (2.5.5) maxWatt has type string and value 4872
2020-01-30 08:22:02.802 Error: dzVents: Error: (2.5.5) max has type number and value 0
2020-01-30 08:22:02.802 Error: dzVents: Error: (2.5.5) maxWatt has type string and value 6000
2020-01-30 08:22:02.803 Error: dzVents: Error: (2.5.5) max has type number and value 0
2020-01-30 08:22:02.803 Error: dzVents: Error: (2.5.5) maxWatt has type string and value 4740
2020-01-30 08:22:02.803 Error: dzVents: Error: (2.5.5) max has type number and value 0
2020-01-30 08:22:02.803 Error: dzVents: Error: (2.5.5) maxWatt has type string and value 2200
2020-01-30 08:22:02.803 Error: dzVents: Error: (2.5.5) max has type number and value 0
2020-01-30 08:22:02.803 Error: dzVents: Error: (2.5.5) maxWatt has type string and value 4008
2020-01-30 08:22:02.804 Error: dzVents: Error: (2.5.5) max has type number and value 0
2020-01-30 08:22:02.804 Error: dzVents: Error: (2.5.5) maxWatt has type string and value 4279
2020-01-30 08:22:02.804 Error: dzVents: Error: (2.5.5) max has type number and value 0
2020-01-30 08:22:02.804 Error: dzVents: Error: (2.5.5) maxWatt has type string and value 4104
2020-01-30 08:22:02.804 Error: dzVents: Error: (2.5.5) max has type number and value 0
2020-01-30 08:22:02.804 Error: dzVents: Error: (2.5.5) maxWatt has type string and value 5292
2020-01-30 08:22:02.804 Error: dzVents: Error: (2.5.5) max has type number and value 0
2020-01-30 08:22:02.805 Error: dzVents: Error: (2.5.5) maxWatt has type string and value 5357
2020-01-30 08:22:02.805 Error: dzVents: Error: (2.5.5) max has type number and value 0
2020-01-30 08:22:02.805 Error: dzVents: Error: (2.5.5) maxWatt has type string and value 4452
2020-01-30 08:22:02.805 Error: dzVents: Error: (2.5.5) max has type number and value 0
2020-01-30 08:22:02.805 Error: dzVents: Error: (2.5.5) maxWatt has type string and value 3476
2020-01-30 08:22:02.805 Error: dzVents: Error: (2.5.5) max has type number and value 0
2020-01-30 08:22:02.805 Error: dzVents: Error: (2.5.5) maxWatt has type string and value 3996
2020-01-30 08:22:02.805 Error: dzVents: Error: (2.5.5) max has type number and value 0
Thanks for helping
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Reporting maximum usage Watt's at any time

Post by waaren »

lemassykoi wrote: Thursday 30 January 2020 8:23 Thanks for helping
Thx for reporting !

To fix: change in the original script the line

Code: Select all

maxWatt = math.max(rt[key].v,rt[key].v2)
to

Code: Select all

maxWatt = tonumber(math.max(rt[key].v,rt[key].v2))
What caused the issue:
Domoticz version V4.11439 upgraded the internal Lua engine from 5.2 to 5.3 and in Lua 5.3 the behavior of the math.max changed.
Before Lua 5.3 the result of math.max was always a number while in 5.3 it will be string if all values passed to it are strings.
Lua 5.1.5 Copyright (C) 1994-2012 Lua.org, PUC-Rio

Code: Select all

> a = '11'
> b = '12'
> c = math.max(a,b)
> print(type(c) ..': '..  c)
number: 12
Lua 5.3.3 Copyright (C) 1994-2016 Lua.org, PUC-Rio

Code: Select all

> a = '11'
> b = '12'
> c = math.max(a,b)
> print(type(c) ..': '..  c)
string: 12
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
lemassykoi
Posts: 15
Joined: Saturday 11 March 2017 23:51
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.1
Location: France
Contact:

Re: Reporting maximum usage Watt's at any time

Post by lemassykoi »

Thanks a lot for solution and explanation. It works.
I'm not expert enough in LUA to diagnose that.

For Info, here is my version :

Code: Select all

lua -v
Lua 5.2.4  Copyright (C) 1994-2015 Lua.org, PUC-Rio
on DomoticZ v4.11605

Thanks a lot again for your help, I saw a few posts from you with LUA exemples. It's because of people like you that I can find almost any solution to any problems, with some piece of code.

Edit : here is "mine", with a new device to update, a numeric one (type Usage). I update it even if no change.

Code: Select all

-- getMaxWatt

local httpResponses = "getMaxWatt"
return {
    on      =   {   
                    timer           =   { "at *:15","at *:45"   }, -- twice an hour, twice a day, to be very sure  
                    httpResponses   =   { httpResponses           } 
                },
                
    logging =   {   
                    level           =   domoticz.LOG_ERROR,
                    marker          =   httpResponse   
                },
                    
    data    =   {   
                    maxWatt            =   { initial = 0          },
                    lastExecutionTime  =   { initial =  ""        },
                    maxTime            =   { initial =  ""        }
                },
                
    execute = function(dz, item)
        -- ****************************** Your settings below this line ***********************************************************************
        energyDevice     = dz.devices(ID)        -- Replace with ID of energyDevice you want to track
        textDevice       = dz.devices(ID)        -- Create a virtual text device and change ID to the ID of this new device
        numericDevice    = dz.devices(ID)        -- Create a virtual usage device and change ID to the ID of this 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()
            local  URLString   = dz.settings['Domoticz url'] .. "/json.htm?type=graph&sensor=counter&range=day&idx=" .. energyDevice.id 
            dz.openURL({    url = URLString,
                            method = "GET",
                            callback = httpResponses })                      
        end
        
        local function convertTime(timeString)               -- based on incoming format yyyy-mm-dd hh:mm
            local year, month, day, hour, minute 
            _,_, year, month, day, hour, minute = string.find(timeString, "(%d+)-(%d+)-(%d+) (%d+):(%d+)")
            fmtString = os.date("%A, %d %B %Y (%H:%M)",os.time({year=year, month=month, day=day, hour=hour, min=minute })):gsub(" 0"," ")
            logWrite(fmtString)
            return fmtString                                 -- outgoing format: ddd d mmmm yyyy (hh:mm)
        end
        
        local function showResult(str)
            textDevice.updateText(str)
            logWrite(str,dz.LOG_FORCE)
        end
        
        local function showNumericResult(str)
            numericDevice.updateEnergy(str)
        end
        
        local function getMax(rt)
            local max       = dz.data.maxWatt
            local maxWatt   = 0
            local maxTime   = dz.data.maxTime
            local keyDate   
            
            for key in ipairs(rt) do
                --maxWatt = math.max(rt[key].v,rt[key].v2)                    -- low or high tariff
                maxWatt = tonumber(math.max(rt[key].v,rt[key].v2))
                
                if type(maxWatt) == 'string' or type(max) == 'string' then 
                    dz.log('max has type ' .. type(max) .. ' and value ' .. max, dz.LOG_ERROR )
                    dz.log('maxWatt has type ' .. type(maxWatt) .. ' and value ' .. maxWatt, dz.LOG_ERROR )
                    dz.utils.dumpTable(rt[key])
                elseif maxWatt > max then
                    logWrite("New max found on " .. convertTime(rt[key].d) .. " ==>> " .. maxWatt )
                    max = maxWatt
                    keyDate = rt[key].d
                end
            end
            if max <= dz.data.maxWatt then
                showResult("No new max found. maxWatt still " .. dz.data.maxWatt .. " Watt, measured on " .. dz.data.maxTime)
                showNumericResult(dz.data.maxWatt)
            else 
                dz.data.maxWatt = max
                dz.data.maxTime = convertTime(keyDate)
                showResult("New max found. maxWatt is now " .. dz.data.maxWatt .. " Watt, measured on " .. dz.data.maxTime)
                showNumericResult(dz.data.maxWatt)
            end    
        end    
   
        
        if not item.isHTTPResponse then
            triggerJSON()
            dz.data.lastExecutionTime = dz.time.rawDate .." " .. dz.time.rawTime    
        elseif item.ok then                                      -- statusCode == 2xx
            getMax(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
}
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests