Page 1 of 2

API script /http polling Powerfox Energy tracker

Posted: Sunday 23 January 2022 15:34
by Mirkoser
Hello Experts

i'm using Domoticz for several years now and also the dashticz dashboard with Ubuntu 20 on my Intel NUC. But what i missed all the years was a tracking of my power consumption.
Last year i installed my new CFOS powerbrain wallbox and together with this i also installed a Powerfox optical engerie tracker who communicates with the wallbox to realize photovoltaic surplus charging.
Now i want to read the powerfox data with domoticz...Powerfox offers a API interface what is described here https://www.powerfox.energy/wp-content/ ... en-API.pdf

Is anybody able to create a working plugin for that. Or is it realisable with the http polling(there a different polling addresses)? I'm noobish in programming so i need your help to make it work

Thanks Mirko

Re: API script /http polling Powerfox Energy tracker

Posted: Sunday 23 January 2022 17:51
by waltervl
The values of
https://backend.powerfox.energy/api/2.0/my/main/current
Could fill an electricity instant and counter device
https://www.domoticz.com/wiki/Dummy_for ... counter.29

There are several ways to get this filled. I would go for the DzVents script way but this could also be done with python plugin, python script, http Json etc.
The only thing I don't now is how to login.

Re: API script /http polling Powerfox Energy tracker

Posted: Monday 24 January 2022 21:04
by Mirkoser
Hi,
Thanks

Regarding the login.These are my personal credentials from powerfox email / password

The response I get

{"Outdated":false,"Watt":1347.0,"Timestamp":1643054398,"A_Plus":1305727.0,"A_Minus":181798.0}

Re: API script /http polling Powerfox Energy tracker

Posted: Tuesday 25 January 2022 10:35
by Mirkoser
Ok i added the virtual sensor IDX 1441 name Powerfox_Energy and started with a template for http in DzVents.

Now its not clear for me to put the informations from the http request ({"Outdated":false,"Watt":831.0,"Timestamp":1643100578,"A_Plus":1318663.0,"A_Minus":181798.0}) into the virtual counter ?

Code: Select all

return {
	on = {
		timer = {
			'every 1 minutes' -- just an example to trigger the request
		},
		httpResponses = {
			'https://mirkosercc%40googlemail.com:[email protected]/api/2.0/my/main/current' -- must match with the callback passed to the openURL command
		}
	},
	logging = {
		level = domoticz.LOG_INFO,
		marker = 'template',
	},
	execute = function(domoticz, item)

		if (item.isTimer) then
			domoticz.openURL({
				url = 'https://mirkosercc%40googlemail.com:[email protected]/api/2.0/my/main/current',
				method = 'GET',
				callback = 'https://mirkosercc%40googlemail.com:[email protected]/api/2.0/my/main/current', -- see httpResponses above.
			})
		end

		if (item.isHTTPResponse) then

			if (item.ok) then
				if (item.isJSON) then

					local someValue = item.json.someValue  -- just an example

					-- update some device in Domoticz
					domoticz.devices('myTextDevice').updateText(someValue)
				end
			else
				domoticz.log('There was a problem handling the request', domoticz.LOG_ERROR)
				domoticz.log(item, domoticz.LOG_ERROR)
			end

		end

	end
}

Re: API script /http polling Powerfox Energy tracker

Posted: Tuesday 25 January 2022 10:55
by waltervl
You are on the right way.
change line
callback = 'https... etc' to

Code: Select all

