[LUA] Récupérer sa conso linky avec ecocompteur legrand

Vous avez créé un script LUA dont vous êtes fier, un .sh génial, un programme Python hors du commun, un Tuto, c'est ici que vous pouvez les partager.
jcligny
Messages : 5
Inscription : 18 mars 2017, 21:27

[LUA] Récupérer sa conso linky avec ecocompteur legrand

Message par jcligny »

Bonjour.

Je vous partage un petit script LUA qui permet de récupérer la consommation de votre linky en téléinfo si vous êtes déjà équipés d'un écocompteur legrand :

Il vous faut un capteur virtuel nommé Linky de type RFXMeter
L'url locale de votre ecocompteur legrand que je vous conseille de rendre statique via DHCP

Je me suis basé que un script existant mais je ne sais plus de qui donc désolé de ne pas donner de source

Code : Tout sélectionner

Debug = "NO"                    -- Turn debugging on ("YES") or off ("NO")

----------------------------------------------
------          Configuration          ------
----------------------------------------------

-- domoticz
local url = 'http://192.168.0.25/data.json'         -- url locale de votre écocompteur legrand
linkyidx = otherdevices_idx['Linky']

local function removeZeros(Id)
   while true do 
      if Id:sub(1,1) == '0' then 
         Id = Id:sub(2)
      else
         break
      end
   end   
   return Id
end

----------------------------------------------
--       Fin de la partie configuration       --
----------------------------------------------

commandArray={}

--import des fontions pour lire le JSON
json = (loadfile "/home/odroid/domoticz/scripts/lua/JSON.lua")()  -- For Linux

local config=assert(io.popen("curl '"..url.."'"))
local location = removeZeros(config:read('*all'))
local trouve = string.find(location,'"conso_base" : ')
local consobase = removeZeros(string.sub(location,trouve+15,trouve +23))
config:close()

if Debug == "YES" then
print(trouve)
print(location)
print(consobase)
end

commandArray['UpdateDevice'] = linkyidx ..'|0|'.. consobase

return commandArray
voici le résultat :

Image
aeronne
Messages : 15
Inscription : 05 avr. 2017, 22:10

Re: Récupérer sa conso linky avec ecocompteur legrand

Message par aeronne »

Bonjour,

Merci pour ce tuto.

J'ai suivi toute les informations à la lettre et j'ai modifié le script lua pour qu'il corresponde à ma configuration:

Code : Tout sélectionner

Debug = "NO" -- Turn debugging on ("YES") or off ("NO")

----------------------------------------------
------ Configuration ------
----------------------------------------------

-- domoticz
local url = 'http://192.168.xxx.xxx/data.json' -- url locale de votre écocompteur legrand
linkyidx = otherdevices_idx['Linky']

local function removeZeros(Id)
while true do
if Id:sub(1,1) == '0' then
Id = Id:sub(2)
else
break
end
end
return Id
end

----------------------------------------------
-- Fin de la partie configuration --
----------------------------------------------

commandArray={}

--import des fontions pour lire le JSON
json = (loadfile "/home/$user/domoticz/scripts/lua/JSON.lua")() -- For Linux

local config=assert(io.popen("curl '"..url.."'"))
local location = removeZeros(config:read('*all'))
local trouve = string.find(location,'"conso_base" : ')
local consobase = removeZeros(string.sub(location,trouve+15,trouve +23))
config:close()

if Debug == "YES" then
print(trouve)
print(location)
print(consobase)
end

commandArray['UpdateDevice'] = linkyidx ..'|0|'.. consobase

return commandArray
Que j'ai appelé script_time_ecoCompteur.lua et mis dans: /home/$user/domoticz/scripts/lua/
Puis j'ai créé un nouveau matériel: Dummy (Does nothing, use for virtual switches only).
J'ai ensuite créer un capteur virtuel nommé "Linky" mais je coince bêtement sur le type rfxmeter. Il n'existe pas dans le menu déroulant des types proposé et j'ai bien un rfxcom mais je ne pense pas que ce soit nécessaire.
En bref, je suis perdu sur le type à ajouter :(

Si quelqu'un à une idée, je suis preneur :)
damolc
Messages : 240
Inscription : 24 juil. 2016, 22:08

Re: Récupérer sa conso linky avec ecocompteur legrand

Message par damolc »

Bonjour moi j ai pris " electrique instantané et compteur"
Mais ce qui serait intéressant dans ce script c est d ajouter "conso_hc" et "conso_hp"
j ai tenté de changer "conso_basal" par l un des deux mais ça ne fonctionne pas
auriez vous une idée?
je ne sais absolument pas programmer
Merci
damolc
Messages : 240
Inscription : 24 juil. 2016, 22:08

Re: Récupérer sa conso linky avec ecocompteur legrand

Message par damolc »

bonjour
quand j interroge Eco compteur legrand avec : http://192.168.1.46/data.json
j ai :

"option_tarifaire" : 1,
"tarif_courant" : 2,
"isousc" : 30,

"conso_base" : 0,
"conso_hc" : 001143021,
"conso_hp" : 003609170,
je cherche a modifier ce script pour récupérer "conso_hc" et "conso_hp"
Pouvez vous m aider dans les modifications ne sachant par programmer?
damolc
Messages : 240
Inscription : 24 juil. 2016, 22:08

Re: Récupérer sa conso linky avec ecocompteur legrand

Message par damolc »

Bonjour
j ai avancé dans le script pour reccuperer HP et HC :

Code : Tout sélectionner

Debug = "YES" -- Turn debugging on ("YES") or off ("NO")

----------------------------------------------
------ Configuration ------
----------------------------------------------

-- domoticz
local url = 'http://192.168.1.xx/data.json' -- url locale de votre écocompteur legrand
hcidx = otherdevices_idx['HeureCreuse']
hpidx = otherdevices_idx['HeurePleine']

local function removeZeros(Id)
while true do
if Id:sub(1,1) == '0' then
Id = Id:sub(2)
else
break
end
end
return Id
end

----------------------------------------------
-- Fin de la partie configuration --
----------------------------------------------

commandArray={}

--import des fontions pour lire le JSON
json = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")() -- For Linux

local config=assert(io.popen("curl '"..url.."'"))
local location = removeZeros(config:read('*all'))
local trouve = string.find(location,'"conso_hc"')
local trouve = string.find(location,'"conso_hp"')
local consoHC = removeZeros(string.sub(location,trouve+15,trouve+23))
local consoHP = removeZeros(string.sub(location,trouve+15,trouve+23))
config:close()

if Debug == "YES" then
print(trouve)
print(location)
print(consoHC)
print(consoHP)

end

commandArray['UpdateDevice'] = hcidx ..'|0|'.. consoHC
commandArray['UpdateDevice'] = hpidx ..'|0|'.. consoHP

return commandArray
mais la valeur de HP n est pas reccuperée?
j ai choisi comme Idx : Compteur
pourriez vous m indiquer ou il y a une erreur dans le script?
Dernière modification par Keros le 16 oct. 2021, 16:19, modifié 1 fois.
Raison : Modification des balises code
denis_brasseur
Messages : 898
Inscription : 24 déc. 2018, 17:05
Localisation : (26)

Re: Récupérer sa conso linky avec ecocompteur legrand

Message par denis_brasseur »

Et que donne ceci :

Code : Tout sélectionner

local trouve = string.find(location,'"conso_hc"')
local consoHC = removeZeros(string.sub(location,trouve+15,trouve+23))

local trouve = string.find(location,'"conso_hp"')
local consoHP = removeZeros(string.sub(location,trouve+15,trouve+23))
Pi3 + DD PiDrive - RFXtrx433 - AEON Labs ZW090
damolc
Messages : 240
Inscription : 24 juil. 2016, 22:08

Re: Récupérer sa conso linky avec ecocompteur legrand

Message par damolc »

non ça ne fonction pas plus
j ai la solution mais elle n est par parfaite : deux scripts lua distincts
j en met un et il faut adapter l autre :

Code : Tout sélectionner

[*]
Debug = "NO" -- Turn debugging on ("YES") or off ("NO")
----------------------------------------------
------ Configuration ------
----------------------------------------------
-- domoticz
local url = 'http://XXX.XXX.X.XX/data.json' -- url locale de votre écocompteur legrand
hcidx = otherdevices_idx['HeureCreuse']
local function removeZeros(Id)
while true do
if Id:sub(1,1) == '0' then
Id = Id:sub(2)
else
break
end
end
return Id
end
----------------------------------------------
-- Fin de la partie configuration --
----------------------------------------------
commandArray={}
--import des fontions pour lire le JSON
json = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")() -- For Linux
local config=assert(io.popen("curl '"..url.."'"))
local location = removeZeros(config:read('*all'))
local trouve = string.find(location,'"conso_hc"')
local consoHC = removeZeros(string.sub(location,trouve+15,trouve +23))
config:close()
if Debug == "YES" then
print(trouve)
print(location)
print(consoHC)
end
commandArray['UpdateDevice'] = hcidx ..'|0|'.. consoHC
return commandArray
damolc
Messages : 240
Inscription : 24 juil. 2016, 22:08

Re: Récupérer sa conso linky avec ecocompteur legrand

Message par damolc »

j ai finalisé le script lua avec 3 devices ( Linky pour le total du courant electrique avec un Idx double valeur : P1 compteur intelligent, et deux Idx compteurs pour HP et HC
tout a l air de bien fonctionner :

Code : Tout sélectionner

Debug = "NO" -- Turn debugging on ("YES") or off ("NO")

----------------------------------------------
------ Configuration ------
----------------------------------------------

-- domoticz
local url = 'http://192.168.1.46/data.json' -- url locale de votre écocompteur legrand
htidx = otherdevices_idx['Linky']           -- Idx P1 Compteur intelligent (électrique)
hcidx = otherdevices_idx['HC']              -- Idx Compteur
hpidx = otherdevices_idx['HP']              -- Idx Compteur

local function removeZeros(Id)
while true do
if Id:sub(1,1) == '0' then
Id = Id:sub(2)
else
break
end
end
return Id
end

----------------------------------------------
-- Fin de la partie configuration --
----------------------------------------------

commandArray={}

--import des fontions pour lire le JSON
json = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")() -- For Linux
--recuperation des mesure HP et HC sur data.json
local config=assert(io.popen("curl '"..url.."'"))
local location = removeZeros(config:read('*all'))
local trouve = string.find(location,'"conso_hp"')
local consoHP = removeZeros(string.sub(location,trouve+15,trouve +23))

local trouve = string.find(location,'"conso_hc"')
local consoHC = removeZeros(string.sub(location,trouve+15,trouve +23))
config:close()

if Debug == "YES" then
print(trouve)
print(location)
print(consoHP)
print(consoHC)
end
--import des valeurs dans les Idx
commandArray[htidx] = { ['UpdateDevice'] = htidx ..'|0|'.. consoHP .. ";" .. consoHC.. ";" .. "0" .. ";" .. "0" .. ";" .. "0" .. ";" .. "0" }
commandArray[hpidx] = { ['UpdateDevice'] = hpidx ..'|0|'.. consoHP }
commandArray[hcidx] = { ['UpdateDevice'] = hcidx ..'|0|'.. consoHC }
 
return commandArray
aeronne
Messages : 15
Inscription : 05 avr. 2017, 22:10

Re: Récupérer sa conso linky avec ecocompteur legrand

Message par aeronne »

@damolc

Merci pour l'info "P1 compteur intelligent, et deux Idx compteurs pour HP et HC""
Pour le reste, super travail! J'ai hâte de trouver 5mn pour tester. Si j'ai bien lu, un script pour trois périphériques qui récupère les infos HC et HP la consommation totale.
Parfait pour moi. Merci
damolc
Messages : 240
Inscription : 24 juil. 2016, 22:08

Re: Récupérer sa conso linky avec ecocompteur legrand

Message par damolc »

Oui c est bien ça ; ;)
Répondre