Page 2 sur 3

Re: Récupération donnée JSON

Publié : 15 mars 2018, 13:22
par andromede
Pour le site web, tu dois t'authentifier avec le nom et pas avec l'adresse mail.

Re: Récupération donnée JSON

Publié : 15 mars 2018, 13:44
par papoo
un petit truc vite fait juste pour voir la remontée des infos dans les logs domoticz
on verra plus tard pour le reste

Code : Tout sélectionner

--[[
/home/pi/domoticz/scripts/lua/script_time_pellets.lua
auteur : papoo
date de création : 14/03/2018
Date de mise à jour : 15/03/2018
Principe : 

/!\attention/!\
si vous souhaitez utiliser ce script dans l'éditeur interne, pour indiquer le chemin complet vers le fichier JSON.lua, il vous faudra changer la ligne 
json = assert(loadfile(luaDir..'JSON.lua'))()
par 
json = assert(loadfile('/le/chemin/vers/le/fichier/lua/JSON.lua'))()
exemple :
json = assert(loadfile('/home/pi/domoticz/scripts/lua/JSON.lua'))()
la reconnaissance automatique du chemin d'exécution de ce script ne fonctionnant pas dans l'éditeur interne
--]]
--------------------------------------------
------------ Variables à éditer ------------
-------------------------------------------- 

local debugging = true  			        -- 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 = 1                            -- délai d'exécution de ce script en minutes de 1 à 59 (délai entre deux appels à l'API)
local url_info_pellets = "https://www.ecopellets.fr/appjson.php?uniqid="   -- Adresse de l'API ecopellets
local uniqid = "c49f8579b9aec326eac372e8a70a9f"             -- votre uniqid
local dev_qtemois = "Consommation mois en cours"
local dev_prixmois = "Coût mensuel"
local dev_tendance = "Tendance"
local dev_stock0 = "Stock zéro"
local dev_datestock0 = "Date stock zéro"
local dev_qtelastmonth = "Quantité mois passé"
local dev_prixlastmonth = "Coût mois passé"
local dev_qtesept = "Quantité depuis septembre"
local dev_coutsept = "Coût depuis septembre"
local dev_qtestock = "État du stock"
local dev_prixstock = "Coût du stock"
local dev_coutotal = "Coût total"
local dev_coutreparation = "Coût réparation"           
local dev_coutentretien = "Coût entretien"                                            
                                            
                                            --------------------------------------------
----------- Fin variables à éditer ---------
--------------------------------------------
--------------------------------------------
------------- Autres Variables -------------
--------------------------------------------
local nom_script = 'Infos ecopellets.fr'
local version = '0.2'


curl = '/usr/bin/curl -m 5 '		 	-- ne pas oublier l'espace à la fin

-- chemin vers le dossier lua
	if (package.config:sub(1,1) == '/') then
		 luaDir = debug.getinfo(1).source:match("@?(.*/)")
	else
		 luaDir = string.gsub(debug.getinfo(1).source:match("@?(.*\\)"),'\\','\\\\')
	end
	json = assert(loadfile(luaDir..'JSON.lua'))()						-- chargement du fichier JSON.lua
--------------------------------------------
----------- Fin Autres Variables -----------
--------------------------------------------	
--------------------------------------------
---------------- Fonctions -----------------
-------------------------------------------- 
function voir_les_logs (s, debugging)
    if (debugging) then 
		if s ~= nil then
        print ("<font color='#f3031d'>".. s .."</font>")
		else
		print ("<font color='#f3031d'>aucune valeur affichable</font>")
		end
    end
end	

--------------------------------------------
-------------- Fin Fonctions ---------------
--------------------------------------------
commandArray = {}
time = os.date("*t")

if script_actif == true then
    if ((time.min-1) % delai) == 0 then -- toutes les xx minutes en commençant par xx:01    
        voir_les_logs("=========== ".. nom_script .." (v".. version ..") ===========",debugging)
        if otherdevices[dev_qtemois] == nil then creaDev(dev_qtemois, 5) end
        --=========== Lecture json ===============--

        local config = assert(io.popen(curl..' "'.. url_info_pellets .. uniqid ..'"')) 
        --end
        local blocjson = config:read('*all')
        config:close()
        local jsonValeur = json:decode(blocjson)
        if jsonValeur then
            local qtemois = jsonValeur.qtemois
            local prixmois = jsonValeur.prixmois
            local tendance = jsonValeur.tendance
            local stock0 = jsonValeur.stock0
            local datestock0 = jsonValeur.datestock0
            local qtelastmonth = jsonValeur.qtelastmonth
            local prixlastmonth = jsonValeur.prixlastmonth
            local qtesept = jsonValeur.qtesept
            local coutsept = jsonValeur.coutsept
            local qtestock = jsonValeur.qtestock
            local prixstock = jsonValeur.prixstock
            local coutotal = jsonValeur.coutotal
            local coutreparation = jsonValeur.coutreparation            
            local coutentretien = jsonValeur.coutentretien             
            if qtemois ~= nil then voir_les_logs('--- --- --- qtemois : '..qtemois,debugging) end
            if prixmois ~= nil then  voir_les_logs('--- --- --- prixmois : '..prixmois,debugging) end
            if tendance ~= nil then voir_les_logs('--- --- --- tendance : '..tendance,debugging) end
            if stock0 ~= nil then voir_les_logs('--- --- --- stock0 : '..stock0,debugging) end
            if datestock0 ~= nil then voir_les_logs('--- --- --- datestock0 : '..datestock0,debugging) end
            if qtelastmonth ~= nil then voir_les_logs('--- --- --- qtelastmonth : '..qtelastmonth,debugging) end
            if prixlastmonth ~= nil then voir_les_logs('--- --- --- prixlastmonth : '..prixlastmonth,debugging) end
            if qtesept ~= nil then voir_les_logs('--- --- --- qtesept : '..qtesept,debugging) end
            if coutsept ~= nil then voir_les_logs('--- --- --- coutsept : '..coutsept,debugging) end
            if qtestock ~= nil then voir_les_logs('--- --- --- qtestock : '..qtestock,debugging) end
            if prixstock ~= nil then voir_les_logs('--- --- --- prixstock : '..prixstock,debugging) end
            if coutotal ~= nil then voir_les_logs('--- --- --- coutotal : '..coutotal,debugging) end          
            if coutreparation ~= nil then voir_les_logs('--- --- --- coutreparation : '..coutreparation,debugging) 
            else
                coutreparation = 0
                voir_les_logs('--- --- --- coutreparation : '..coutreparation,debugging) 
            end             
            if coutentretien  ~= nil then  
                voir_les_logs('--- --- --- coutentretien : '..coutentretien,debugging) 
            else 
                coutentretien = 0 
                voir_les_logs('--- --- --- coutentretien : '..coutentretien,debugging) 
            end             

        else
            voir_les_logs('--- --- --- aucun résultat à décoder',debugging)
        end --if jsonValeur
    -- ====================================================================================================================	

    voir_les_logs("======== Fin ".. nom_script .." (v".. version ..") ==========",debugging)        
    end --if time       
