Watermeter to virtual ( flow) sensor

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

Moderator: leecollings

Post Reply
Arav80
Posts: 1
Joined: Saturday 21 December 2024 13:55
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Watermeter to virtual ( flow) sensor

Post 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)
Attachments
domoticz-water.jpg
domoticz-water.jpg (279.4 KiB) Viewed 105 times
User avatar
waltervl
Posts: 5148
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: Watermeter to virtual ( flow) sensor

Post 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
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
Kedi
Posts: 536
Joined: Monday 20 March 2023 14:41
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Somewhere in NL
Contact:

Re: Watermeter to virtual ( flow) sensor

Post 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.
Logic will get you from A to B. Imagination will take you everywhere.
Kedi
Posts: 536
Joined: Monday 20 March 2023 14:41
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Somewhere in NL
Contact:

Re: Watermeter to virtual ( flow) sensor

Post 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. ;)
Logic will get you from A to B. Imagination will take you everywhere.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 1 guest