Page 1 of 2

Script subtracting measurements from two water meters

Posted: Wednesday 21 August 2024 12:56
by Maciek90
I have two water meters at home; one main for the whole house and garden, and a separate one just for the garden. I want to calculate the difference between the readings of the main water meter and the one for the garden. I tried to write something using ChatGPT, but it didn't quite work out well. I added the RFX Meter Counter device. The overall difference is counted correctly, but the daily difference is not updated, it is always 0 like on screen below. Could someone help improve the script below?

Code: Select all

return {
    active = true,
    on = {
        devices = { 'Water', 'Garden' }
    },
    logging = {
        level = domoticz.LOG_DEBUG,
        marker = "Water Counter Script"
    },
    execute = function(domoticz, device)

        local count1 = domoticz.devices('Water').counter
        local count2 = domoticz.devices('Garden').counter
        local resultCounter = domoticz.devices('test')

        local dailyCount1 = domoticz.devices('Water').counterToday
        local dailyCount2 = domoticz.devices('Garden').counterToday

        domoticz.log('Water: ' .. tostring(count1), domoticz.LOG_DEBUG)
        domoticz.log('Garden: ' .. tostring(count2), domoticz.LOG_DEBUG)
        domoticz.log('Daily Water: ' .. tostring(dailyCount1), domoticz.LOG_DEBUG)
        domoticz.log('Daily Garden: ' .. tostring(dailyCount2), domoticz.LOG_DEBUG)

        if count1 ~= nil and count2 ~= nil then
            local totalDifference = count1 - count2
            local dailyDifference = dailyCount1 - dailyCount2

            domoticz.log('Total difference: ' .. tostring(totalDifference), domoticz.LOG_DEBUG)
            domoticz.log('Daily difference: ' .. tostring(dailyDifference), domoticz.LOG_DEBUG)

            resultCounter.updateCounter(totalDifference)

        else
            domoticz.log('One device has nil value', domoticz.LOG_ERROR)
        end
    end
}

Re: Script subtracting measurements from two water meters

Posted: Wednesday 21 August 2024 20:06
by waltervl
The script seems ok. The if about nil values is not necessary but also cannot hurt. What does the log say? Do you see changing counter values? Does the test counter change during the day (now 1150.005 m3) as the daily value is calculated by Domoticz based on changing counter values.

Re: Script subtracting measurements from two water meters

Posted: Wednesday 21 August 2024 22:35
by Maciek90
Log looks good, like below

Code: Select all

2024-08-21 22:27:52.274 dzVents: Handling events for: "Garden", value: "18446744073709549768"
2024-08-21 22:27:52.274 dzVents: Water Counter Script: ------ Start internal script: Water counter: Device: "Garden (Water)", Index: 1146
2024-08-21 22:27:52.275 dzVents: Debug: Water Counter Script: Start script: Garden
2024-08-21 22:27:52.275 dzVents: Debug: Water Counter Script: Processing device-adapter for Water: Counter device adapter
2024-08-21 22:27:52.275 dzVents: Debug: Water Counter Script: Processing device-adapter for test: Counter device adapter
2024-08-21 22:27:52.276 dzVents: Debug: Water Counter Script: Water: 1488.783
2024-08-21 22:27:52.276 dzVents: Debug: Water Counter Script: Garden: 338.397
2024-08-21 22:27:52.276 dzVents: Debug: Water Counter Script: Daily Water: 0.704
2024-08-21 22:27:52.276 dzVents: Debug: Water Counter Script: Daily Garden: 0.069
2024-08-21 22:27:52.276 dzVents: Debug: Water Counter Script: Total difference: 1150.386
2024-08-21 22:27:52.276 dzVents: Debug: Water Counter Script: Daily difference: 0.635
2024-08-21 22:27:52.276 dzVents: Water Counter Script: ------ Finished Water counter
Counter change during the day. Now it looks like below. I don't know why the daily counter shows 0 liters all the time, even though it calculates daily consumption in the logs

Re: Script subtracting measurements from two water meters

Posted: Thursday 22 August 2024 11:53
by willemd
You are using two incremental counters to calculate the difference and you are then updating a normal counter "test" with that value.

So the normal counter will show only the difference between the two incremental counters and therefore will go up and down all the time during the day. The daily value as calculated by the nightly switchover routine will then be the difference between the minimum and the maximum value of the day on that normal counter. (which I would expect to be small but not zero, since the minimum of the day will be the lowest single use of water in the house and the maximum will be the largest single use of water in the house, so the difference might be a shower or toilet flush quantity minus a quick handwash quantity, so guess daily value is then 20 or 30 liters or so, 0.02 m3)

You have two options:
1) Also use an incremental counter for the "test" counter.
2) Use a normal counter but before you do an update first read the current value and add the new value to the current value, then update the counter with that new total (taking into account any divider set on that counter).

