How to get daily used and delivered energy value

Topics (not sure which fora)
when not sure where to post, post here and mods will move it to right forum.

Moderators: leecollings, remb0

Post Reply
jpmd
Posts: 16
Joined: Tuesday 25 April 2023 19:46
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Netherlands
Contact:

How to get daily used and delivered energy value

Post by jpmd »

Does anyone know how to get the total used (electric) energy of a specific dag in the past.

The energy is saved in a utility device:
Utility device
Utility device
Schermafbeelding 2025-03-27 083454.jpg (14.01 KiB) Viewed 542 times
I like to get the used- an return energy value of a specific day of this figure:
Energy per day
Energy per day
Schermafbeelding 2025-03-27 083750.jpg (52.33 KiB) Viewed 542 times
Thanks in advance
manjh
Posts: 748
Joined: Saturday 27 February 2016 12:49
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: NL
Contact:

Re: How to get daily used and delivered energy value

Post by manjh »

I keep all meter readings on a daily basis. Every day at 23:59 I pick up all readings of electricity, gas, and PV, and append the values to a file.
Over time I have collected a lot of data, useful for further processing.

For example: I wrote a flow in Node Red which "predicts" annual usage, based on a sliding year.
I select the end-date, and the flow calculates what my usage would be if the energy provider were to calculate it on that day.

This is very useful to see the effects of energy saving: in my case I installed an airconditioning unit in October last year, also capable of heating. This is much more cost effective than gas heating.
With the Node Red flow, I can see the progress day-by-day.
Hans
User avatar
jvdz
Posts: 2328
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: How to get daily used and delivered energy value

Post by jvdz »

This is a lua script that can retrieve the info for an specified date.
Save the below code to getp1dayinfo.lua and you can use is as:
getp1dayinfo.lua :

Code: Select all

------------------------------------------------------------------------------
-- LUA to get P1 usage a specified day
------------------------------------------------------------------------------
-- Change to your enviroment -----------
local P1MeterIDX = 375        -- IDX of P1 meter device
local DomoUrl = "http://127.0.0.1:8080" -- URL of domoticz.
local json = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")()
--------------------------------------
local iDatum = arg[1] or os.date('%Y-%m-%d')
local iJaar = iDatum:sub(1,4)

-- Get data from Domoticz with Curl
local 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 P1 information for a whole year. Data starts with today lastyear.
local url = DomoUrl .. '/json.htm?type=command&param=graph&sensor=counter&idx=' .. tostring(P1MeterIDX) .. '&range=year&actyear=' .. iJaar
local jresponse = GetDomData(url)
local decoded_response = json:decode(jresponse)
local p1result = decoded_response['result']
-- List all data
local P1_Usage = '?'
local P1_Return = '?'
for i = 1, #p1result do
   local p1record = p1result[i]
   if type(p1record) == 'table' then
      -- Get the info when date is found
      if (iDatum == p1record['d']) then
         P1_Usage = tostring(((p1record['v1'] or 0) + (p1record['v2'] or 0)))
         P1_Return = tostring(((p1record['r1'] or 0) + (p1record['r2'] or 0)))
         break
      end
   end
end
--
print('P1 verbruik voor ' .. iDatum .. ' is ' .. P1_Usage .. 'kWh')
print('P1 teruglevering voor ' .. iDatum .. ' is ' .. P1_Return .. 'kWh')
Usage:

Code: Select all

~/domoticz $ lua getp1dayinfo.lua
P1 verbruik voor 2025-03-27 is 2.885kWh
P1 teruglevering voor 2025-03-27 is 1.257kWh

~/domoticz $ lua getp1dayinfo.lua 2025-02-28
P1 verbruik voor 2025-02-28 is 7.999kWh
P1 teruglevering voor 2025-02-28 is 16kWh

/domoticz $ lua getp1dayinfo.lua 2023-02-28
P1 verbruik voor 2023-02-28 is 8.308kWh
P1 teruglevering voor 2023-02-28 is 15.843kWh
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
jpmd
Posts: 16
Joined: Tuesday 25 April 2023 19:46
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Netherlands
Contact:

Re: How to get daily used and delivered energy value

Post by jpmd »

Wow thats great. Thanks.

But unfortunately Two issues still:
1/ I removed one slash in "local DomoUrl = "http://raspberrypi-1:/8080" -- URL of domoticz." Witch results in: "local DomoUrl = "http://raspberrypi-1:8080" -- URL of domoticz."