callback = 'Powerfoxtrigger'
and line
httpResponses = {
'https.... etc


to

Code: Select all

httpResponses = { 'Powerfoxtrigger' }
basis of a http dzvents script is below (see also documentation https://www.domoticz.com/wiki/DzVents:_ ... P_requests )

Code: Select all

return {
    on = {
    timer = {'every 5 seconds'},
    httpResponses = { 'trigger' }
    },
    execute = function(domoticz, item)
    if (item.isTimer) then
        domoticz.openURL({
            url = '...',
            callback = 'trigger'
        })
    end
    if (item.isHTTPResponse) then
        if (item.ok) then
            ...
        end
    end
    end
}

Re: API script /http polling Powerfox Energy tracker

Posted: Tuesday 25 January 2022 11:05
by waltervl
Additional:
to get the data in a Electricity (instand and counter) dummy device use

.updateElectricity(power, energy) eg domoticz.devices('myCounterDevice').updateElectricity(power, energy)
POWER = current power (Watt) = your json value for "Watt"
ENERGY = total cumulative energy in Watt-hours (Wh) = is your jsan value for "A_Plus"
See also
https://www.domoticz.com/wiki/DzVents:_ ... counter.29

What does "A_Minus" mean? Energy that is returned to the grid?

Re: API script /http polling Powerfox Energy tracker

Posted: Tuesday 25 January 2022 12:18
by Mirkoser
Wow thanks for your replies. I will test in the evening.

"What does "A_Minus" mean? Energy that is returned to the grid?" -> Yes the powerfox shows the current power in W , PLus : wh consumed from grid, Minus : sum of wh returned to grid
So the watts could be also negative (if the consumption of my home is less than the solar output)

Re: API script /http polling Powerfox Energy tracker

Posted: Tuesday 25 January 2022 13:50
by waltervl
OK, then perhaps better use a P1 Smart meter instead of a Electricity (instand and counter) dummy device
domoticz.devices('myCounterDevice').updateP1(usage1, usage2, return1, return2, cons, prod)

USAGE1;USAGE2;RETURN1;RETURN2;CONS;PROD
IDX = id of your device (This number can be found in the devices tab in the column "IDX")
USAGE1= total cumulative energy usage meter tariff 1 in Wh. your A_Plus
USAGE2= total cumulative energy usage meter tariff 2 in Wh. (in your case send always 0)
RETURN1= total cumulative energy return meter tariff 1 in Wh. your A_Minus
RETURN2= total cumulative energy return meter tariff 2 in Wh. (in your case send always 0)
CONS= actual usage power (Watt) your Watt
PROD= actual return power (Watt) (if your "Watt" value is negative, but send a positive value)

Re: API script /http polling Powerfox Energy tracker

Posted: Sunday 30 January 2022 10:20
by Mirkoser
Good Morning, i guess i made some steps further but the final hint is missing. This line i do not understand?

Code: Select all

local someValue = item.json.someValue  -- just an example


do i have to insert the json e.g this /json.htm?type=command&param=udevice&idx=1453&nvalue=0&svalue=USAGE1;USAGE2;RETURN1;RETURN2;CONS;PROD

But the editor says there is an error near to the question mark??


i get this error log
2022-01-30 09:30:00.242 Error: UpdateMultiMeter: Error converting sValue values! (IDX: 1453, sValue: 'nil;nil;nil;nil;nil;nil', dType: 250, sType: 1)

Code: Select all

return {
	on = {
		timer = {
			'every 20 seconds' -- just an example to trigger the request
		},
		httpResponses = { 'Powerfoxtrigger' } -- must match with the callback passed to the openURL command
		
	},
	logging = {
		level = domoticz.LOG_INFO,
		marker = 'Powerfox',
	},
	execute = function(domoticz, item)

		if (item.isTimer) then
			domoticz.openURL({
				url = 'https://mirkosercc%40googlemail.com:[email protected]/api/2.0/my/main/current',
				method = 'GET',
				callback = 'Powerfoxtrigger', -- see httpResponses above.
			})
		end

		if (item.isHTTPResponse) then

			if (item.ok) then
				if (item.isJSON) then

					local someValue = item.json.someValue  -- just an example

					-- update some device in Domoticz
					domoticz.devices('Stromverbrauch').updateP1(usage1, usage2, return1, return2, cons, prod)
					
                    IDX = '1453'
                    USAGE1= 'A_Plus'--total cumulative energy usage meter tariff 1 in Wh. your A_Plus
                    USAGE2= '0'--total cumulative energy usage meter tariff 2 in Wh. (in your case send always 0)
                    RETURN1= 'A_Minus'--total cumulative energy return meter tariff 1 in Wh. your A_Minus
                    RETURN2= '0'--total cumulative energy return meter tariff 2 in Wh. (in your case send always 0)
                    CONS= 'Watt'--actual usage power (Watt) your Watt
                    PROD= 'Watt'--actual return power (Watt) (if your "Watt" value is negative, but send a positive value)
					
				end
			else
				domoticz.log('There was a problem handling the request', domoticz.LOG_ERROR)
				domoticz.log(item, domoticz.LOG_ERROR)
			end

		end

	end
}

Re: API script /http polling Powerfox Energy tracker

Posted: Sunday 30 January 2022 11:10
by waltervl
You have the order wrong.
From the openurl command you get a JSON from your meter.
From that JSON you have to read all the attributes that you can send to domoticz with the .updateP1() function.

So for example
local USAGE1 = item.json.A_Plus
local RETURN1 = item.json.A_Minus
etc.
For Watt you need first a check if it is positive or negative and then set CONS and PROD with the correct values

Be aware that variables are case sensitive in DzVents so use USAGE1 or usage1 all the way.
And better add some extra logging so you can see what you get. First try to get all the variables out, send the to the log and then send them to the P1 device.

Re: API script /http polling Powerfox Energy tracker

Posted: Sunday 30 January 2022 11:39
by Mirkoser
Ok i will try to put the variables from the http to the log first. Step by step:) Sorry but I’m not so familiar with that kind of programming. In the Wiki I miss some concrete examples who would help me to understand

Re: API script /http polling Powerfox Energy tracker

Posted: Sunday 30 January 2022 12:28
by waltervl
In the DzVents example forum there are a lot of examples for reading a JSON file and send the values to Domoticz like viewtopic.php?f=72&t=23406&hilit=Item.json

Re: API script /http polling Powerfox Energy tracker

Posted: Sunday 30 January 2022 14:55
by Mirkoser
So almost finished, still need to adjust the unit. The Json value is Wh and i need to devide by 1000 to get the right values displayed in Domoticz P1 counter..

and i need an if for the negative value of watt...

Code: Select all

return {
	on = {
		timer = {
			'every minute' -- just an example to trigger the request
		},
		httpResponses = { 'Powerfoxtrigger' } -- must match with the callback passed to the openURL command
		
	},
	logging = {
		level = domoticz.LOG_INFO,
		marker = 'Powerfox',
	},
	execute = function(domoticz, item)

		if (item.isTimer) then
			domoticz.openURL({
				url = 'https://mirkosercc%40googlemail.com:[email protected]/api/2.0/my/main/current',
				method = 'GET',
				callback = 'Powerfoxtrigger', -- see httpResponses above.
			})
	end

	if (item.isHTTPResponse) then

			if (item.ok) then
				if (item.isJSON) then

					local USAGE1 = item.json.A_Plus 
					local RETURN1 = item.json.A_Minus
					local CONS = item.json.Watt 
					  domoticz.log(item.json.A_Plus , domoticz.LOG_INFO)
					  domoticz.log(item.json.A_Minus, domoticz.LOG_INFO)
					  domoticz.log(item.json.Watt, domoticz.LOG_INFO)
					  domoticz.log('Success updating receiving http values', domoticz.LOG_INFO)
					 
					end
			
				    
				    domoticz.devices('Stromverbrauch').updateP1(item.json.A_Plus, '0', item.json.A_Minus, '0', item.json.Watt , '0')
					
                    
					
                   domoticz.log('Success updating Device P1', domoticz.LOG_INFO)
                   
                    
				end
	   
		
			

		end

	end
}

Re: API script /http polling Powerfox Energy tracker

Posted: Sunday 30 January 2022 15:02
by waltervl
The divider is done by Domoticz setting for divider (in menu Setup - Settings tab Counter)
So you have to always supply Wh values for P1 (and also other Electricity Counters)!
See also https://www.domoticz.com/wiki/Domoticz_ ... mart_meter

Perhaps also try to supply integer values and not fractional eg 1305727.0 if you run into strange results.

Re: API script /http polling Powerfox Energy tracker

Posted: Sunday 30 January 2022 15:57
by Mirkoser
Bildschirmfoto vom 2022-01-30 15-49-51.png
Bildschirmfoto vom 2022-01-30 15-49-51.png (25.62 KiB) Viewed 3846 times
ok, but why still a dot is shown? i activated the p1 "no decimals" in the settings

Re: API script /http polling Powerfox Energy tracker

Posted: Sunday 30 January 2022 16:25
by waltervl
What values are you sending according your log?
What is the setting of menu Setup - Settings tab Counter?

Re: API script /http polling Powerfox Energy tracker

Posted: Sunday 30 January 2022 16:44
by Mirkoser
Bildschirmfoto vom 2022-01-30 16-44-00.png
Bildschirmfoto vom 2022-01-30 16-44-00.png (74.5 KiB) Viewed 3842 times
Bildschirmfoto vom 2022-01-30 16-43-33.png
Bildschirmfoto vom 2022-01-30 16-43-33.png (56.77 KiB) Viewed 3842 times

Re: API script /http polling Powerfox Energy tracker

Posted: Sunday 30 January 2022 16:46
by Mirkoser
so it should show me 1434 Kwh , 196Kwh , 2991W

If i get a negative value from the powerfox for Watt it will not work and i have no idea how to realize it in the script

Re: API script /http polling Powerfox Energy tracker

Posted: Sunday 30 January 2022 20:58
by waltervl
There is an issue with decimals yes or no especially on graphs.
So that seemsnto be a bug.
I will come back for the negative value.

Re: API script /http polling Powerfox Energy tracker

Posted: Sunday 30 January 2022 21:13
by waltervl
DzVents is based on Lua so math.abs() function should work.

Code: Select all

GetWatt = item.json.Watt
If GetWatt < 0  then
   CONS = 0
   PROD = math.abs(GetWatt)
else
  CONS = GetWatt
  PROD = 0
end
It would perhaps also help to use math.floor() to get numbers without decimals. math.floor(1234.0) will give 1234