Re: Script subtracting measurements from two water meters

Posted: Thursday 22 August 2024 21:51
by waltervl
I don't agree with @willend. An incremental counter just works the same as a normal counter only instead of sending a new total counter you send an incremental. Domoticz will store the incremental added on the last total counter as a new total counter. So it should be perfectly normal to use a normal counter to store the difference. See also the logging from the script.

I can agree there could be an issue with the divider.

Re: Script subtracting measurements from two water meters

Posted: Friday 23 August 2024 12:14
by willemd
waltervl wrote: Thursday 22 August 2024 21:51 I don't agree with @willend. An incremental counter just works the same as a normal counter only instead of sending a new total counter you send an incremental. Domoticz will store the incremental added on the last total counter as a new total counter. So it should be perfectly normal to use a normal counter to store the difference. See also the logging from the script.

I can agree there could be an issue with the divider.
Yes you can store a difference on a normal counter, but my point was that the daily cutover routine will then not calculate the total difference of the day correctly. It will not add up all the differences of the day to make a total.

In the current code it will select the first value of the day and the last value of the day and subtract first from last to get a total day value, which therefore might well be zero, even if during the day larger values were entered onto the counter.

Re: Script subtracting measurements from two water meters

Posted: Saturday 24 August 2024 22:08
by waltervl
The new diff counter will only get total counter values, not daily counter values, so I do not see where this could go wrong.

Re: Script subtracting measurements from two water meters

Posted: Saturday 24 August 2024 23:34
by FlyingDomotic
As you've got a total, and partial consumption already counter in total, difference may not be negative (which causes problems to some Domoticz devices).

In this case, you may have a permanent difference creating a dummy water counter (NOT incrementing), managed by a small script, triggered by changes in both "Water" and "Garden", setting test(current total) = Water(current total) - garden(current total).

Something like (dzVents, not tested):

Code: Select all

return {
	on = {devices = {'Water', 'Garden'}},
	execute = function(domoticz, device)
        	domoticz.Devices("Test").updateCounter(domoticz.Devices("Water").counter - domoticz.Devices("Garden").counter)
	end
}
You should have all difference stats (5 minutes, daily, weekly, monthly, yearly)

Re: Script subtracting measurements from two water meters

Posted: Sunday 25 August 2024 13:25
by willemd
waltervl wrote: Saturday 24 August 2024 22:08 The new diff counter will only get total counter values, not daily counter values, so I do not see where this could go wrong.
I did an example for myself with 4 events and it turns out I was wrong and you were correct. Sorry.

So the script is correct but then the original question still remains why the icon shows 0 as daily increase even though updates take place.

What if the counterToday() value of that counter is printed into the log during the run of the script? Does that also show 0 all the time?

(note that updating a counter in a script and then trying to print out the new counterToday() will not work, the new value will only be correct on the next run of the script, but if the script is run multiple times per day it should still show an increase)

Re: Script subtracting measurements from two water meters

Posted: Sunday 25 August 2024 14:54
by FlyingDomotic
Would it be possible to get a copy of your script?

Re: Script subtracting measurements from two water meters

Posted: Sunday 25 August 2024 20:04
by Maciek90
I noticed something else strange. Sometimes the daily value of the test counter shows 1000 liters as in the graph below(Pią is last friday, Sob is last saturday). Any ideas what to correct in the script? I'm sorry, but I'm quite weak in dzVents

Re: Script subtracting measurements from two water meters

Posted: Sunday 25 August 2024 22:56
by FlyingDomotic
Depending on device type, it may be necessary to multiply data read by some coefficient. Here, that's probably 1000, as very often data is shown in m3, while counted in liter. Try to multiply "Water" and "Garden" by 1000 and see what's happening.

Re: Script subtracting measurements from two water meters

Posted: Monday 26 August 2024 13:40
by Maciek90
It doesn't help. The value in the logs has been multiplied, but the counter still shows 0
I multiplied in this part of the code. Is that correct?

Code: Select all

local totalDifference = count1 - count2
            local dailyDifference = dailyCount1*1000 - dailyCount2*1000

            domoticz.log('Total difference: ' .. tostring(totalDifference), domoticz.LOG_DEBUG)
            domoticz.log('Daily difference: ' .. tostring(dailyDifference), domoticz.LOG_DEBUG)

            resultCounter.updateCounter(totalDifference)

Re: Script subtracting measurements from two water meters

Posted: Monday 26 August 2024 15:36
by habahabahaba
What if you try to update it through API call?

Code: Select all

domoticz.openURL('http://127.0.0.1:8081/json.htm?type=command&param=udevice&idx=IDX_OF_TEST_COUNTER&nvalue=0&svalue='..totalDifference).afterSec(1)

Re: Script subtracting measurements from two water meters

