Page 1 of 1

Must be not difficult! Adding two values??

Posted: Sunday 19 January 2025 15:51
by PeterRozenveld
It's Sunday and my brain doesn't want anymore:

What I'm doing wrong ?
Adding two values...

Code: Select all

return
{
    on =
    {
        devices =
        {
            'Kookplaat_F1_(Rechts)',
            'Kookplaat_F3_(Links_Flexzone)',
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG,
        marker = 'Adding F1 en F2',
    },

    execute = function(dz, devices)

        targetDevice = dz.devices('Tot_Fornuis') -- define as virtual custom sensor

        -- local ph1 = dz.devices.('Kookplaat_F1_(Rechts)')
        local ph1 = dz.devices('Kookplaat_F1_(Rechts)') --  Fase meting L1 Kookplaat Rechts
        local ph2 = dz.devices('Kookplaat_F3_(Links_Flexzone)') -- Fase neting L2 Links_Flexzone
        local sumForn = ph1 + ph2
        dz.log('Sum of phase1 en phase2' .. sumForn, dz.LOG_DEBUG)

        targetDevice.updateCustomSensor(sumForn)
    end
}

Re: Must be not difficult! Adding two values??

Posted: Sunday 19 January 2025 16:05
by jvdz
What isn't working or what does the output show? Could be they are strings in which case you need to use the
tonumber() function around them to be able to add them up.

Re: Must be not difficult! Adding two values??

Posted: Sunday 19 January 2025 16:14
by waltervl
You forgot to define the value for ph1 and ph2. If your device is a Watt meter then it should be something like

Code: Select all

local ph1 = dz.devices('Kookplaat_F1_(Rechts)').actualWatt

Re: Must be not difficult! Adding two values??

Posted: Sunday 19 January 2025 21:28
by PeterRozenveld
update Usage I think is wrong somewhere!

Code: Select all

return
{
    on =
    {
        devices =
        {
            'Kookplaat_F1_(Rechts)',
            'Kookplaat_F3_(Links_Flexzone)',
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG,
        marker = 'Adding F1 en F2',
    },

    execute = function(dz, devices)

        targetDevice = dz.devices('Tot_Fornuis').Usage -- define as virtual custom sensor

        -- local ph1 = dz.devices.('Kookplaat_F1_(Rechts)')
        local ph1 = dz.devices('Kookplaat_F1_(Rechts)').actualWatt --  Fase meting L1 Kookplaat Rechts
        local ph2 = dz.devices('Kookplaat_F3_(Links_Flexzone)').actualWatt -- Fase neting L2 Links_Flexzone
        local sumForn = ph1 + ph2
        dz.log('Sum of phase1 en phase2' .. sumForn, dz.LOG_DEBUG)

        targetDevice.updateUsage(sumForn)
    end
}
results this:

Code: Select all

2025-01-19 21:23:32.787 Error: dzVents: Error: (3.1.8) Adding F1 en F2: An error occurred when calling event handler 11_Afzkap_SUm
2025-01-19 21:23:32.787 Error: dzVents: Error: (3.1.8) Adding F1 en F2: ...ticz/scripts/dzVents/generated_scripts/11_Afzkap_SUm.lua:29: attempt to index a nil value (global 'targetDevice') 
I do notice, still have a lot to learn
Peter

Re: Must be not difficult! Adding two values??

Posted: Sunday 19 January 2025 23:06
by waltervl
Change targetDevice = dz.devices('Tot_Fornuis').Usage
In

Code: Select all

targetDevice = dz.devices('Tot_Fornuis')
But look in the dzvents wiki per device type what variables are available to see what you want to calculate.....

Re: Must be not difficult! Adding two values??  [Solved]

Posted: Monday 20 January 2025 8:56
by PeterRozenveld
Thanks, and in particular : waltervl for the right and fine support.
Herewith the working code:
Peter Rozenveld.

Code: Select all

return
{
    on =
    {
        devices =
        {
            'Kookplaat_F1_(Rechts)',
            'Kookplaat_F3_(Links_Flexzone)',
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG,
        marker = 'Adding F1 en F2',
    },

    execute = function(dz, devices)

        targetDevice = dz.devices('Tot_Fornuis') -- define as virtual custom sensor

        local ph1 = dz.devices('Kookplaat_F1_(Rechts)').actualWatt --  Fase meting L1 Kookplaat Rechts
        local ph2 = dz.devices('Kookplaat_F3_(Links_Flexzone)').actualWatt -- Fase meting L2 Links_Flexzone
        local sumForn = ph1 + ph2
        dz.log('Sum of phase1 en phase2' .. sumForn, dz.LOG_DEBUG)

        targetDevice.updateEnergy(sumForn)
    end
}