Hello,
I would like to calculate moving average of a value, but can't succeed.
The goal is to detect water leak depending on water consumption. I do have the following available values from water counter : number of liters used since midnight and instantaneous water flow. I think the last one is the clue, if the water flow is stable for a certain amount of time (tbd) and not equal to zéro, there is a leak.
This means that if the moving average equals the actual flow, the leak is suspected.
Can anybody give me some help about moving average calculation to solve m'y issue ?
Thanks
Moving average usage
Moderator: leecollings
-
- Posts: 612
- Joined: Sunday 01 November 2015 22:45
- Target OS: Raspberry Pi / ODroid
- Domoticz version: 2023.2
- Location: Twente
- Contact:
Re: Moving average usage
Tbh, there's nothing from me here, it's just CoPilot.
I inserted your question, preceded with dzVents coding and changed your last sentence to: Can you give me some help about moving average calculation to solve my issue ?
CoPilot answered:
I'd be happy to help you with calculating the moving average for your water consumption data!
The moving average is a commonly used technique for smoothing out data over a specific period. To detect a water leak based on stable water flow over time, you can calculate the moving average of the instantaneous water flow and compare it to the actual flow. If they are equal and not zero, it indicates a potential leak.
Here's an example of how to calculate the moving average in Lua, a language often used with Dzvents:
In this example:
period is the number of values over which you want to calculate the moving average (e.g., 10 minutes).
flow_values is an array storing the last period values of the instantaneous water flow.
calculate_moving_average(new_flow) is a function that adds the new flow value to the array, calculates the moving average, and returns it.
Replace current_flow with your actual instantaneous water flow value, and the script will print whether a leak is suspected based on the moving average.
Feel free to adjust the period value to suit your needs, and let me know if you need further assistance!
I inserted your question, preceded with dzVents coding and changed your last sentence to: Can you give me some help about moving average calculation to solve my issue ?
CoPilot answered:
I'd be happy to help you with calculating the moving average for your water consumption data!
The moving average is a commonly used technique for smoothing out data over a specific period. To detect a water leak based on stable water flow over time, you can calculate the moving average of the instantaneous water flow and compare it to the actual flow. If they are equal and not zero, it indicates a potential leak.
Here's an example of how to calculate the moving average in Lua, a language often used with Dzvents:
Code: Select all
-- Define the period for the moving average (e.g., 10 minutes)
local period = 10
-- Store the last 'period' values of water flow in an array
local flow_values = {}
-- Function to calculate the moving average
function calculate_moving_average(new_flow)
table.insert(flow_values, new_flow)
if #flow_values > period then
table.remove(flow_values, 1)
end
local sum = 0
for _, flow in ipairs(flow_values) do
sum = sum + flow
end
local moving_average = sum / #flow_values
return moving_average
end
-- Example of using the function
local current_flow = 5 -- Replace this with the actual instantaneous water flow value
local moving_average = calculate_moving_average(current_flow)
print("Current flow: " .. current_flow)
print("Moving average: " .. moving_average)
-- Check for leak
if moving_average == current_flow and current_flow ~= 0 then
print("Leak suspected!")
else
print("No leak.")
end
period is the number of values over which you want to calculate the moving average (e.g., 10 minutes).
flow_values is an array storing the last period values of the instantaneous water flow.
calculate_moving_average(new_flow) is a function that adds the new flow value to the array, calculates the moving average, and returns it.
Replace current_flow with your actual instantaneous water flow value, and the script will print whether a leak is suspected based on the moving average.
Feel free to adjust the period value to suit your needs, and let me know if you need further assistance!
Bugs bug me.
- waltervl
- Posts: 5846
- Joined: Monday 28 January 2019 18:48
- Target OS: Linux
- Domoticz version: 2024.7
- Location: NL
- Contact:
Re: Moving average usage
See this topic: viewtopic.php?p=322453#p322453
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
-
- Posts: 117
- Joined: Tuesday 06 February 2018 18:48
- Target OS: Raspberry Pi / ODroid
- Domoticz version: Beta rel
- Contact:
Re: Moving average usage
Thanks all,
I'll try this and keep you informed
I'll try this and keep you informed
-
- Posts: 660
- Joined: Thursday 10 November 2016 9:30
- Target OS: Raspberry Pi / ODroid
- Domoticz version:
- Contact:
Re: Moving average usage
Take care examples are just averaging values not taking into account the time they last.
As well i'm not into dzVent as i stayed lua but i presume historical data used comes from DB
If so you just get 5mn period samples that do not reflect true consumptions for devices with figures that may vary a lot in these 5mn slices.
To sum up, you cannot handle such device as a T or Hum that is quite stable. But that's what domoticz core does.
Had same issue with wattmeters and had to script discrete integration over 10mn slices using last_update.
As well i'm not into dzVent as i stayed lua but i presume historical data used comes from DB
If so you just get 5mn period samples that do not reflect true consumptions for devices with figures that may vary a lot in these 5mn slices.
To sum up, you cannot handle such device as a T or Hum that is quite stable. But that's what domoticz core does.
Had same issue with wattmeters and had to script discrete integration over 10mn slices using last_update.
-
- Posts: 117
- Joined: Tuesday 06 February 2018 18:48
- Target OS: Raspberry Pi / ODroid
- Domoticz version: Beta rel
- Contact:
Re: Moving average usage
Hello all,
Thank you very much for your inputs, I managed using your examples, the final version that looks working fine is below and can be reused and improved of course. I had to add a virtual device "Water_Leak" as I didn't succeed to send a telegram notification using DzVents (if any clue?)
Thank you very much for your inputs, I managed using your examples, the final version that looks working fine is below and can be reused and improved of course. I had to add a virtual device "Water_Leak" as I didn't succeed to send a telegram notification using DzVents (if any clue?)
Code: Select all
return {
on = {
timer = {'every 1 minutes'},
devices = {'Waterflow'}
},
logging =
{
level = domoticz.LOG_DEBUG,
marker = 'Leak detection',
},
data =
{
Waterflow_History = { history = true, maxMinutes = 5},
},
execute = function(domoticz)
local Waterflow = domoticz.devices('Waterflow').flow
-- add new data waterflow
domoticz.data.Waterflow_History.add(Waterflow)
-- average for waterflow
local Moving_avg_Flow = domoticz.data.Waterflow_History.avg()
domoticz.log('Water flow avg ' .. Moving_avg_Flow, domoticz.LOG_INFO)
if Moving_avg_Flow == Waterflow and Waterflow > 0 then
domoticz.devices('Water_Leak').switchOn().checkFirst()
end
if Moving_avg_Flow ~= Waterflow or Waterflow == 0 then
domoticz.devices('Water_Leak').switchOff().checkFirst()
end
end
}
-
- Posts: 117
- Joined: Tuesday 06 February 2018 18:48
- Target OS: Raspberry Pi / ODroid
- Domoticz version: Beta rel
- Contact:
Re: Moving average usage
Hello all,
As written above, it is working fine, except for low flows as the flowmeter device looks to be at detection limit, thus unstable.
As @lost was saying, is it possible to read the meter value (instantaneous cubic meter reading) and associated sample times to compute consumption over time instead of using flowmeter device ?
As written above, it is working fine, except for low flows as the flowmeter device looks to be at detection limit, thus unstable.
As @lost was saying, is it possible to read the meter value (instantaneous cubic meter reading) and associated sample times to compute consumption over time instead of using flowmeter device ?
Who is online
Users browsing this forum: No registered users and 1 guest