Posted: Monday 26 August 2024 15:51
by willemd
Maciek90 wrote: Monday 26 August 2024 13:40 It doesn't help. The value in the logs has been multiplied, but the counter still shows 0
I multiplied in this part of the code. Is that correct?

Code: Select all

local totalDifference = count1 - count2
            local dailyDifference = dailyCount1*1000 - dailyCount2*1000

            domoticz.log('Total difference: ' .. tostring(totalDifference), domoticz.LOG_DEBUG)
            domoticz.log('Daily difference: ' .. tostring(dailyDifference), domoticz.LOG_DEBUG)

            resultCounter.updateCounter(totalDifference)
Did you try adding this to your code?
domoticz.log('resultCounterToday: ' .. (resultCounter.counterToday, domoticz.LOG_DEBUG)
domoticz.log('resultCounter : ' .. (resultCounter.counter, domoticz.LOG_DEBUG)
That might give some more info.

Re: Script subtracting measurements from two water meters

Posted: Monday 26 August 2024 18:31
by FlyingDomotic
Please *DONT* use daily counters, but just (total) counters, in order for values not going back to zero each morning at midnight!

Re: Script subtracting measurements from two water meters

Posted: Tuesday 27 August 2024 21:21
by Maciek90
Did you try adding this to your code?
domoticz.log('resultCounterToday: ' .. (resultCounter.counterToday, domoticz.LOG_DEBUG)
domoticz.log('resultCounter : ' .. (resultCounter.counter, domoticz.LOG_DEBUG)
That might give some more info.
This only causes errors in the logs
Please *DONT* use daily counters, but just (total) counters, in order for values not going back to zero each morning at midnight!
I just want the Test counter to reset to zero every day at midnight. Once again. I have two meters: Water and Garden. I would like the third meter, provisionally named Test, to calculate the difference between the Water and Garden meters, both total readings (this is currently working) and daily readings. Daily readings are correctly calculated in the logs, but are not visible in the Test device

Re: Script subtracting measurements from two water meters

Posted: Tuesday 27 August 2024 23:37
by FlyingDomotic
Maciek90 wrote: Tuesday 27 August 2024 21:21 I just want the Test counter to reset to zero every day at midnight. Once again. I have two meters: Water and Garden. I would like the third meter, provisionally named Test, to calculate the difference between the Water and Garden meters, both total readings (this is currently working) and daily readings. Daily readings are correctly calculated in the logs, but are not visible in the Test device
Domoticz counters have daily values reset to zero every morning at midnight. You don't have to do something special to get it.

However, not increasing continuously a counter makes amazing daily totals ... as you can see ;-)

Just try my proposal for a day or two...

Re: Script subtracting measurements from two water meters

Posted: Wednesday 28 August 2024 15:18
by willemd
Maciek90 wrote: Tuesday 27 August 2024 21:21
Did you try adding this to your code?
domoticz.log('resultCounterToday: ' .. (resultCounter.counterToday, domoticz.LOG_DEBUG)
domoticz.log('resultCounter : ' .. (resultCounter.counter, domoticz.LOG_DEBUG)
That might give some more info.
This only causes errors in the logs
Sorry, there is one left parenthesis too much. Please remove the "(" before the word resultCounter. Then it should work.

Re: Script subtracting measurements from two water meters

Posted: Sunday 01 September 2024 21:41
by Maciek90
Sorry, there is one left parenthesis too much. Please remove the "(" before the word resultCounter. Then it should work.
I did it and the logs showed something like this:

Code: Select all

2024-09-01 21:39:31.464 dzVents: Debug: Water Counter Script: Processing device-adapter for Water: Counter device adapter
2024-09-01 21:39:31.464 dzVents: Debug: Water Counter Script: Processing device-adapter for test: Counter device adapter
2024-09-01 21:39:31.464 dzVents: Debug: Water Counter Script: Water: 1497.183
2024-09-01 21:39:31.464 dzVents: Debug: Water Counter Script: Garden: 339.457
2024-09-01 21:39:31.464 dzVents: Debug: Water Counter Script: Daily Water: 0.518
2024-09-01 21:39:31.464 dzVents: Debug: Water Counter Script: Daily Garden: 0.069
2024-09-01 21:39:31.464 dzVents: Debug: Water Counter Script: Total difference: 1157.726
2024-09-01 21:39:31.464 dzVents: Debug: Water Counter Script: Daily difference: 0.449
2024-09-01 21:39:31.464 dzVents: Debug: Water Counter Script: resultCounterToday: 0
2024-09-01 21:39:31.464 dzVents: Debug: Water Counter Script: resultCounter : 1157.726 
Domoticz counters have daily values reset to zero every morning at midnight. You don't have to do something special to get it.

However, not increasing continuously a counter makes amazing daily totals ... as you can see ;-)

Just try my proposal for a day or two...
What do you mean specifically? What should I change in the script to implement your suggestion?