Error Calculating flow from total usage device  [Solved]

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

Moderator: leecollings

Post Reply
dthompson
Posts: 27
Joined: Tuesday 07 June 2016 21:47
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Error Calculating flow from total usage device

Post by dthompson »

I'm trying to create a virtual point that will show the actual usage of water every 5 minutes from my water meter which is total consumed.

But, i keep getting this error no matter what i attempt to reference for the device value:

2026-08-16 18:14:00.473 Error: dzVents: An error occurred when calling event handler Water usage
2026-08-16 18:14:00.473 Error: dzVents: ...moticz/scripts/dzVents/generated_scripts/Water usage.lua:12: attempt to index a nil value (global 'device')

Here's the code i'm using:

Code: Select all

return {
    on = {
        timer = {
            'every 1 minutes' -- This is your time trigger
        },
        devices = {
            'Total water used' -- CHANGE THIS to your actual device name
        }
    },
    execute = function(domoticz, timer)
        -- 1. Get the current reading
        local currentFlow = device.sensorValue -- Use .sensorValue, .flow, or .state depending on device type
        
        -- 2. Retrieve the last saved reading
        local lastFlow = domoticz.variables('LastValue').sensorValue
        
        -- 3. Calculate the difference
        local difference = currentFlow - lastFlow
        
        -- 4. Log the results to the Domoticz log
        domoticz.log('Current Flow: ' .. currentFlow .. ' L/m', domoticz.LOG_INFO)
        domoticz.log('Previous Flow: ' .. lastFlow .. ' L/m', domoticz.LOG_INFO)
        domoticz.log('Calculated Difference: ' .. difference .. ' L/m', domoticz.LOG_INFO)
        
        -- 5. OPTIONAL: Update a dummy device with the difference
        -- domoticz.devices('Flow Difference Device').updateCustomSensor(difference)
        
        -- 6. Update the persistent variable for the next run
        domoticz.variables('TotalValue').set = currentFlow
    end
}
And, here are the devices i'm using:

223 MQTTmapper powerpi/SCMplus/71280747 1 Total water used General Custom Sensor 101119 Gal - - 2026-08-16 18:20:16
224 Virtual sensors 00082224 1 Water usage General Custom Sensor 0 Gal - - 2026-08-15 21:24:00

The actual water meter is being read via an usb SDR dongle on a RPI 3b running rtl_433 and using MQTT to pass the value to Domoticz thru the MQTTmapper.

Domoticz server:
OS: Raspberry PI OS Bookworm - fully updated
Domoticz : 2026.2

So, what am i doing incorrectly? I feel like i've tried every combination of value, curentvalue, counter, etc. and nothing seems to work.

Thanks,
Darryl
User avatar
waltervl
Posts: 7016
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2026.3
Location: NL
Contact:

Re: Error Calculating flow from total usage device

Post by waltervl »

Line 12 is

Code: Select all

local currentFlow = device.sensorValue
But there is no device as in the execute a timer is given ( execute = function(domoticz, timer) ).

So change the execute (change 'timer' into 'item') and check if item is a timer or a device trigger.
Or change line 12 into

Code: Select all

local currentFlow = domoticz.devices('Total water used').sensorValue
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
dthompson
Posts: 27
Joined: Tuesday 07 June 2016 21:47
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Error Calculating flow from total usage device

Post by dthompson »

So, having this in the beginning doesn't define the device?

},
devices = {
'Total water used' -- CHANGE THIS to your actual device name
}


This is just after the timer = declaration.

Thanks ,
Darryl
dthompson
Posts: 27
Joined: Tuesday 07 June 2016 21:47
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Error Calculating flow from total usage device

Post by dthompson »

Ok, so i modified my code as suggested and received this error:

2026-08-17 17:32:00.822 Error: dzVents: ...moticz/scripts/dzVents/generated_scripts/Water usage.lua:12: attempt to index a nil value (global 'dz')

Here's my new code:

return {
on = {
timer = {
'every 1 minutes' -- This is your time trigger
}
},
execute = function(domoticz, timer)
-- 1. Get the current reading
local currentFlow = domoticz.devices('Total water used').sensorValue

-- 2. Retrieve the last saved reading
local lastFlow = dz.variables('LastValue').state

-- 3. Calculate the difference
local difference = currentFlow - lastFlow

-- 4. Log the results to the Domoticz log
domoticz.log('Current Flow: ' .. currentFlow .. ' L/m', domoticz.LOG_INFO)
domoticz.log('Previous Flow: ' .. lastFlow .. ' L/m', domoticz.LOG_INFO)
domoticz.log('Calculated Difference: ' .. difference .. ' L/m', domoticz.LOG_INFO)

-- 5. OPTIONAL: Update a dummy device with the difference
domoticz.devices('Water usage').updateCustomSensor(difference)

-- 6. Update the persistent variable for the next run
domoticz.variables('TotalValue').set = currentFlow
end
}
dthompson
Posts: 27
Joined: Tuesday 07 June 2016 21:47
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Error Calculating flow from total usage device

Post by dthompson »

I think i corrected the code for that error. Thank you for walking me thru this.

But, now i have this error:

2026-08-17 17:35:00.934 Error: dzVents: ...moticz/scripts/dzVents/generated_scripts/Water usage.lua:15: attempt to perform arithmetic on a nil value (local 'lastFlow')

And, this new code:

return {
on = {
timer = {
'every 1 minutes' -- This is your time trigger
}
},
execute = function(domoticz, timer)
-- 1. Get the current reading
local currentFlow = domoticz.devices('Total water used').sensorValue

-- 2. Retrieve the last saved reading
local lastFlow = domoticz.variables('LastValue').state

-- 3. Calculate the difference
local difference = currentFlow - lastFlow

-- 4. Log the results to the Domoticz log
domoticz.log('Current Flow: ' .. currentFlow .. ' L/m', domoticz.LOG_INFO)
domoticz.log('Previous Flow: ' .. lastFlow .. ' L/m', domoticz.LOG_INFO)
domoticz.log('Calculated Difference: ' .. difference .. ' L/m', domoticz.LOG_INFO)

-- 5. OPTIONAL: Update a dummy device with the difference
domoticz.devices('Water usage').updateCustomSensor(difference)

-- 6. Update the persistent variable for the next run
domoticz.variables('TotalValue').set = currentFlow
end
}

Thanks,
Darryl
dthompson
Posts: 27
Joined: Tuesday 07 June 2016 21:47
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Error Calculating flow from total usage device  [Solved]

Post by dthompson »

I think i finally got it to work. Thank you for pointing me in the right direction. I'm no longer getting any errors, and it is calculating and passing the results to the virtual point i created.

Here's the final code:

return {
on = {
timer = {
'every 5 minutes' -- This is your time trigger
}
},
execute = function(domoticz, timer)
-- 1. Get the current reading
local currentFlow = domoticz.devices('Total water used').sensorValue

-- 2. Retrieve the last saved reading
local lastFlow = domoticz.variables('LastValue').value

-- 3. Calculate the difference
local difference = currentFlow - lastFlow

-- 4. Log the results to the Domoticz log
domoticz.log('Current Flow: ' .. currentFlow .. ' L/m', domoticz.LOG_INFO)
domoticz.log('Previous Flow: ' .. lastFlow .. ' L/m', domoticz.LOG_INFO)
domoticz.log('Calculated Difference: ' .. difference .. ' L/m', domoticz.LOG_INFO)

-- 5. OPTIONAL: Update a dummy device with the difference
domoticz.devices('Water usage').updateCustomSensor(difference)

-- 6. Update the persistent variable for the next run
domoticz.variables('LastValue').set(currentFlow)
end
}

Thank you again!!
Darryl
Post Reply