aWattar integration

Use this forum to discuss possible implementation of a new feature before opening a ticket.
A developer shall edit the topic title with "[xxx]" where xxx is the id of the accompanying tracker id.
Duplicate posts about the same id. +1 posts are not allowed.

Moderators: leecollings, remb0

Post Reply
multifluid
Posts: 7
Joined: Monday 25 May 2020 15:16
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

aWattar integration

Post by multifluid »

There is a power provider (aWATTar) in Germany who charges by hourly power consumption. The fee is partly fixed and gets a variable on top driven by the price per kWh on the stock market. The price on the stock market can even be negative if there is too much power available. Only green energy is used.

The price will be announced 24h in advance so you can charge your car or switch on the washing machine base on the kWh price.

The API is availbale here https://www.awattar.de/services/api (German only).

Let me know you thoughts.

Thanks,
Multi
elmortero
Posts: 247
Joined: Sunday 29 November 2015 20:46
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.9639
Location: Spain
Contact:

Re: aWattar integration

Post by elmortero »

Depends on how you would want to use it, but following dzvents script will fetch the value for now+24h
If what you want is to get it for the next x hours, some adaptations will be needed.

Code: Select all

return {
	on = {
		timer = { 'every hour'},
		httpResponses = { 'aWATTar' } -- matches callback string below
	},
	
	execute = function(domoticz, triggerItem)
    local WanIp = domoticz.devices('WanIp')
    local prevWanIP = tostring(WanIp.text)
		if (triggerItem.isTimer) then
			domoticz.openURL({
				url = 'https://api.awattar.de/v1/marketdata',
				method = 'GET',
				callback = 'aWATTar'
			})
			
		elseif (triggerItem.isHTTPResponse) then

	local response = triggerItem
		if (response.ok and response.isJSON) then
		local price = tostring(response.json.data[24].marketprice)   
        print('IN 24 hours the cost will be '..price..' Eur/MWh')
        -- do here other stuff
			else
				print('**getip failed to fetch info')
			end
		end
	end
}
This script just gets the value and prints it to the log, but of course you can add any action - given it's possible in domoticz 8) - to it.
multifluid
Posts: 7
Joined: Monday 25 May 2020 15:16
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: aWattar integration

Post by multifluid »

I guess, I need to learn how to use dzvents for this. :?

The API will only provide once per day the values for the next day. I would like to set a threshold under which a switch or my e car charger would be turned on.
aWATTar.png
aWATTar.png (56.55 KiB) Viewed 1667 times
In this example, I'd say, switch on when less than zero.
elmortero
Posts: 247
Joined: Sunday 29 November 2015 20:46
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.9639
Location: Spain
Contact:

Re: aWattar integration

Post by elmortero »

