Script subtracting measurements from two water meters

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

Moderator: leecollings

FlyingDomotic
Posts: 356
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: Sunday 01 September 2024 21:41 What do you mean specifically? What should I change in the script to implement your suggestion?
Create a water counter, name it "Test" and ceate a dzVents script containing:

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
}
willemd
Posts: 649
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: Sunday 01 September 2024 21:41
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 
I assume you get the above output multiple times per day and each time the Total Difference is slightly higher, the resultCounter is slightly higher but resultCounterToday remains zero? (ref. my previous explanation that counterToday is not updated during the script but will be visible at the next run)

I have no idea why this does not work. The resultCounter is updated correctly and therefore Domoticz itself should calculate and show counterToday value correctly. I do this all the time in my installation.

The only strange thing I see in your first post is that the total value is shown in m3 while the daily total is show with unit Liter (even if it is 0).
I assume you are multiplying the value with the divider of the resultcounter during the update? It was mentioned before and otherwise the total also would not be correct.

Try making a second resultcounter and update that one in the same way. Maybe somehow something is wrong with the device.
Maciek90
Posts: 29
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 »

Create a water counter, name it "Test" and ceate a dzVents script containing:
Just this script or should I add something else? I get these errors in the logs when I try to do this.

Code: Select all

 2024-09-02 12:05:30.577 Error: dzVents: An error occurred when calling event handler Woda chtgpt licznik dzienny
2024-09-02 12:05:30.577 Error: dzVents: ...zVents/generated_scripts/Woda chtgpt licznik dzienny.lua:4: attempt to call a nil value (field 'Devices')
2024-09-02 12:05:30.674 Error: dzVents: An error occurred when calling event handler Woda chtgpt licznik dzienny
I assume you get the above output multiple times per day and each time the Total Difference is slightly higher, the resultCounter is slightly higher but resultCounterToday remains zero? (ref. my previous explanation that counterToday is not updated during the script but will be visible at the next run)
Yes.
Can you show me your script and the devices it supports? I'll compare and maybe I'll spot some error in my script.
FlyingDomotic
Posts: 356
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 »

Sorry, I made a mistake written device with an uppercase.

Try to cut & paste:

Code: Select all

return {
	on = {devices = {'Water', 'Garden'}},
	execute = function(domoticz, device)
	    print("Water: "..tostring(domoticz.devices("Water").counter))
	    print("Garden: "..tostring(domoticz.devices("Garden").counter))
        domoticz.devices("TestWater").updateCounter(domoticz.devices("Water").counter - domoticz.devices("Garden").counter)
	end
}
Maciek90
Posts: 29
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 get logs like below and TestWater meter still shows daily usage of 0 liters. Maybe the problem is the type of meter I am using? I select from the list counter and then click edit and change the type to water

Code: Select all

 2024-09-03 00:35:51.519 dzVents: Handling events for: "Garden", value: "18446744073709548527"
2024-09-03 00:35:51.520 dzVents: ------ Start internal script: Water: Device: "Garden (Wodomierz)", Index: 1146
2024-09-03 00:35:51.520 dzVents: Water: 1497.201
2024-09-03 00:35:51.520 dzVents: Garden: 339.507
2024-09-03 00:35:51.520 dzVents: ------ Finished Water 
FlyingDomotic
Posts: 356
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 »

Oups, I forgot an amazing thing with counters. Returned data is divided by divisor (for water, default is 100), but stored as is. It means that you must multiply data by divisor before sending it (as Domoticz will divide it by divisor).

If not using default value (100 for water), change 100 in the following script by right divisor

Code: Select all

return {
	on = {devices = {'Water', 'Garden'}},
	execute = function(domoticz, device)
        	domoticz.devices("TestWater").updateCounter((domoticz.devices("Water").counter - domoticz.devices("Garden").counter)*100)
	end
}
Maciek90
Posts: 29
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 seems to work (thank you very much FlyingDomotic). I have one more question. Why do I get the result in liters and not m3? Is there any way to change this?
Attachments
Bez tytułu2.png
Bez tytułu2.png (10.36 KiB) Viewed 742 times
FlyingDomotic
Posts: 356
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 »

Differences comes from different counter types: Water and Garden are "(incremental) counters" while Test3 is "RFXmeter counter". If you wish having the same format, changing Test3 to "Counter" (not incremental) may help (I didn't test it).
Maciek90
Posts: 29
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 »

This doesn't help, because the RFXmeter counter is a counter selected from a list. As in the screenshot below.
Attachments
Bez tytułu.png
Bez tytułu.png (8.56 KiB) Viewed 710 times
FlyingDomotic
Posts: 356
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: Saturday 07 September 2024 20:37 This doesn't help, because the RFXmeter counter is a counter selected from a list. As in the screenshot below.
What's the issue? Just create d dummy device with "Counter type", select it in "Utility" tab, change change type to "water" and validate.
Maciek90
Posts: 29
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 know that. I created my test3 counter exactly as you wrote, but it shows me liters, not m3. I asked if it is possible to somehow change liters to m3 so that in each counter I have m3
User avatar
waltervl
Posts: 5851
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 »

You can also use this counter and set it to custom and use m3 as value. A normal water counter will always show liter in daily use and m3 in totals
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: 29
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 »

Thank you very much for you help. It works!!!
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest