logo

Connecting Domoticz to Daytopper

12/07/2024

For many, Domoticz is a well-known player in the automation landscape. It is the counterpart of the well-known Home Assistant. Despite its somewhat dated look, it is a powerful system for managing your entire home automation. It comes with an extensive set of powerful devices (e.g., P1 meter, Zigbee support, Z-Wave support, and much more) and is fully customizable thanks to its unique event system.

Some users also use Domoticz to monitor their solar panels. This way, you can use your surplus energy to control various devices in your home. For example, you can turn on your pool filter or boiler when there is surplus energy, or switch on a smart plug that is connected to your car's granny charger.

Step 1 - Register your inverter

In this FAQ article, you will find instructions on how to register an inverter. Once you have completed these steps up to step 5, copy the token. We will need this in the next step.

Step 2 - create a new DzEvent script

To connect Domoticz to Daytopper, we can use a DzEvent script. This is a script commonly used within Domoticz to respond to events. With this script, we will respond to your solar device (i.e., when the device is updated) and send the data to the Daytopper API.

Go into your Domoticz installation and navigate Settings -> More Options -> Events. Here you can create a new event script.

local solarDeviceId = 40 <-- Your solar device id
local daytopperApiKey = "" <-- Your Daytopper API key

return {
	on = {
		devices = {
			solarDeviceId,
		},
		httpResponses = {
			'triggerDaytopperLogin',
			'triggerDaytopperPost'
		},
	},
	logging = {
		level = domoticz.LOG_INFO,
		marker = 'Daytopper',
	},
	execute = function(domoticz, item)
	 	local solarDevice = domoticz.devices(solarDeviceId)
		local total = solarDevice.WhTotal;
		local current = solarDevice.actualWatt;

		if (item.isHTTPResponse) then
			
			if (item.ok) then
				if (item.isJSON) then
					local result = item.json

					if (item.trigger == 'triggerDaytopperLogin') then
					    domoticz.log('Sending data to Daytopper API...')
					    domoticz.openURL({
            				url = 'https://api.solar-daytopper.com/solar',
            				method = 'POST',
            				callback = 'triggerDaytopperPost',
            				headers = {
                                Authorization = "Bearer " .. result.token,
                                ['Content-Type'] = "application/json"
                            },
            				postData = "{\"total\":" .. total .. " ,\"current\": " .. current .."}"
            			})
					elseif (item.trigger == 'triggerDaytopperPost') then
					    domoticz.log(result)
						domoticz.log('Succes sending data')
					end
					
				else
					domoticz.log('HTTP is not json', domoticz.LOG_ERROR)
				end
			else
				domoticz.log('There was a problem handling the request', domoticz.LOG_ERROR)
				domoticz.log(item, domoticz.LOG_ERROR)
			end
		else
			domoticz.openURL({
				url = 'https://api.solar-daytopper.com/device/login',
				method = 'POST',
				callback = 'triggerDaytopperLogin',
				postData = {
		            token = daytopperApiKey
		        }
			})
		end

	end
}

In the future, we also want to explore creating a plugin that works in both directions. Either you send your data from Domoticz to the Daytopper API, or you use the Domoticz plugin to retrieve your current generation data from Daytopper and store it in Domoticz. But that is for later.