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

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

Moderator: leecollings

Post Reply
PeterRozenveld
Posts: 38
Joined: Sunday 15 October 2023 18:44
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Must be not difficult! Adding two values??

Post 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
}
User avatar
jvdz
Posts: 2207
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

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

Post 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.
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
User avatar
waltervl
Posts: 5388
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

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

Post 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
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
PeterRozenveld
Posts: 38
Joined: Sunday 15 October 2023 18:44
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

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

Post 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
User avatar
waltervl
Posts: 5388
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

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

Post 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.....
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
PeterRozenveld
Posts: 38
Joined: Sunday 15 October 2023 18:44
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

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

Post 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
}
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests