Dzevent script and updating sensor not working correctly.

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

Moderator: leecollings

Post Reply
jslegers
Posts: 56
Joined: Tuesday 23 February 2016 9:14
Target OS: Raspberry Pi / ODroid
Domoticz version: 2026.2
Location: NL
Contact:

Dzevent script and updating sensor not working correctly.

Post by jslegers »

Hi,

I am busy with a Solar used energy script. Script is doing correctly its job but I am still struggling with updating a Virtual Sensor (Electric Instant and Counter)

Below is my script :

Code: Select all

return {
    on = {
        timer = {
            'at 23:50'
        }
    },
    logging = {
        level = domoticz.LOG_INFO,
        marker = 'CounterReader'
    },
    execute = function(domoticz, timer)

        local P1 = domoticz.devices(1)
        local GoodWe = domoticz.devices(50)
        local dailyUsage = domoticz.devices(62)

        if not P1 then
            domoticz.log('Device with IDX 1 not found!', domoticz.LOG_ERROR)
            return
        elseif not GoodWe then
            domoticz.log('Device with IDX 50 not found!', domoticz.LOG_ERROR)
            return
        elseif not dailyUsage then
            domoticz.log('Dummy device IDX 62 not found!', domoticz.LOG_ERROR)
            return
        end

        local deliveryToday = P1.counterDeliveredToday
        local generatedToday = GoodWe.counterToday
        local resultToday = math.max(0, generatedToday - deliveryToday)
        
        dailyUsage.updateElectricity(0, resultToday * 1000)

        domoticz.log('CounterDeliveredToday IDX 1: ' .. tostring(deliveryToday) .. ' kWh', domoticz.LOG_INFO)
        domoticz.log('CounterToday IDX 50: ' .. tostring(generatedToday) .. ' kWh', domoticz.LOG_INFO)
        domoticz.log('Result: ' .. tostring(resultToday) .. ' kWh', domoticz.LOG_INFO)

    end
}
As you can see the script is only run once in the evening just before a new day. In the graph I see the following :

Image

The first bar is from monday 9.946 kWh second graph from yesterday should be 7.711 kWh but it shows like a negative 2.235 kWh. Looks like it does 7.711 - 9.946 then you will get - 2.235.

If I look at the counter at the top of the graphs I see the the total of yesterday :

Image

What am I doing wrong?
User avatar
waltervl
Posts: 7020
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2026.3
Location: NL
Contact:

Re: Dzevent script and updating sensor not working correctly.

Post by waltervl »

This device needs a running counter. So feeding it with a daily value will not work as it resets to 0 at midnight resulting in a negative value.
You need to supply the counter with totals from P1 and Goodwe (eg. P1.usage and Goodwe.usage)

The Electric Instant and Counter device will then do the daily calculation for you in its reports.
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
jslegers
Posts: 56
Joined: Tuesday 23 February 2016 9:14
Target OS: Raspberry Pi / ODroid
Domoticz version: 2026.2
Location: NL
Contact:

Re: Dzevent script and updating sensor not working correctly.

Post by jslegers »

Hello Walter,

Thanks for the answer. So I need to use the counter or counter incremental?

Regards,
John
User avatar
waltervl
Posts: 7020
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2026.3
Location: NL
Contact:

Re: Dzevent script and updating sensor not working correctly.

Post by waltervl »

You need the current (Electric Instant and Counter) device to store the data in and change your script.
Try this (not tested), the first day this will be a huge value building up as it takes all the history. You can remove this value huge value from the month graph tomorrow....

Code: Select all

return {
    on = {
        devices = {
           50 -- Goodwe
        }
    },
    logging = {
        level = domoticz.LOG_INFO,
        marker = 'CounterReader'
    },
    execute = function(domoticz, timer)

        local P1 = domoticz.devices(1)
        local GoodWe = domoticz.devices(50)
        local dailyUsage = domoticz.devices(62)

        if not P1 then
            domoticz.log('Device with IDX 1 not found!', domoticz.LOG_ERROR)
            return
        elseif not GoodWe then
            domoticz.log('Device with IDX 50 not found!', domoticz.LOG_ERROR)
            return
        elseif not dailyUsage then
            domoticz.log('Dummy device IDX 62 not found!', domoticz.LOG_ERROR)
            return
        end

        local deliveryTotal = P1.usageDelivered      -- total delivered
        local generatedTotal = GoodWe.WhTotal        -- when the goodwe device is also a Electric instant and counter device, else change it
        local resultTotal = math.max(0, generatedTotal - deliveryTotal)
        
        dailyUsage.updateElectricity(0, resultTotal * 1000)

        -- domoticz.log('CounterDeliveredToday IDX 1: ' .. tostring(deliveryToday) .. ' kWh', domoticz.LOG_INFO)
        -- domoticz.log('CounterToday IDX 50: ' .. tostring(generatedToday) .. ' kWh', domoticz.LOG_INFO)
        domoticz.log('Result: ' .. tostring(resultTotal) .. ' kWh', domoticz.LOG_INFO)

    end
}
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
jslegers
Posts: 56
Joined: Tuesday 23 February 2016 9:14
Target OS: Raspberry Pi / ODroid
Domoticz version: 2026.2
Location: NL
Contact:

Re: Dzevent script and updating sensor not working correctly.

Post by jslegers »

Hello Walter,

Thanks for this I will test it.

John
jslegers
Posts: 56
Joined: Tuesday 23 February 2016 9:14
Target OS: Raspberry Pi / ODroid
Domoticz version: 2026.2
Location: NL
Contact:

Re: Dzevent script and updating sensor not working correctly.

Post by jslegers »

Ok I have done some testing again and still it is not working. I am going to stop with it. Tried a lot of things and also 3 AI Chat Engines and we still can't make in my eye's something this simple work. Would have been nice if Domoticz can create a simple bar graph for this. Maybe in a future update.
User avatar
waltervl
Posts: 7020
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2026.3
Location: NL
Contact:

Re: Dzevent script and updating sensor not working correctly.

Post by waltervl »

The script I gave you should have worked. Only perhaps some typing errors perhaps.....

Did you check the energy dashboard? https://wiki.domoticz.com/Energy_dashboard
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
Post Reply