2/ The "local url" becomes "http://raspberrypi-1:8080/json.htm?type ... tyear=2025"
If I use that in the chroom-browser, it results in an nice json-response :)
BUT: The LUA-line "local jresponse = GetDomData(url)" gives a value for jresponse "<html><head><title>Unauthorized</title></head><body><h1>401 Unauthorized</h1></body></html".

Any idea why GetDomData(url) gets a 401 Unauthorized response?
User avatar
jvdz
Posts: 2328
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: How to get daily used and delivered energy value

Post by jvdz »

jpmd wrote: Thursday 27 March 2025 11:45 But unfortunately Two issues still:
1/ I removed one slash in "local DomoUrl = "http : //raspberrypi-1:/8080" -- URL of domoticz." Witch results in: "local DomoUrl = "http://raspberrypi-1:8080" -- URL of domoticz."
Don't understand what you mean. The posted script has: "http : //127.0.0.1:8080", which assumes the script runs on the server itself.
So just change that to what is required. ;)
jpmd wrote: Thursday 27 March 2025 11:45 2/ The "local url" becomes "http : //raspberrypi-1:8080/json.htm?type=command&param=graph&sensor=counter&idx=210&range=year&actyear=2025"
If I use that in the chroom-browser, it results in an nice json-response :)
BUT: The LUA-line "local jresponse = GetDomData(url)" gives a value for jresponse "<html><head><title>Unauthorized</title></head><body><h1>401 Unauthorized</h1></body></html".

Any idea why GetDomData(url) gets a 401 Unauthorized response?
Sure, This is a security issue and described in the Wiki: https://wiki.domoticz.com/Security#API_Protection

I would simply add the local LAN ip-range to the trusted networks so no UserId/Password is required: https://wiki.domoticz.com/Security#Trusted_Networks
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
jpmd
Posts: 16
Joined: Tuesday 25 April 2023 19:46
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Netherlands
Contact:

Re: How to get daily used and delivered energy value

Post by jpmd »

Tanks,
I used a value for Domourl "http://raspberrypi-1:8080" and the value for "Trusted Networks" was "localhost".
I had to change Domourl to '192.168.2.11' :( and changed "Trusted Networks" to "localhost;192.168.*"
After those changes the jresponse contains a proper JSON output.

Thanks for helping me :P
User avatar
jvdz
Posts: 2328
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: How to get daily used and delivered energy value

Post by jvdz »

Just stepping through your last reply:
jpmd wrote: Thursday 27 March 2025 15:33 I used a value for Domourl "http://raspberrypi-1:8080" and the value for "Trusted Networks" was "localhost".
That means it won't be working indeed as locahost = 127.0.0.1
jpmd wrote: Thursday 27 March 2025 15:33 I had to change Domourl to '192.168.2.11' :(
The Domoticz url shouldn't have to be changed when raspberrypi-1 resolves to 192.168.2.11 on the system you run this script on. so this is an DNS thing.
jpmd wrote: Thursday 27 March 2025 15:33 and changed "Trusted Networks" to "localhost;192.168.*"
Make sense, and all it needs is the addition of an /24 ip range like: localhost;192.168.2.*" assuming the client running the script is also in the 192.168.2.x network.

... but nice when the script works for you. :-)
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
User avatar
waltervl
Posts: 5846
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: How to get daily used and delivered energy value

Post by waltervl »

If it is just to know you can hover over the specific day and see the value:
Schermafbeelding 2025-03-27 233200.png
Schermafbeelding 2025-03-27 233200.png (44.74 KiB) Viewed 390 times
You can also download the excel or csv data from the graph from the hamburger menu, top right.

Code: Select all

DateTime	 Totaal verbruik Totaal Levering	Kosten
2025-02-27 00:00:00	20,826	-7,037	4,3118
2025-02-28 00:00:00	20,704	-8,693	3,7558
2025-03-01 00:00:00	31,6	-0,051	9,8654
2025-03-02 00:00:00	17,333	-15,863	0,4597
2025-03-03 00:00:00	17,075	-7,782	2,9059
2025-03-04 00:00:00	14,58	-9,834	1,4841
2025-03-05 00:00:00	10,948	-19,884	-2,7943
You can also go to the report page (device widget Log button - Report button on top right) and see the values per month per day per year.
Schermafbeelding 2025-03-27 233946.png
Schermafbeelding 2025-03-27 233946.png (40.4 KiB) Viewed 389 times
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
Post Reply

Who is online

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