Page 1 of 1

Watermeter to virtual ( flow) sensor

Posted: Saturday 21 December 2024 14:23
by Arav80
Hi everyone,

Due to a massive waterleak in our stables I'm trying to figure out a proper way to avoid this in the future.

I just installed 'Watermeter Gateway' : a wifi device which 'sticks' on the watermeter and counts the pulses.
After some fiddling I got this working Image

I would like to get a notification( Pushover and email) when the water usage is above , say 8 liters/minute.
There is a virtual sensor in Domoticz which shows liters/min. Thats nice, so this virtual sensor could send a notification.

I tried this in blockly as I am a total noob in programming. Blockly is lacking some essential 'blocks' imo. I couldnt find a way to
get in working.
Oh well, ChatGPT to the resque : I asked to write a script which shows waterusage in l/min with Virtual sensor "waterverbruik"
(I created this sensor) with data from "Watermeter Value". This sensor gets data every 10 seconds and starts counting at 0:00 each day and resets after 24h.
However this didnt work either, the log shows an error in line 2 :

2024-12-21 14:15:00.771 Error: EventSystem: in lua-testwater: [string "-- Verkrijg de huidige waarde van de watermet..."]:2: attempt to index a nil value (global 'domoticz')



And tips/advice how to get this done? I'm clueless how to get this working :|


Code: Select all

-- Verkrijg de huidige waarde van de watermeter sensor
local watermeter = tonumber(domoticz.devices['Watermeter Value'].state)

-- Verkrijg de tijd van de vorige meting (bijvoorbeeld in een bestand of variabele)
local previous_time = tonumber(domoticz.variables['previous_time'].value)  -- of een andere methode om de tijd bij te houden
local previous_value = tonumber(domoticz.variables['previous_value'].value)

-- Bereken het verschil in tijd (bijvoorbeeld in minuten)
local current_time = os.time()
local elapsed_time = (current_time - previous_time) / 60  -- tijd in minuten

-- Bereken het verschil in waarde van de watermeter
local delta_value = watermeter - previous_value

-- Bereken het waterverbruik per minuut
local consumption_per_minute = delta_value / elapsed_time

-- Update de virtuele sensor voor liters per minuut
domoticz.devices['Watermeter per Minute'].updateConsumption(consumption_per_minute)

-- Sla de huidige waarde en tijd op voor de volgende meting
domoticz.variables['previous_time'].set(current_time)
domoticz.variables['previous_value'].set(watermeter)

Re: Watermeter to virtual ( flow) sensor

Posted: Saturday 21 December 2024 17:30
by waltervl
This is part of a Dzvents script, not LUA. Dzvents is build on Lua but has its own way of setting up the script. In eventmanager you can start an empty template.
https://wiki.domoticz.com/Events#dzVents_Interface


Dzvents documentation:
https://wiki.domoticz.com/DzVents:_next ... _scripting

Re: Watermeter to virtual ( flow) sensor

Posted: Saturday 21 December 2024 19:40
by Kedi
This is why ChatGPT still is in a beta (or even alpha) stage for programming.
1. It is part of a script and does not tell you that is only a part.
2. It does not tell you that you have to create 2 domoticz variables.
3. It could be much better done with data variables.
4. "updateConsumption" is not valid in dzVents.

So a simple script with 4 errors.

Re: Watermeter to virtual ( flow) sensor

Posted: Saturday 21 December 2024 19:50
by Kedi
This

Code: Select all

local watermeter = tonumber(domoticz.devices['Watermeter Value'].state)
should probably be

Code: Select all

local watermeter = tonumber(domoticz.devices['Watermeter Value'].flow)
or
local watermeter = tonumber(domoticz.devices['Watermeter Value'].counter)
Depending on the device state changed in flow or counter

That would be the fifth error. ;)