use DzVents for SolarEgde

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

Moderator: leecollings

Post Reply
jluc2808
Posts: 49
Joined: Thursday 18 January 2018 6:30
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

use DzVents for SolarEgde

Post by jluc2808 »

hi,
solaredge is already embedded in domoticz but without the Consumption and SelfConsumption meters. (depending of the material installed, you could store the consumption with all the other information)
i have modified the production info to display "Production now" and "Production of the day"
i have added the consumption info to display "Consumption now" and "Consumption of the day"
i have modified the self_consumption info to display "self consumption now", "Self consumption of the day" , "% of self consumption" which is what you expect to be the real beneficit of your installation

Image

Now the dzVents scripts which use Global_data to get and store info, apikey, solaredge_id, production , consumption, self consumption
So they could be displayed as you want.

Code: Select all

--[[
script_solaredge_consommation_production.lua
auteur : JL Berno

MAJ : 08/01/2019
création : 08/01/2019

Principe : ce script va chercher toutes les minutes (modifiable via la variable delai) la valeur de consoammation electrique mesurée par solaredge
et affiche la valeur dans un dispositifs mesure de consommation solaredge.

pour la consommation instantanée
URL GET : "https://monitoringapi.solaredge.com/site/".. Solaredge_Id .. "/powerDetails.json?&startTime=" .. maintenant_moins_15minutes .. "&endTime=" .. maintenant .. "&api_key=" .. Solaredge_Apikey

pour la consommation  du jour
URL GET : "https://monitoringapi.solaredge.com/site/".. Solaredge_Id .. "/energyDetails.json?&timeUnit=DAY&startTime=" .. debut_de_journee .. "&endTime=" .. fin_de_journee .. "&api_key=" .. Solaredge_Apikey
--]]
------------------------ variables de script -----------------------
local nom_script = "solaredge_consommation_production"
local version = "2.0"
local debugging_solaredge_conso = false          -- true pour voir les logs dans la console log Dz ou false pour ne pas les voir
local script_actif    = true                    -- active (true) ou désactive (false) ce script simplement
local delai = 10                                -- délai d'exécution de ce script en minutes de 1 à 59
--------------------------------------------------------------------
return {
	logging = {
	-- level = domoticz.LOG_DEBUG, -- Uncomment to override the dzVents global logging setting
		marker = nom_script
	},
	on = {
		timer = {
			'every ' .. delai .. ' minutes' -- to trigger the request
		},
		httpResponses = {
			'solaredge_conso_now_trigger',  'solaredge_conso_day_trigger' -- must match with the callback passed to the openURL command
		}
	},
	execute = function(domoticz, item_solaredge)

--------------------------------------------
------------ Variables à éditer ------------
-------------------------------------------- 
local message = {}                                              -- pas utilisé
local Solaredge_Id =  domoticz.globalData.Solaredge_SiteId      -- n° de site solaredge à récupérer zur le site 
local Solaredge_Apikey = domoticz.globalData.Solaredge_APIKey   -- api_key à récupérer sur le site / compte 
local seuil_notification = nil                                  -- pas utilisé
local Solar_Production = domoticz.devices(61)					-- Dispositif virtuel pour afficher la Production Solaire électrique, type: kWh, Electricity (instant and counter)
local Elect_Consommation = domoticz.devices(63) 				-- Dispositif virtuel pour afficher la Consommation électrique, type: kWh, Electricity (instant and counter)
local Self_Consommation = domoticz.devices(64)					-- Dispositif virtuel pour afficher l'auto Consommation de Production Solaire électrique, type: Electric usage
local Pourcent_Self_Consommation = domoticz.devices(66) 		-- Dispositif virtuel pour afficher le % d'auto consommation par rapport à la consommation totale, type : Pourcent 
local Text_Self_Consommation = domoticz.devices(65)				-- Dispositif virtuel pour afficher un texte d'auto consommation, type : Text  
---------- Fin des Variables à éditer ------


    -- le timer est passé on déclenche la requête de la conso instantanée 

		if (item_solaredge.isTimer) then
		    -- construction des bornes de temps pour la requête instantanée
		    maintenant = os.date('%Y-%m-%d%%20%H:%M:%S', os.time())
            maintenant_moins_15minutes = os.date('%Y-%m-%d%%20%H:%M:%S', (os.time()- (15*60)))
            maintenant_moins_xminutes = os.date('%Y-%m-%d%%20%H:%M:%S', (os.time()- (delai*60)))
        
        	solaredge_now_url = "https://monitoringapi.solaredge.com/site/".. Solaredge_Id .. "/powerDetails.json?&startTime=" .. maintenant_moins_xminutes .. "&endTime=" .. maintenant .. "&api_key=" .. Solaredge_Apikey
            if (debugging_solaredge_conso) then 
                domoticz.log(solaredge_now_url)
            end 
        	domoticz.openURL({
        	    url = solaredge_now_url,
        		method = 'GET',
        		callback = 'solaredge_conso_now_trigger', -- see httpResponses above.
        		})
        		
        else            -- ce n'est pas le time donc c'est la requête qui déclenche 
        -- contrôle qu'il n'y a pas de problème dans la requête et son retour 
    		if (item_solaredge.isHTTPResponse or item_solaredge.statusCode == 200) then
    		    domoticz.log(item_solaredge, domoticz.LOG_DEBUG)        -- en mode debug afficher le contenu du retour 
    		    ------------------------    conso et prod instantanée --------------------------------
    		    -- on regarde de quelle requête ça vient  - si c'est pour la conso instantanée
    		    ------------------------------------------------------------------------------
    		    
    		    if (item_solaredge.trigger == 'solaredge_conso_now_trigger') then
    
       			    domoticz.log('traitement du retour de la requête solaredge_conso_now_trigger', domoticz.LOG_INFO)
       			    
       			    local now_results = item_solaredge.json.powerDetails.meters
            			local Now_SelfConsumption_value = 0
            			local Now_Production_value = 0
            			local Now_Consumption_value = 0
            			local Now_FeedIn_value = 0
            			local Now_Purchased_value = 0
                        -- loop through the nodes and print some info
                        for i, node in pairs(now_results) do
                            if (node.type == "SelfConsumption") then 
                                if (node.values[1].value) then
                                    Now_SelfConsumption_value = node.values[1].value
                                    domoticz.log("i: "..i .. " node.type: " .. node.type .. " Now_SelfConsumption_value: " ..Now_SelfConsumption_value, domoticz.LOG_INFO)
                                    Self_Consommation.updateEnergy(Now_SelfConsumption_value)                    -- met à jour le compteur 
                                    domoticz.globalData.Solaredge_SelfConso_Now = Now_SelfConsumption_value       -- met à jour la variable globale 
                                end
                            elseif (node.type == "Production") then
                                if (node.values[1].value) then
                                    Now_Production_value = node.values[1].value
                                     domoticz.log("i: "..i .. " node.type: " .. node.type .. " Now_Production_value: " ..Now_Production_value, domoticz.LOG_INFO)
                                     domoticz.globalData.Solaredge_Prod_Now = Now_Production_value       -- met à jour la variable globale
                                 end
                            elseif (node.type == "Consumption") then
                                if (node.values[1].value) then
                                    Now_Consumption_value = node.values[1].value
                                    domoticz.log("i: "..i .. " node.type: " .. node.type .. " Now_Consumption_value: " ..Now_Consumption_value, domoticz.LOG_INFO)
                                    domoticz.globalData.Solaredge_Conso_Now = math.floor(tonumber(Now_Consumption_value) * 10 + 0.5) / 10       -- met à jour la variable globale
                                end
                            elseif (node.type == "FeedIn") then
                                if (node.values[1].value) then
                                    Now_FeedIn_value = node.values[1].value
                                    domoticz.log("i: "..i .. " node.type: " .. node.type .. " Now_FeedIn_value: " ..Now_FeedIn_value, domoticz.LOG_INFO)
                                end
                            elseif (node.type == "Purchased") then
                                if (node.values[1].value) then
                                    Now_Purchased_value = node.values[1].value
                                    domoticz.log("i: "..i .. " node.type: " .. node.type .. " Now_Purchased_value: " ..Now_Purchased_value, domoticz.LOG_INFO)
                                end
                            end
                        end
                        
       			    

                    -- maintenant on déclenche la requête conso du jour 
                    
                        -- construction des bornes de temps pour requête journalière 
                            debut_de_journee = os.date("%Y-%m-%d") .. "%2000:00:00"
                		    fin_de_journee = os.date("%Y-%m-%d") .. "%2023:59:59"
                		    
                		    solaredge_day_url = "https://monitoringapi.solaredge.com/site/".. Solaredge_Id .. "/energyDetails.json?&timeUnit=DAY&startTime=" .. debut_de_journee .. "&endTime=" .. fin_de_journee .. "&api_key=" .. Solaredge_Apikey
                            domoticz.log(solaredge_day_url, domoticz.LOG_DEBUG)
                			domoticz.openURL({
                			url = solaredge_day_url,
                			method = 'GET',
                			callback = 'solaredge_conso_day_trigger', -- see httpResponses above.
                			})
                end
                ------------------------  fin conso et prod instantanée -------------------------------
                     
                     
                
                ------------------------    conso et prod du jour --------------------------------------
                -- on regarde de quelle requête ça vient  - sinon c'est pour la conso du jour
                --------------------------------------------------------------------------------
                
                if (item_solaredge.trigger == 'solaredge_conso_day_trigger') then
                
                    domoticz.log('traitement du retour de la requête solaredge_conso_day_trigger', domoticz.LOG_INFO)
 
                    local today_results = item_solaredge.json.energyDetails.meters
            			local Day_SelfConsumption_value = 0
            			local Day_Production_value = 0
            			local Day_Consumption_value = 0
            			local Day_FeedIn_value = 0
            			local Day_Purchased_value = 0
                        -- loop through the nodes and print some info
                        for i, node in pairs(today_results) do
                            if (node.type == "SelfConsumption") then 
                                if (node.values[1].value) then
                                    Day_SelfConsumption_value = node.values[1].value
                                    domoticz.log("i: "..i .. " node.type: " .. node.type .. " Day_SelfConsumption_value: " ..Day_SelfConsumption_value, domoticz.LOG_INFO)
                                    domoticz.globalData.Solaredge_SelfConso_Today = Day_SelfConsumption_value       -- met à jour la variable globale 
                                end
                            elseif (node.type == "Production") then
                                if (node.values[1].value) then
                                    Day_Production_value = node.values[1].value
                                     domoticz.log("i: "..i .. " node.type: " .. node.type .. " Day_Production_value: " ..Day_Production_value, domoticz.LOG_INFO)
                                     domoticz.globalData.Solaredge_Prod_Today =  math.floor(tonumber(Day_Production_value) * 10 + 0.5) / 10     -- met à jour la variable globale
                                 end
                            elseif (node.type == "Consumption") then
                                if (node.values[1].value) then
                                    Day_Consumption_value = node.values[1].value
                                    domoticz.log("i: "..i .. " node.type: " .. node.type .. " Day_Consumption_value: " ..Day_Consumption_value, domoticz.LOG_INFO)
                                    domoticz.globalData.Solaredge_Conso_Today = Day_Consumption_value       -- met à jour la variable globale
                                end
                            elseif (node.type == "FeedIn") then
                                if (node.values[1].value) then
                                    Day_FeedIn_value = node.values[1].value
                                    domoticz.log("i: "..i .. " node.type: " .. node.type .. " Day_FeedIn_value: " ..Day_FeedIn_value, domoticz.LOG_INFO)
                                end
                            elseif (node.type == "Purchased") then
                                if (node.values[1].value) then
                                    Day_Purchased_value = node.values[1].value
                                    domoticz.log("i: "..i .. " node.type: " .. node.type .. " Day_Purchased_value: " ..Day_Purchased_value, domoticz.LOG_INFO)
                                end
                            end
                        end
                        
                    

                        -- mise à jour de données de la consommation sur l'interface domoticz
            			Elect_Consommation.updateElectricity(domoticz.globalData.Solaredge_Conso_Now,domoticz.globalData.Solaredge_Conso_Today)  -- affiche sur compteur electricité conso
            	        -- mise à jour de données de la production sur l'interface domoticz
                        Solar_Production.updateElectricity(domoticz.globalData.Solaredge_Prod_Now,domoticz.globalData.Solaredge_Prod_Today)  -- affiche sur 'Compteur électricité Prod'
                        Day_Pourcent_self_conso = domoticz.utils.round(Day_SelfConsumption_value / Day_Consumption_value * 100, 1)  -- calcul du % avec 1 chiffre derrière la virgule
                        Text_Self_Consommation.updateText(tostring(Day_SelfConsumption_value).." Wh soit "..Day_Pourcent_self_conso.. "% de la consommation du jour (" ..Day_Consumption_value..")")  -- affiche en texte
                        Pourcent_Self_Consommation.updatePercentage(Day_Pourcent_self_conso)  -- affiche en %
                end
                ------------------------  fin  conso et prod du jour  -------------------------------
    
             
            else       -- au cas ou rien n'est correct
    				domoticz.log('Problème dans le retour de la requête', domoticz.LOG_ERROR)
    				domoticz.log(item_solaredge, domoticz.LOG_ERROR)
    		end
		end 

    end
}

