Script subtracting measurements from two water meters

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

Moderator: leecollings

Maciek90
Posts: 19
Joined: Friday 05 March 2021 23:01
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Script subtracting measurements from two water meters

Post 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
}
Attachments
Bez tytułu.png
Bez tytułu.png (62.28 KiB) Viewed 978 times
User avatar
waltervl
Posts: 5148
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: Script subtracting measurements from two water meters

Post 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.
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
Maciek90
Posts: 19
Joined: Friday 05 March 2021 23:01
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: Script subtracting measurements from two water meters

Post 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
Attachments
Bez tytułu2.png
Bez tytułu2.png (63.22 KiB) Viewed 956 times
willemd
Posts: 621
Joined: Saturday 21 September 2019 17:55
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.1
Location: The Netherlands
Contact:

Re: Script subtracting measurements from two water meters

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

Re: Script subtracting measurements from two water meters

Post 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.
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
willemd
Posts: 621
Joined: Saturday 21 September 2019 17:55
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.1
Location: The Netherlands
Contact:

Re: Script subtracting measurements from two water meters

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

Re: Script subtracting measurements from two water meters

Post 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.
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
FlyingDomotic
Posts: 303
Joined: Saturday 27 February 2016 0:30
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Contact:

Re: Script subtracting measurements from two water meters

Post 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)
willemd
Posts: 621
Joined: Saturday 21 September 2019 17:55
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.1
Location: The Netherlands
Contact:

Re: Script subtracting measurements from two water meters

Post 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)
FlyingDomotic
Posts: 303
Joined: Saturday 27 February 2016 0:30
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Contact:

Re: Script subtracting measurements from two water meters

Post by FlyingDomotic »

Would it be possible to get a copy of your script?
Maciek90
Posts: 19
Joined: Friday 05 March 2021 23:01
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: Script subtracting measurements from two water meters

Post 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
Attachments
Bez tytułu.png
Bez tytułu.png (20.66 KiB) Viewed 807 times
FlyingDomotic
Posts: 303
Joined: Saturday 27 February 2016 0:30
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Contact:

Re: Script subtracting measurements from two water meters

Post 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.
Maciek90
Posts: 19
Joined: Friday 05 March 2021 23:01
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: Script subtracting measurements from two water meters

Post 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)
User avatar
habahabahaba
Posts: 192
Joined: Saturday 18 March 2023 14:44
Target OS: Windows
Domoticz version: 2024.4
Contact:

Re: Script subtracting measurements from two water meters

Post 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)
willemd
Posts: 621
Joined: Saturday 21 September 2019 17:55
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.1
Location: The Netherlands
Contact:

Re: Script subtracting measurements from two water meters

Post 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.
FlyingDomotic
Posts: 303
Joined: Saturday 27 February 2016 0:30
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Contact:

Re: Script subtracting measurements from two water meters

Post by FlyingDomotic »

Please *DONT* use daily counters, but just (total) counters, in order for values not going back to zero each morning at midnight!
Maciek90
Posts: 19
Joined: Friday 05 March 2021 23:01
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: Script subtracting measurements from two water meters

Post 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
FlyingDomotic
Posts: 303
Joined: Saturday 27 February 2016 0:30
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Contact:

Re: Script subtracting measurements from two water meters

Post 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...
willemd
Posts: 621
Joined: Saturday 21 September 2019 17:55
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.1
Location: The Netherlands
Contact:

Re: Script subtracting measurements from two water meters

Post 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.
Maciek90
Posts: 19
Joined: Friday 05 March 2021 23:01
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: Script subtracting measurements from two water meters

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

Who is online

Users browsing this forum: No registered users and 1 guest