So basically you want:
  • You have a switch to start charging the car
  • Read the content of https://api.awattar.de/v1/marketdata which returns a json
  • From that json, find the time where the price is the lowest (in current data I don't see negative values, but anyways, the lowest
  • Then with that time create a timer on the switch that switches on at the time we fetched above
Is that about right?

What if the car is not connected at the time?
What if non of the rates is below the threshold? Do you then not charge it?
multifluid
Posts: 7
Joined: Monday 25 May 2020 15:16
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: aWattar integration

Post by multifluid »

elmortero wrote: Wednesday 27 May 2020 16:53 So basically you want:
  • You have a switch to start charging the car
    Yes, already working with Domoticz.
  • Read the content of https://api.awattar.de/v1/marketdata which returns a json
    Yes
  • From that json, find the time where the price is the lowest (in current data I don't see negative values, but anyways, the lowest
    Either the lowest or a value you can set. I will need to learn the data myself over time.
  • Then with that time create a timer on the switch that switches on at the time we fetched above
    Yes
Is that about right?

What if the car is not connected at the time?
If the car is not connected, it will not get charged, no problem.
What if non of the rates is below the threshold? Do you then not charge it?
Right, it might be only some hours per week where the set criteria is true. Therefore the threshold need to be adjustable.
Yep, that sounds right.

Thanks,
Multi
elmortero
Posts: 247
Joined: Sunday 29 November 2015 20:46
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.9639
Location: Spain
Contact:

Re: aWattar integration

Post by elmortero »

Hi, this is what I came up with.
I am putting comments outside of the code to keep it clean.
The time to run the script is set to 23:55 - in fact any time between 23:01 and 23:59 should be ok - so that it does not mess with schedules. If you should run it for example at 0:01 and the cheapest time is between 0:00 and 1:00 the timer will be set for that timeframe BUT the day after.
This script is for the lowest value, you need to decide if you want the to use a threshold, it changes the way how (or rather how far) we need to iterate over the results. In that case the logical way would be to look through the prices and it will stop at the first price that is below threshold, stop there and use that timeframe for the schedule of the charger switch.
For learning purposes - you are right, you should get into understanding dzvents a bit (for adaptations, troubleshooting...) - I have not used special functions / objects(without this disclamer I might get a slap on the wrist from dannybloe 8-) ) so that it is an easy start for you.
What we do is :
  • Define the idx of your switch that needs to be scheduled (your charger)
  • Set the url to your domoticz instance, here it is http://192.168.x.x:8080 but you should of course put the IP address for your domoticz and the port number too
  • Set the time when the script will run, as you said one time a day, here it is set to 23:55
  • The api url is being opened, due to Asynchronous_HTTP_Request (at some point you should study that, in case you change provider, the url changes..)
  • We then wait for domoticz to trigger the callback event --> the url was opened and we got a response, the script gets triggered again and continues to go through the results
  • We set low_time = ''; low_price = 999 --> the cheapest time (empty because we don't know yet and the lowest price --> extremely high you will see why
  • An url is created based on your switch IDX to clear all schedules it might have !! This is important to know if you already use a schedule, it will be removed at this point. Should not be an issue if you are going to let this script decide when to charge
  • Then we go through the results one by one and check if the price is lower than the initial 999 (which it will be hopefully for our lifetime), if that is the case the variable low_price is updated with that price and we set the time (low_time) to the corresponding time
  • This goes on for all results and in the end the value of low_price will contain the value we are looking for with the matching timeframe
  • Then we create a url - that we will call later - with the time from low_time
  • Next we open the url so that for the switch defined by IDX (on the very first line of the script)
You must, sorry for the imperative form, read: Using dzVents with Domoticz, especially the first paragraph and of course the part on how to create a dzvents script.

Last, but not least, you should know I did some testing, not too much (I wrote the script an hour ago, spending more explaining that it took to write the actual code) but you might want to let it run dry for a while. You could create a virtual switch or put the IDX of a light at first to see if it activates on the correct time. Also, you will notice there will not be a schedule for switch off. I don't know how these (or yours) car chargers work, I assume they stop charging as soon as the battery is fully charged, and if you switch it of (in domoticz) when you disconnect. If you know how long (maximum) it takes to charge we could add a schedule to switch of at (starttime + max charge time)

Code: Select all

local IDX = 'XXX'
local domo_url = 'http://192.168.x.x:8080'
return {
	on = {
		timer = { 'at 23:55'},
		httpResponses = { 'aWATTar' }
	},
	
	execute = function(domoticz, triggerItem)
		if (triggerItem.isTimer) then
			domoticz.openURL({
				url = 'https://api.awattar.de/v1/marketdata',
				method = 'GET',
				callback = 'aWATTar'
			})
			
		elseif (triggerItem.isHTTPResponse) then

	local response = triggerItem
		if (response.ok and response.isJSON) then
        		low_time = ''; low_price = 999
        		sched_clr = domo_url..'/json.htm?type=command&param=cleartimers&idx='..IDX
        		domoticz.openURL(sched_clr)
        	    t = 1
        	    repeat
            		local raw_start_time = (response.json.data[t].start_timestamp) /1000
            		date = os.date("*t",raw_start_time)
            		hora = (tostring(date.hour)..':00')
            		local price = tonumber(response.json.data[t].marketprice)
                		if price < low_price then 
                		    low_price = price
                		    low_time = hora
                		    low_hora = tostring(date.hour)
            		    end
            		t = t +1
                until t > 24
                print(low_time..' has the lowest price: '..low_price)
                sched_set =  domo_url..'/json.htm?type=command&param=addtimer&idx='..IDX..'&active=true&timertype=2&hour='..low_hora..'&min=0&randomness=false&command=0&days=0'
                domoticz.openURL(sched_set)
			else
				print('**aWATTar failed to fetch info')
			end
		end
	end
}
multifluid
Posts: 7
Joined: Monday 25 May 2020 15:16
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: aWattar integration

Post by multifluid »

Wow, I'm really impressed with your support!

Give my some time (couple of days) to get into the weeds of dzVents and your guide.

Thanks again for now

Multi
multifluid
Posts: 7
Joined: Monday 25 May 2020 15:16
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: aWattar integration

Post by multifluid »

Okay, some first feedback. The script is running, I changed some things with some other help. I adjusted the IPX, the timing and the trigger value. Now I will monitor how the switching works. No further questions for now.

Thanks again alot!

Multi
User avatar
mgerhard74
Posts: 48
Joined: Friday 28 July 2017 18:28
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.7
Location: Austria, Upperaustria
Contact:

Re: aWattar integration

Post by mgerhard74 »

Hello ,
does this script still working in domoticz 2022.2?
I got this in log
2023-01-21 11:30:05.647 Status: dzVents: Info: ------ Start internal script: aWattar:, trigger: "every minute"
2023-01-21 11:30:05.647 Status: dzVents: Info: ------ Finished aWattar
...but i got no new schedule in the switch IDX 141.

I use this simple code for test, but i get no result "start_timestamp":

Code: Select all

return {
	on = {
		timer = { 'every minute'},
		httpResponses = { 'aWATTar' }
	},
	execute = function(domoticz, triggerItem)
		if (triggerItem.isTimer) then
    	    starttime = os.time(os.date('*t')) * 1000
	        stoptime = (os.time(os.date('*t')) + 86000) * 1000
	        print('Start: '..tostring(starttime)..', Stop: '..tostring(stoptime))
			domoticz.openURL({
				url = 'https://api.awattar.de/v1/marketdata?start='..tostring(starttime)..'&' .. 'end='..tostring(stoptime)..'',
				method = 'GET',
				callback = 'aWATTar'
			})
    
        elseif triggerItem.isHTTPResponse then
	
	    local response = triggerItem
        if (response.ok) then
        	print(response.json.data[1]['start_timestamp'])
		end
        end
	end
}
Gerhard
Domoticz improves my photovoltaik ownconsumption (Rpi3, wifi plugs) - PV 6,5kWp (Fronius Symo inverter) - 10kWh PV batterie - Nissan Leaf2 (40kWh) and Kia eNiro (64kWh)
User avatar
waltervl
Posts: 5721
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: aWattar integration

Post by waltervl »

Perhaps the json from aWattar has changed?
Nothing really big has changed on dzVents the last releases.
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
User avatar
waltervl
Posts: 5721
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: aWattar integration

Post by waltervl »

I tested a simple version of the original script not updating schedules and devices, only logging and it works without problems.

Code: Select all

return {
	on = {
		timer = { 'every minute'},
		httpResponses = { 'aWATTar' }
	},
	
	execute = function(domoticz, triggerItem)
		if (triggerItem.isTimer) then
			domoticz.openURL({
				url = 'https://api.awattar.de/v1/marketdata',
				method = 'GET',
				callback = 'aWATTar'
			})
			
		elseif (triggerItem.isHTTPResponse) then

	local response = triggerItem
		if (response.ok and response.isJSON) then
        		low_time = ''; low_price = 999
        		
        		
        	    t = 1
        	    repeat
            		local raw_start_time = (response.json.data[t].start_timestamp) /1000
            		date = os.date("*t",raw_start_time)
            		hora = (tostring(date.hour)..':00')
            		local price = tonumber(response.json.data[t].marketprice)
                		if price < low_price then 
                		    low_price = price
                		    low_time = hora
                		    low_hora = tostring(date.hour)
            		    end
            		t = t +1
                until t > 24
                print(low_time..' has the lowest price: '..low_price)
			else
				print('**aWATTar failed to fetch info')
			end
		end
	end
}
Spoiler: show
2023-01-21 17:07:00.122 Status: dzVents: Info: ------ Start internal script: awattar test:, trigger: "every minute"
2023-01-21 17:07:00.122 Status: dzVents: Info: ------ Finished awattar test
2023-01-21 17:07:00.435 Status: dzVents: Info: Handling httpResponse-events for: "aWATTar"
2023-01-21 17:07:00.436 Status: dzVents: Info: ------ Start internal script: awattar test: HTTPResponse: "aWATTar"
2023-01-21 17:07:00.437 Status: dzVents: 2:00 has the lowest price: 116.77
2023-01-21 17:07:00.437 Status: dzVents: Info: ------ Finished awattar test
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
User avatar
mgerhard74
Posts: 48
Joined: Friday 28 July 2017 18:28
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.7
Location: Austria, Upperaustria
Contact:

Re: aWattar integration

Post by mgerhard74 »

Thank you for testing, i used your script and i got only:
Spoiler: show
2023-01-21 17:15:43.634 Status: dzVents: Info: ------ Start internal script: aWattar:, trigger: "every minute"
2023-01-21 17:15:43.634 Status: dzVents: Debug: OpenURL: url = https://api.awattar.de/v1/marketdata
2023-01-21 17:15:43.635 Status: dzVents: Debug: OpenURL: method = GET
2023-01-21 17:15:43.635 Status: dzVents: Debug: OpenURL: post data = nil
2023-01-21 17:15:43.635 Status: dzVents: Debug: OpenURL: headers = nil
2023-01-21 17:15:43.635 Status: dzVents: Debug: OpenURL: callback = aWATTar
2023-01-21 17:15:43.635 Status: dzVents: Info: ------ Finished aWattar
2023-01-21 17:15:43.635 Status: dzVents: !Info: Debug: Writing module summary to /home/pi/domoticz/scripts/dzVents/module.log
2023-01-21 17:15:43.635 Status: dzVents: Debug: Commands sent to Domoticz:
2023-01-21 17:15:43.636 Status: dzVents: Debug: - OpenURL = {["_trigger"]="aWATTar", ["method"]="GET", ["URL"]="https://api.awattar.de/v1/marketdata"}
2023-01-21 17:15:43.636 Status: dzVents: Debug: =====================================================
Domoticz improves my photovoltaik ownconsumption (Rpi3, wifi plugs) - PV 6,5kWp (Fronius Symo inverter) - 10kWh PV batterie - Nissan Leaf2 (40kWh) and Kia eNiro (64kWh)
User avatar
waltervl
Posts: 5721
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: aWattar integration

Post by waltervl »

Perhaps make callback and httpresponse value (now 'aWattar') different from your normal script. DzVents normally needs these to be unique for each script.

It seems that "triggerItem.isHTTPResponse" is not working.
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
User avatar
mgerhard74
Posts: 48
Joined: Friday 28 July 2017 18:28
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.7
Location: Austria, Upperaustria
Contact:

Re: aWattar integration

Post by mgerhard74 »

Thank you helping me to solve this problem. After changing 'aWattar' to 'WATT' it is working perfekt.
This is not clear for me, because it is my first and only DzVents script - maybe it was a problem, because my new switch has the name 'aWattar'.
Domoticz improves my photovoltaik ownconsumption (Rpi3, wifi plugs) - PV 6,5kWp (Fronius Symo inverter) - 10kWh PV batterie - Nissan Leaf2 (40kWh) and Kia eNiro (64kWh)
User avatar
mgerhard74
Posts: 48
Joined: Friday 28 July 2017 18:28
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.7
Location: Austria, Upperaustria
Contact:

no HTTPResponse?

Post by mgerhard74 »

My Awattar script normally runs once per day (14:03), but in most cases, it did not correct finish.
Log:
Status: dzVents: Info: ------ Start internal script: aWattar:, trigger: "every minute"
Status: dzVents: Info: ------ Finished aWattar

Code: Select all

return { on = 
    {timer = 
        { 'at 14:03'},
        httpResponses = { 'WATT' }	
        },
	
	execute = function(domoticz, triggerItem)
	if (triggerItem.isTimer) then
		domoticz.openURL({
		    url = 'https://api.awattar.at/v1/marketdata',	
		    method = 'GET',	
		    callback = 'WATT'	
		    })

	elseif (triggerItem.isHTTPResponse) then
    
        local response = triggerItem
		if (response.ok and response.isJSON) then
        		low_time = ''; low_price = 999
        	    t = 1
        	    repeat
                    if response.json.data[t].start_timestamp ~= nil then
                        local raw_start_time = (response.json.data[t].start_timestamp) /1000
            		    date = os.date("*t",raw_start_time)
            		    hora = (tostring(date.hour)..':00')
            		    local price = tonumber(response.json.data[t].marketprice)
            		    print ('Stunde: '..tostring(hora)..', Preis: '..tostring(price))
                		    if price < low_price then 
                		        low_price = price
                		        low_time = hora
                		        low_hora = tostring(date.hour)
                		        if (date.hour+1) == 24 then
                		            low_hora_off = '1'
                		        else
                		            low_hora_off = tostring(date.hour+1)
                		        end      
    		                end 
    		            end
            		t = t + 1
                until t>24
                domoticz.variables('aWattarTime').set(low_hora..':00')
                domoticz.variables('aWattarPrice').set(low_price)
                print('Um '..low_time..' Uhr gibts den niedrigsten Preis der nächsten 24h: '..low_price..' Eur/MWh')
    end
    end
}
Can anyone find the issue?
Domoticz improves my photovoltaik ownconsumption (Rpi3, wifi plugs) - PV 6,5kWp (Fronius Symo inverter) - 10kWh PV batterie - Nissan Leaf2 (40kWh) and Kia eNiro (64kWh)
User avatar
waltervl
Posts: 5721
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: aWattar integration

Post by waltervl »

Just put some more logging in the script to debug where it fails.
If this is the only thing logged it like the response is not as expected (not ok or not a JSON)
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
User avatar
mgerhard74
Posts: 48
Joined: Friday 28 July 2017 18:28
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.7
Location: Austria, Upperaustria
Contact:

Re: aWattar integration

Post by mgerhard74 »

The problem is that triggerItem.isHTTPResponse is always "false".
As i checked with Firefox the https://api.awattar.at/v1/marketdata returns a content-type application/json; charset=utf-8.
The Api is described at https://www.awattar.at/services/api
Domoticz V2023.1 on a Raspi 3B Raspbian 10 Buster, dzvents 3.1.8

Is there a bug at https://api.awattar.at/v1/marketdata or in dzvents recognize not a application/json???

Code: Select all

return {
	on = {
		timer = { 'every minute'},
		httpResponses = { 'aWATTar' }
	},
	
	execute = function(domoticz, triggerItem)
		if (triggerItem.isTimer) then
			domoticz.openURL({
				url = 'https://api.awattar.at/v1/marketdata',
				method = 'GET',
				callback = 'aWATTar'
			})
        print ('triggerItem.isHTTPResponse:  ' .. tostring(triggerItem.isHTTPResponse));	
		elseif (triggerItem.isHTTPResponse) then

	local response = triggerItem
		if (response.ok and response.isJSON) then
        		low_time = ''; low_price = 999
        	    t = 1
        	    repeat
            		local raw_start_time = (response.json.data[t].start_timestamp) /1000
            		date = os.date("*t",raw_start_time)
            		hora = (tostring(date.hour)..':00')
            		local price = tonumber(response.json.data[t].marketprice)
                		if price < low_price then 
                		    low_price = price
                		    low_time = hora
                		    low_hora = tostring(date.hour)
            		    end
            		t = t +1
                until t > 12
                print(low_time..' has the lowest price: '..low_price)
			else
				print('**aWATTar failed to fetch info')
			end
		end
	end
}
Domoticz improves my photovoltaik ownconsumption (Rpi3, wifi plugs) - PV 6,5kWp (Fronius Symo inverter) - 10kWh PV batterie - Nissan Leaf2 (40kWh) and Kia eNiro (64kWh)
User avatar
waltervl
Posts: 5721
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: aWattar integration

Post by waltervl »

The test script I used before in this thread is still working on my system (2023.1)
So if that is also still working on yours check the differences. Be aware of the callback names.
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: Amazon [Bot] and 1 guest