and the global_data.lua contents

Code: Select all

return {
       helpers = {},
       data = {
          Solaredge_SiteId = { initial = 468475 },
          Solaredge_APIKey = { initial = 'NDH5U98CIX0F0P14LXYZ7C7TGNB2KLU7' },
          Solaredge_Conso_Now = { initial = 0 },
          Solaredge_Conso_Today = { initial = 0 },
          Solaredge_SelfConso_Today = { initial = 0 },
          Solaredge_SelfConso_Now = { initial = 0 },
          Solaredge_Prod_Today = { initial = 0 },
          Solaredge_Prod_Now = { initial = 0 }

       }
    }
1 - create a virtual material using : Dummy (Does nothing, use for virtual switches only) type
name it as you want
use the inside popup to create the virtual devices
2 - create 2 virtual device with type : kWh, Electricity (instant and counter) one for Production , one for Consumption
name them as you want but with Prod and Cons in the name to differentiate when display
change the IDX in the script: for Prod (for solar Production) - main is 61
change the IDX in the script: for Cons (for your general electricity consumption) - main is 63
3 - create 1 virtual device with type : Electric usage
name it as you want but with SelfCons inside (for self consumption)
note the IDX: SelfCons - main is 64
4 - create 1 virtual device type : Percentage
name it as you want but with % SelfCons inside
change the IDX in the script : main is 66
5 - create 1 virtual device with type: Text
name it as you want but with % selfcons text inside
change the IDX in the script : main is 65
6 - if not already done change IDX 61, 63, 64, 65, 66 to your own one
change apikey and site_id to your own one
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest