Page 1 of 1

Find out Max value from different variables

Posted: Monday 14 November 2022 15:15
by CronoS
Hi,

I have 13 solarpanels where every panel reports to Domoticz every Minute with how many Watts they are producing at that moment. What I would like to find out what for these 13 solarpanels the max Watt is for that moment and to display this value in Domoticz. And on the next minute i want to recheck this if this is greater and then reset this on a daily basis.

I know in basic how I should write these lines of code, so I think I can manage that, but what I do not know is how to calculate a max value when comparing those different value's

Ofcourse I could do it with the > to find out which of the two value's are greater, but then I would need allot of these entries in the script, I was hoping that It can be done easier with a few lines of code where I can say that this is the max value by comparing the variables. But unfortunately I do not know how to do that..

As a start I have this, this is not a working script yet.. still a rough setup to start with:

Code: Select all

local ScriptVersion = '0.1.9'
local fetchIntervalMins = 1

return
{
    on =
    {
        timer = { 'every minute between 15 minutes after sunset and 15 minutes before sunrise' } 
    },

    logging =
    {
        level = domoticz.LOG_INFO,    -- Uncomment this line to override the dzVents global logging setting LOG_DEBUG for debug loggin
        marker = 'SME '.. ScriptVersion,
    },

    execute = function(dz, item)

    --  Read the IDX
        local Panel1 = 3330   -- Panel 1
        local Panel2 = 3331 -- Panel 2
        local Panel3 = 3332  -- Panel 3
        local Panel4 = 3333   -- Panel 4
        local Panel5 = 3334 -- Panel 5
        local Panel6 = 3335 -- Panel 6
        local Panel7 = 3336 -- Panel 7
        local Panel8 = 3337 -- Panel 8
        local Panel9 = 3338 -- Panel 9 
        local Panel10 = 3339 -- Panel 10 
        local Panel11 = 3340 -- Panel 11 
        local Panel12 = 3341 -- Panel 12 
        local Panel13 = 3342 -- Panel 13
        
        Local MaxWatt = 3343 --MaxValue
        
        local function logWrite(str,level)
            dz.log(tostring(str),level or dz.LOG_DEBUG)
        end

 
        -- Read Totals Watt
        local Panel1Watt = Panel1.sensorValue -- (only three to start with)
        local Panel2Watt = Panel2.sensorValue
        local Panel3Watt = Panel3.sensorValue
        
        .... -- (Now I want to compare those three to find out which one has the max value)
        
Any idea?

Best Regards

Re: Find out Max value from different variables

Posted: Monday 14 November 2022 16:30
by willemd
Can you use the

Code: Select all

max( [fromIdx], [toIdx] ): Returns the highest value in the range defined by fromIdx and toIdx.
as explained on the Dzvents wiki page?

I have never used it but it seems appropriate for your purpose.

Re: Find out Max value from different variables

Posted: Monday 14 November 2022 19:45
by CronoS
Any idea how to use it? I try somethings but that doesn't seems to work..

Re: Find out Max value from different variables

Posted: Monday 14 November 2022 21:39
by willemd
As stated, I have no experience with that max option.

Looking at it further, I would also investigate the forEach() function to loop through your collection of solarpanels. I have only tried one example of such a setup and find it difficult to understand, but any type of loop seems better that a large setup of if-statements, so I would certainly investigate it if I were you.

Sorry I am not able to help much further.

Re: Find out Max value from different variables

Posted: Tuesday 15 November 2022 21:23
by CronoS
No probs.. Difficult this: I also don't see it yet..

Re: Find out Max value from different variables

Posted: Wednesday 16 November 2022 17:24
by waltervl
For examples how to use statistical function in dzVents with persistent variables, search forum for "history = true"
search.php?keywords=history+%3D+true

One example viewtopic.php?p=288548

Re: Find out Max value from different variables

Posted: Thursday 17 November 2022 11:56
by CronoS
I have created now something with the ForEach(); seems to Work and it does not give me allot of code:

Code: Select all

local ScriptVersion = '0.1.9'
local fetchIntervalMins = 1

return
{
    on =
    {
        timer = { 'every minute' } 
    },

    logging =
    {
        level = domoticz.LOG_DEBUG,    -- Uncomment this line to override the dzVents global logging setting LOG_DEBUG for debug loggin
        marker = 'SME '.. ScriptVersion,
    },

    execute = function(dz, item)

        local function logWrite(str,level)
            dz.log(tostring(str),level or dz.LOG_DEBUG)
        end
        
    --  Read the IDX
        local Panels = {3330,3331,3332,3333,3334,3335,3336,3337,3338,3339,3340,3341,3342,}
       
        local Max = 3343 --MaxValue
        local MaxW = dz.devices(Max)
        local MaxWatt = MaxW.sensorValue

        -- Reset Watt
        if dz.time.matchesRule('at 00:05-00:10') then
            dz.devices(Max).updateCustomSensor(0)
        end

        -- Set Value        
        dz.devices().filter(Panels).forEach(function(panel)
            local PanelWatt = panel.actualWatt
            if PanelWatt ~= nil then
                if PanelWatt > MaxWatt then
                    dz.devices(Max).updateCustomSensor(PanelWatt)
                end
            end
            if MaxWatt == 0 then
                dz.devices(Max).updateCustomSensor(PanelWatt)
            end
        end)
    end
}