end -- if script_actif
return commandArray

Re: Récupération donnée JSON

Publié : 15 mars 2018, 19:01
par andromede
un petit truc vite fait !!!
cela m’étonnera toujours :o :o

bref, je viens d'intégrer le script et il bloque ici :

--------------------------------------------
function voir_les_logs (s, debugging)
if (debugging) then
if s ~= nil then
print ("".. s .."")
else
print ("aucune valeur affichable

Re: Récupération donnée JSON

Publié : 15 mars 2018, 19:07
par andromede
ok, ma faute, j'ai mal lu l'avertissement en début de script :oops:

Voici ce qu'il me marque :

2018-03-15 19:06:00.488 LUA: =========== Infos ecopellets.fr (v0.2) ===========
2018-03-15 19:06:00.488 Error: EventSystem: in script_time_pellets: [string "--[[..."]:85: attempt to call global 'creaDev' (a nil value)

Re: Récupération donnée JSON

Publié : 15 mars 2018, 19:13
par papoo
Supprime la ligne

Code : Tout sélectionner

   if otherdevices[dev_qtemois] == nil then creaDev(dev_qtemois, 5) end
J’ai été trop vite dans le nettoyage du script

Re: Récupération donnée JSON

Publié : 15 mars 2018, 19:16
par andromede
Efficace et réactif, merci papoo

voici le résultat !!

2018-03-15 19:15:00.137 LUA: =========== Infos ecopellets.fr (v0.2) ===========
2018-03-15 19:15:00.502 LUA: --- --- --- qtemois : 5
2018-03-15 19:15:00.502 LUA: --- --- --- prixmois : 0
2018-03-15 19:15:00.502 LUA: --- --- --- tendance : 0,29
2018-03-15 19:15:00.502 LUA: --- --- --- stock0 : 34
2018-03-15 19:15:00.502 LUA: --- --- --- datestock0 : 18/04/2018
2018-03-15 19:15:00.502 LUA: --- --- --- qtelastmonth : 6
2018-03-15 19:15:00.502 LUA: --- --- --- prixlastmonth : 22.25
2018-03-15 19:15:00.502 LUA: --- --- --- qtesept : 11
2018-03-15 19:15:00.502 LUA: --- --- --- coutsept : 22.25
2018-03-15 19:15:00.502 LUA: --- --- --- qtestock : 10
2018-03-15 19:15:00.502 LUA: --- --- --- prixstock : 0
2018-03-15 19:15:00.502 LUA: --- --- --- coutotal : 22.25
2018-03-15 19:15:00.502 LUA: --- --- --- coutreparation : 0
2018-03-15 19:15:00.502 LUA: --- --- --- coutentretien : 0
2018-03-15 19:15:00.502 LUA: ======== Fin Infos ecopellets.fr (v0.2) ==========

Re: Récupération donnée JSON

Publié : 16 mars 2018, 09:13
par andromede
Je ne suis vraiment pas bon en script, pour la suite, je rajoute ces lignes à la fin du script ?

# On envoie les variables vers les compteurs domoticz :
curl -s "http://192.168.0.56:8080/json.htm?type= ... ue=$stock0"
curl -s "http://192.168.0.56:8080/json.htm?type= ... datestock0"

Re: Récupération donnée JSON

Publié : 16 mars 2018, 12:28
par papoo
Je m’en occupe ce week-end là je suis un peu charrette
Sinon le plus simple ce sont les commandarray UpdateDevice
https://www.domoticz.com/wiki/LUA_commands

Re: Récupération donnée JSON

Publié : 16 mars 2018, 13:51
par andromede
Je vais jouer un peu avec ça, en attendant ton expertise.

Encore un grand merci papoo.

Re: Récupération donnée JSON

Publié : 17 mars 2018, 21:32
par papoo
quel est le type de device à créer pour chaque information ?

qtemois =
prixmois =
tendance =
stock0 =
datestock0 =
qtelastmonth =
prixlastmonth =
qtesept =
coutsept =
qtestock =
prixstock =
coutotal =
coutreparation =
coutentretien =