docker-sensors-polling/read_teleinfo.py

49 lines
1.7 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
2024-02-04 20:19:01 +00:00
# # pip install teleinfo
# https://pypi.org/project/teleinfo/
# https://www.magdiblog.fr/gpio/teleinfo-edf-suivi-conso-de-votre-compteur-electrique/
import argparse
import json
from teleinfo import Parser
from teleinfo.hw_vendors import UTInfo2
2024-06-08 20:00:17 +00:00
import subprocess
2024-02-04 20:19:01 +00:00
parser = argparse.ArgumentParser(description='Téléinfo retriever.')
parser.add_argument("-f", "--format", help="Output format.",
type=str, choices=['human-readable', 'raw_json', 'custom_json'], default='human-readable')
args = parser.parse_args()
2024-06-08 20:00:17 +00:00
find_ttyUSB_output = subprocess.check_output(["./find_ttyUSB.sh"])
device = None
for line in find_ttyUSB_output.decode('utf-8').split("\n"):
if "Cartelectronic_Interface_USB_1_TIC_DA6TLO1X" in line:
device = line.split(" ")[0]
break
if device is None:
print("USB teleinfo device not found.")
2024-06-08 20:00:17 +00:00
sys.exit(1)
ti = Parser(UTInfo2(port=device))
2024-02-04 20:19:01 +00:00
res = ti.get_frame()
if args.format == 'human-readable':
print("Puissance apparente compteur : "+str(int(res['PAPP']))+"VA")
2024-02-04 20:19:01 +00:00
# moins précis car Intensité arrondie à l'entier
print("Puissance apparente calculée : "+str(int(res['IINST'])*230)+"VA")
print("Puissance souscrite : 6kVA")
print("Puissance max avant coupure (marge 30%) : 7,8kVA")
print("Intensité : "+str(int(res['IINST']))+"A")
print("Intensité abonnement : "+str(int(res['ISOUSC']))+"A")
print("Consommation : "+str(int(res['BASE']))+"Wh")
2024-02-04 20:19:01 +00:00
elif args.format == 'raw_json':
print(json.dumps(res))
2024-02-04 20:19:01 +00:00
elif args.format == 'custom_json':
data = {}
data['Modane_elec_main_power'] = int(res['PAPP'])
data['Modane_elec_energy_index'] = int(res['BASE'])
print(json.dumps(data))
2024-02-04 20:19:01 +00:00
#for frame in ti:
# print(frame)