Communication script with investor goodwe

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
tonymer
Posts: 16
Joined: Wednesday 15 June 2022 16:43
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Communication script with investor goodwe

Post by tonymer »

Hello, I leave here the script to communicate directly with a goodwe inverter (EM Series, possibly it works in all of them) locally. All the data comes in the inverter's response, although I only use positions 46-47 that indicate the energy that is exported and position 88 that indicates whether the energy mentioned above is exported or imported. I have created a virtual device (Dummy) of energy use that is where I store received data. If the energy is exported it is +, if the energy is imported it is -. you have to install the socket package for Lua

If you want to get other data you can check what each position means (not all) here:

https://powerforum.co.za/topic/6931-goo ... gineering/

Code: Select all

return {
	on = {
		timer = {
			'every 2 minutes',function(domoticz)
			    
			
			end,
		}
	},
	logging = {
		level = domoticz.LOG_INFO,
		marker = 'template',
	},
	execute = function(domoticz, timer)
	    
	    package.cpath ="/usr/lib/arm-linux-gnueabihf/lua/5.3/?.so;" .. package.cpath
	    package.path = "/usr/share/lua/5.3/?.lua;" .. package.path
	    
	    local socket = require("socket")
	    local udp = assert(socket.udp())
        local datosrecibidos
        local energiadisponible
        
        --ponemos timeout
        udp:settimeout(5)
        assert(udp:setsockname("*",0))
        assert(udp:setpeername("192.168.1.10",8899))
        --enviamos solicitud
        assert(udp:send("\xAA\x55\xc0\x7f\x01\x06\x00\x02\x45"))
        --recibimos cadena de datos
        datosrecibidos = udp:receive()
        
        --Donde almacenamos los bytes recibidos
        local bytes={}
        
         --convertimos los datos Recibidos a un array hexadecimal (jugamos con el array internamente)
        for i=1,datosrecibidos:len()  do
                    bytes[#bytes+1] = ('%2x'):format(datosrecibidos:byte(i,i))
                end
        --Posiciones necesarias empezando de 1
        print("Posicion 46: "..bytes[46])
        print("Posicion 47: "..bytes[47])
        print("Posicion 88: "..bytes[88])
        --Exportando (Se asignará valor en positivo)
            if ((tonumber(bytes[88],16)==0)or(tonumber(bytes[88],16)==1)) then        
                energiadisponible=tonumber(bytes[46]..bytes[47],16)
            end
            
            --Importando (Se asignara valor en negativo)
            if (tonumber(bytes[88],16)==2) then        
                energiadisponible=tonumber(bytes[46]..bytes[47],16)*-1
                end
           
        --cerramos socket
        udp:close()
        
        --Asignamos al sensor virtual de energia el valor
        if (energiadisponible~=nil) then
        print("Energia disponible="..energiadisponible)
	    domoticz.devices('Energia Disponible').updateEnergy(energiadisponible)
	    end
	    
		domoticz.log('Timer event was triggered by ' .. timer.trigger, domoticz.LOG_INFO)
	end
}
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest