Hi,
i want to create a counter, wich starts from a specific date (1-10-2022). i have solar since last week, so i want to see how much energy i get from the grid, and how much i deliver back to te grid since 1-10-2022, so i can see whats my nett usage/delivery, approaching the end of the contract year. i tried it with chatGPT, but after hours, i didnt get it to work.
i have a p1 - 3 phase, ande the p1 working in domoticz, already made 2 dummys rftxmeter couters for consume ande deliver energy,.
so can someone please help me?
counter for my energy since contract date for 'salderen'
Moderator: leecollings
-
- Posts: 4
- Joined: Sunday 12 February 2023 11:53
- Target OS: Linux
- Domoticz version: 2023.1
- Location: Huizen, Nederland
- Contact:
Re: counter for my energy since contract date for 'salderen'
i use a lua script
--[[
dzVents script dzVents script time
om P1 Smart Meter Elektriciteitswaarde te ontleden in afzonderlijke Meterstanden.
Houd er rekening mee dat de teller van vandaag aanvankelijk tot de volgende dag bij de GUI een verkeerde waarde zal weergeven.
Script gedownload van huizebruin.nl
www.huizebruin.nl/domoticz/slimme-meter ... z-met-lua/
29/08/2020-/-v1.10: verbeteringen in totaal verwerkt
Te gebruiken voor domoticz versie >= V4.11305
]] --
local fetchIntervalMins = 2 -- (Geheel) Minutenfrequentie van deze scriptuitvoering 1 = elke minuut, 10 = elke 10 minuten, enz.) Moet een van (1,2,3,4,5,6,10,12,15,20,30) zijn.
local ScriptVersion = '1.10' -- domoticz > V2020.1 / dzVents >= 2.4.28
return {
on = {
timer = { 'every ' .. fetchIntervalMins .. ' minutes' }
},
logging = {
level = domoticz.LOG_DEBUG, -- Maak commentaar op deze regel om de instelling van dzVents global logging te overschrijven
marker = 'Afzonderlijke Meterstanden '.. ScriptVersion
},
data = { lastP1 = { initial = {} }},
execute = function(dz, item)
-- Voeg apparaatnamen toe tussen aanhalingstekens of apparaat-idx zonder aanhalingstekens
local P1 = dz.devices(25) -- Electra, P1 Smart Meter device (idx or "name") (required)
--[[ Voer namen / idx in voor apparaten die je wilt onder deze commentaarregels
Deze apparaten moeten worden gemaakt als nieuwe incrementele tellers. Script kan verkeerde waarden opleveren
bij gebruik met bestaande die al waarden bevatten
De resterende regels kunnen worden verwijderd of becommentarieerd
]]--
local usageLow = dz.devices('Verbruik Laag') -- Metergebruik laag, virtueel apparaat, teller incrementeel
local usageHigh = dz.devices('Verbruik Hoog') -- Metergebruik Hoog, Virtueel apparaat, teller incrementeel
local returnLow = dz.devices('Teruglevering Laag') -- Meter Return Laag, Virtueel apparaat, teller incrementeel
local returnHigh = dz.devices('Teruglevering Hoog') -- Meter Return Hoog, Virtueel apparaat, teller incrementeel
local usagetotaal = dz.devices('Verbruik Totaal')
local returntotaal = dz.devices('Teruglevering Totaal')
local saldo = dz.devices('Saldo')
-- local metersaldo = dz.devices(59) -- Return - Verbruik
-- Onder deze regel zijn geent wijzigingen vereist ---
lastP1 = dz.data.lastP1
local function updateCounter(dv, value, previousValue )
if not(dv) then return end
if not(previousValue) then
dz.log("Geen eerdere gegevens voor " .. dv.name .. " nog; deze run overslaan",dz.LOG_DEBUG)
return
end
if (dv.counter ~=0 or dv.counter ~= nil) then
value = value - previousValue
end
dv.updateCounter(value)
dz.log("Increment " .. dv.name .. " with: " .. value,dz.LOG_DEBUG)
end
local function round(num, dec)
return ( math.floor( num * 10^dec ) / 10^dec )
end
local beginstand = 795
verbruik = P1.usage1 + P1.usage2
terug = P1.return1 + P1.return2
totaalover = verbruik - terug
local Saldo = round (((totaalover)/1000 - beginstand),1)
-- Update the device
updateCounter(usageLow, P1.usage1, lastP1.usage1)
updateCounter(usageHigh, P1.usage2, lastP1.usage2)
updateCounter(returnLow, P1.return1, lastP1.return1)
updateCounter(returnHigh, P1.return2, lastP1.return2)
updateCounter(usagetotaal, verbruik, lastP1.verbruik)
updateCounter(returntotaal, terug, lastP1.terug)
-- updateCounter(metersaldo, Saldo, lastP1.Saldo)
lastP1.usage1 = P1.usage1
lastP1.usage2 = P1.usage2
lastP1.return1 = P1.return1
lastP1.return2 = P1.return2
lastP1.verbruik = verbruik
lastP1.terug = terug
lastP1.Saldo = Saldo
saldo.updateCustomSensor(Saldo)
--metersaldo.updateCustomSensor(totaalover)
end
}
- jvdz
- Posts: 2269
- Joined: Tuesday 30 December 2014 19:25
- Target OS: Raspberry Pi / ODroid
- Domoticz version: 4.107
- Location: Netherlands
- Contact:
Re: counter for my energy since contract date for 'salderen'
This LUA script will get the start counters for the start date of the contract and the todays counter values (calculated), and then calculate the totals for the YTD since contract start.
It has an if at the start to run only one time per hour, but you can change that if you want to whatever frequency you like.
The output is currently shown in the Domoticz Console, so you need to change that to where you would like to store the value(s), which could be a counter or a text device for example.
Save it in domoticz/scripts/lua in a file called script_time_xyz.lua and it will be ran each minute.
It has an if at the start to run only one time per hour, but you can change that if you want to whatever frequency you like.
The output is currently shown in the Domoticz Console, so you need to change that to where you would like to store the value(s), which could be a counter or a text device for example.
Code: Select all
-- LUA to get P1 usage for the contract period
-- set this info
ContractStartDag = 19
ContractStartMaand = 7
P1idx = 375 -- IDX of P1 meter device
DomoUrl = "http://127.0.0.1:8080" -- URL of domoticz.
commandArray = {}
-- only run one time per hour at xx:01
if os.date('%M') == '01' then
json = require 'JSON'
pers = pers or {}
function GetDomData(url)
local sQuery = 'curl -s "' .. url .. '"'
-- print("sQuery="..sQuery)
local handle = assert(io.popen(sQuery))
local Web_Data = handle:read('*all')
handle:close()
if (Web_Data == '') then
print('P1 get data Error: Empty result from curl command')
return ''
end
return Web_Data
end
--Get values of previous 8 days
local t, jresponse, status, decoded_response, idx
-- Calculate start date info
CurJaar = tonumber(os.date('%Y'))
CurMaand = tonumber(os.date('%m'))
CurDag = tonumber(os.date('%d'))
ContractJaar = CurJaar
p1resultly = {}
-- Determine the start year of the contract.
-- if start year is last year then get also last years data to get the start counters
if (tonumber(CurMaand) < ContractStartMaand or (tonumber(CurMaand) == ContractStartMaand and tonumber(CurDag) < ContractStartDag)) then
ContractJaar = CurJaar - 1
-- P1 information last year year
t = DomoUrl .. '/json.htm?type=graph&sensor=counter&range=year&idx=' .. tostring(P1idx) .. '&year=' .. ContractJaar
jresponse = GetDomData(t)
decoded_response = json:decode(jresponse)
p1resultly = decoded_response['result']
for i = 1, #p1resultly do
record = p1resultly[i]
if type(record) == 'table' then
rdate = record['d']
rJaar = tonumber(rdate:sub(1, 4))
rMaand = tonumber(rdate:sub(6, 7))
rDag = tonumber(rdate:sub(9, 10))
-- Get counter info start contract date.
if (ContractJaar == rJaar and rMaand == ContractStartMaand and rDag == ContractStartDag) then
P1_StartCounterContractUsage = ((record['c1'] or 0) + (record['c3'] or 0))
P1_StartCounterContractReturn = ((record['c2'] or 0) + (record['c4'] or 0))
--print("Found last year")
end
end
end
end
-- P1 information this year
t = DomoUrl .. '/json.htm?type=graph&sensor=counter&range=year&idx=' .. tostring(P1idx)
jresponse = GetDomData(t)
--print(jresponse)
decoded_response = json:decode(jresponse)
p1result = decoded_response['result']
-- List all data
for i = 1, #p1result do
record = p1result[i]
if type(record) == 'table' then
rdate = record['d']
rJaar = tonumber(rdate:sub(1, 4))
rMaand = tonumber(rdate:sub(6, 7))
rDag = tonumber(rdate:sub(9, 10))
-- Get counter info start contract date when the contract started in the current year
if (ContractJaar == CurJaar and ContractJaar == rJaar and rMaand == ContractStartMaand and rDag == ContractStartDag) then
p1record = p1result[i]
P1_StartCounterContractUsage = ((p1record['c1'] or 0) + (p1record['c3'] or 0))
P1_StartCounterContractReturn = ((p1record['c2'] or 0) + (p1record['c4'] or 0))
end
-- Get counter info yesterday and add currentday info.
if (CurJaar == rJaar and CurMaand == rMaand and CurDag == rDag) then
-- Use yesterday's result and add day values to the daystart counters.
p1record = p1result[i - 1]
P1_CurrentCounterUsage = ((p1record['c1'] or 0) + (p1record['c3'] or 0) + (p1record['v'] or 0) + (p1record['v2'] or 0) + (record['v'] or 0) + (record['v2'] or 0))
P1_CurrentCounterReturn = ((p1record['c2'] or 0) + (p1record['c4'] or 0) + (p1record['r1'] or 0) + (p1record['r2'] or 0) + (record['r1'] or 0) + (record['r2'] or 0))
end
end
end
P1_StartCounterContractUsage = (P1_StartCounterContractUsage or 0)
P1_StartCounterContractReturn = (P1_StartCounterContractReturn or 0)
P1_CurrentCounterUsage = (P1_CurrentCounterUsage or 0)
P1_CurrentCounterReturn = (P1_CurrentCounterReturn or 0)
print('Get P1 Contract start P1:' .. (ContractJaar or '?') .. '/' .. (ContractStartMaand or '?') .. '/' .. (ContractStartDag or '?'))
print('P1 contract verbruik is ' .. P1_CurrentCounterUsage .. ' - ' .. P1_StartCounterContractUsage .. ' = ' .. (P1_CurrentCounterUsage - P1_StartCounterContractUsage) .. 'kWh')
print('P1 contract levering is ' .. P1_CurrentCounterReturn .. ' - ' .. P1_StartCounterContractReturn .. ' = ' .. (P1_CurrentCounterReturn - P1_StartCounterContractReturn) .. 'kWh')
print('P1 contract Netto: ' .. ((P1_CurrentCounterUsage - P1_StartCounterContractUsage) - (P1_CurrentCounterReturn - P1_StartCounterContractReturn)) .. 'kWh')
end
return commandArray
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
-
- Posts: 75
- Joined: Sunday 30 November 2014 8:41
- Target OS: Raspberry Pi / ODroid
- Domoticz version:
- Location: Hoorn, Netherlands
- Contact:
Re: counter for my energy since contract date for 'salderen'
@hwnkkroon, thanks! I already saw that script, but didn't match for my situation.
@jdvz, thanks! It looks like what I'm looking for. But I can't get te output in de console (with the lua command in the lua script directory. So ik need to figure it out a bit more. But I think it's the goos direction!
@jdvz, thanks! It looks like what I'm looking for. But I can't get te output in de console (with the lua command in the lua script directory. So ik need to figure it out a bit more. But I think it's the goos direction!
- jvdz
- Posts: 2269
- Joined: Tuesday 30 December 2014 19:25
- Target OS: Raspberry Pi / ODroid
- Domoticz version: 4.107
- Location: Netherlands
- Contact:
Re: counter for my energy since contract date for 'salderen'
Just use the found numbers to update your dummy counters. 
Let me know when you need help with that....

Let me know when you need help with that....
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
-
- Posts: 75
- Joined: Sunday 30 November 2014 8:41
- Target OS: Raspberry Pi / ODroid
- Domoticz version:
- Location: Hoorn, Netherlands
- Contact:
Re: counter for my energy since contract date for 'salderen'
Hi,
Yes I need your help with it
Yes I need your help with it
- jvdz
- Posts: 2269
- Joined: Tuesday 30 December 2014 19:25
- Target OS: Raspberry Pi / ODroid
- Domoticz version: 4.107
- Location: Netherlands
- Contact:
Re: counter for my energy since contract date for 'salderen'
Ok..so how do you want it to be displayed?
Doubt you can use regular energy devices as you need to reset the counter to 0 at contract start date... So what exactly do you have in mind?
A simple text device would be pretty easy both no graphs or anything...just the current info.
...and is the script already working for you displaying the data in the domoticz log?
Doubt you can use regular energy devices as you need to reset the counter to 0 at contract start date... So what exactly do you have in mind?
A simple text device would be pretty easy both no graphs or anything...just the current info.
...and is the script already working for you displaying the data in the domoticz log?
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
-
- Posts: 75
- Joined: Sunday 30 November 2014 8:41
- Target OS: Raspberry Pi / ODroid
- Domoticz version:
- Location: Hoorn, Netherlands
- Contact:
Re: counter for my energy since contract date for 'salderen'
Nope. I haven't seen any data yet in the logs...
I think a text is good for now. (maybe it can be displayed in a dial in Dashticz).
I think a text is good for now. (maybe it can be displayed in a dial in Dashticz).
- jvdz
- Posts: 2269
- Joined: Tuesday 30 December 2014 19:25
- Target OS: Raspberry Pi / ODroid
- Domoticz version: 4.107
- Location: Netherlands
- Contact:
Re: counter for my energy since contract date for 'salderen'
Not a dashticz user, so can't help with that, but:
I think you need to start with making the script work for you. Let's take this offline (pm or email) to get that script so some data first. Pm/ email me the following:
The actual script file you created as attachment and the location where it is stored on you rpi.
Also create a dummy text device and supply me with the idx and actual device name, so I can have a look at adding that.
I think you need to start with making the script work for you. Let's take this offline (pm or email) to get that script so some data first. Pm/ email me the following:
The actual script file you created as attachment and the location where it is stored on you rpi.
Also create a dummy text device and supply me with the idx and actual device name, so I can have a look at adding that.
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
Who is online
Users browsing this forum: No registered users and 1 guest