Page 1 of 1

Send Domoticz Energy Usage to Emoncms

Posted: Saturday 13 January 2018 13:42
by febalci
I am using Aoetec Gen5 Home Energy Monitor 3phase to follow my energy usage. Is there a script to export the values to emoncms regularly, so that i can follow up it from emoncms web site, which btw looks much more easier to read instead of domoticz standard graphing.

Thanks...

Re: Send Domoticz Energy Usage to Emoncms

Posted: Monday 15 January 2018 9:45
by febalci
For those who look for the same thing, i found the script in the French Domoticz Web site:

1. You have to enter your emoncms write API key to local API_KEY
2. The commandArray line at the end works for Synology. In your case if it doesn't work, comment out this line and uncomment the os.execute line.
3. If you don't have accented characters in your language, you can comment out sans_accent function and the calls to sans_comment function inside the script.

Code: Select all

--[[script_time_export_emoncms.lua
auteur : papoo

maj : 23/04/2017
date : 18/05/2016
Principle:
export the meter data, temperatures, etc. on the website https://emoncms.org/
The script is read every minute but only exports the data every 2 minutes
http://easydomoticz.com/forum/viewtopic.php?f=17&t=2017
]]--

--------------------------------------------
------------ Variables à éditer ------------
-------------------------------------------- 
local nom_script = "export emoncms"
local version = "2.01"
local debugging = false  	-- true to see the logs in the console log Dz or false to not see them
local url_emoncms = 'http://emoncms.org/api/' 
local API_key = 'YOU EMONCMS KEY' -- create a string variable containing your 32-character Write KEY emoncms API code

local les_devices = {}; -- no more than 11 devices
-- if you want to go up the values of a device which has several of them (ex: temperature and external hygrometry)
--process the name of the value to be raised (from the second value) and the order number in the channel (see examples below les_devices [9] and les_devices [17])
les_devices[1] = {device="Home Watt", nom="", canal=""} -- 1er compteur 
les_devices[2] = {device="kWh Meter", nom="", canal=""} -- 2eme compteur 
les_devices[3] = {device="Voltage", nom="", canal=""}-- 3eme compteur
-- les_devices[4] = {device="Counter Taken", nom="", canal=""}
-- les_devices[5] = {device="Lights Counter", nom="", canal=""}
--les_devices[6] = {device="Technical Counter", nom="", canal=""}
--les_devices[7] = {device="DJU", nom="", canal=""}
--les_devices[8] = {device="Temperature exterieure", nom="", canal=""}
--les_devices[8] = {device="External Temperature", nom="Humidité", canal=2}
--les_devices[9] = {device="Salon Temperature", nom="", canal=""}
--les_devices[10] = {device="1st floor corridor", nom="", canal=""}
--les_devices[11] = {device="Icing point", nom="", canal=""}
--les_devices[12] = {device="Dew point", nom="", canal=""}
--les_devices[13] = {device="Temp départ chauffage", nom="", canal=""}
--les_devices[14] = {device="Temp retour chauffage", nom="", canal=""}
--les_devices[16] = {device="Temp Parents", nom="", canal=""}
--les_devices[17] = {device="anémometre", nom="direction", canal=2}
local fields =""
--------------------------------------------
----------- Fin variables à éditer ---------
--------------------------------------------

--------------------------------------------
---------------- Fonctions -----------------
--------------------------------------------
commandArray = {}
function voir_les_logs (s)
    if (debugging) then 
        print ("<font color='#f3031d'>".. s .."</font>");
    end
end   
function sans_accent(str)
    if (str) then
    str = string.gsub (str,"Ç", "C")
    str = string.gsub (str,"ç", "c")
    str = string.gsub (str,"Ö", "O")
    str = string.gsub (str,"ö", "o")
    str = string.gsub (str,"Ğ", "G")
    str = string.gsub (str,"ğ", "g")
    str = string.gsub (str,"Ü", "U")
    str = string.gsub (str,"ü", "u")
    str = string.gsub (str,"Ş", "S")
    str = string.gsub (str,"ş", "s")
    str = string.gsub (str,"İ", "I")
    str = string.gsub (str,"ı", "i")
     end
    return (str)
end

function split(inputstr, sep)
        if sep == nil then
                sep = "%s"
        end
        local t={} ; i=1
        for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
                t[i] = str
                i = i + 1
        end
        return t
end -- usage : valeurs = split(variable,";")

function urlencode(str)
   if (str) then
   str = string.gsub (str, "\n", "\r\n")
   str = string.gsub (str, "([^%w ])",
   function (c) return string.format ("%%%02X", string.byte(c)) end)
   str = string.gsub (str, " ", "+")
   end
   return str
end 
 
--------------------------------------------
-------------- Fin Fonctions ---------------
--------------------------------------------

commandArray = {}
time=os.date("*t")
if time.min % 2 == 0 then  -- export des données toutes les 2 minutes
voir_les_logs("=========== ".. nom_script .." (v".. version ..") ===========",debugging)
voir_les_logs("--- --- --- Export vers emoncms  : "..time.hour.. ":" ..time.min,debugging)
    for i,d in ipairs(les_devices) do
    v=otherdevices[d.device]
	c=tonumber(d.canal)
			voir_les_logs("--- --- --- ".. sans_accent(d.device) .." = "..(v or "nil"),debugging)
			voir_les_logs("--- --- --- canal = "..(c or "nil"),debugging)
			if v~= nil then v,nbCommas=string.gsub(v,";",";") end-- verification of the presence of one or more semicolon => multiple values
	   if nbCommas > 1 and c ~= nil then
			voir_les_logs("--- --- --- valeurs multiples dans ".. sans_accent(d.device) .." = ".. v,debugging)
			voir_les_logs("--- --- ---  Nb de point virgule = "..(nbCommas or "nil"),debugging) 
			local valeurs = split(v,";")
			voir_les_logs("--- --- ---  valeur 1 = "..(valeurs[1] or "nil"),debugging)
			voir_les_logs("--- --- ---  valeur 2 = "..(valeurs[2] or "nil"),debugging)
			voir_les_logs("--- --- ---  valeur 3 = "..(valeurs[3] or "nil"),debugging)
			voir_les_logs("--- --- ---  valeur 4 = "..(valeurs[4] or "nil"),debugging)
			
		fields = fields .. '{'.. d.nom .. ':' .. valeurs[c] .. '},'
		else 
		fields = fields .. '{'.. d.device .. ':' .. v .. '},'
		end
	end
		fields = string.gsub (fields, ",$", "") -- deleting the last comma
		fields = urlencode(sans_accent(fields)) -- deleting accented characters
		voir_les_logs("--- --- --- ".. url_emoncms ..'post?apikey=' .. API_key .. '&json=' .. fields ,debugging)
	commandArray[1]={['OpenURL']=url_emoncms ..'post?apikey=' .. API_key .. '&json=' .. fields }
	--os.execute('curl -m5 "'..url_emoncms ..'post?apikey=' .. API_key .. '&json=' .. fields)
		voir_les_logs("========= Fin ".. nom_script .." (v".. version ..") =========",debugging)
end -- if time
-- ============================================================================
return commandArray