I made a script based on several I saw already on this forum. Means I am not a programmer...
My script is working finte. It's reading the registers of a modbus from my ventilation system but I want to optimize some points.
1) I would like to know if we can set how often is runing the plugin?
2) I am using Device[1].update() to update my devices. Is there a way to read the values of the devices ? I would like to avoid to write every time the same values. Or would you have such function? I looked at Domoticz-Api but I didn't see a read function. It must be possible with json but I don't so much about json...
Tx
Antoine
Here the code.
Code: Select all
#!/usr/bin/env python
#Drexel und Weiss the Python Modbus plugin for Domoticz.
#Author: Antoine
#Requirements:
# 1.python module minimalmodbus -> http://minimalmodbus.readthedocs.io/en/master/
# (pi@raspberrypi:~$ sudo pip3 install minimalmodbus)
# 2.Communication module Modbus USB to RS232 converter module
# Below is what will be displayed in Domoticz GUI under HW
#
"""
<plugin key="Drexel" name="Drexel und Weiss" version="0.0.1" author="Antoine">
<params>
<param field="SerialPort" label="Modbus Port" width="200px" required="true" default="/dev/ttyUSBRS232" />
<param field="Mode1" label="Baud rate" width="40px" required="true" default="19200" />
<param field="Mode3" label="Reading Interval min." width="40px" required="true" default="1" />
<param field="Mode4" label="Sleep" width="40px" required="true" />
<param field="Mode6" label="Debug" width="75px">
<options>
<option label="True" value="Debug"/>
<option label="False" value="Normal" default="true" />
</options>
</param>
</params>
</plugin>
"""
import Domoticz
import sys
sys.path.append('/usr/local/lib/python3.4/dist-packages')
sys.path.append('/usr/local/lib/python3.5/dist-packages')
import minimalmodbus
import serial
import time
class BasePlugin:
def __init__(self):
return
def onStart(self):
devicecreated = []
Domoticz.Log("Drexel & Weiss plugin start")
self.runInterval = int(Parameters["Mode3"]) * 1
if (len(Devices) == 0):
#For slave ID 1.
Domoticz.Device(Name="Temperature Maison", Unit=1, TypeName="Temperature", Used=1).Create()#200
Domoticz.Device(Name="Temperature Ext", Unit=2, TypeName="Temperature", Used=1).Create()#202
Domoticz.Device(Name="ppm CO2", Unit=3, TypeName="Air Quality", Used=1).Create()#230
Domoticz.Device(Name="Ventillateur Insuflation, heure", Unit=4, TypeName="Text", Used=1).Create()#900
Domoticz.Device(Name="Ventillateur Aspiration, heure", Unit=5, TypeName="Text", Used=1).Create()#902
Domoticz.Device(Name="Heure au niveau 1", Unit=6, TypeName="Text", Used=1).Create()#904
Domoticz.Device(Name="Heure au niveau 2", Unit=7, TypeName="Text", Used=1).Create()#906
Domoticz.Device(Name="Heure au niveau 3", Unit=8, TypeName="Text", Used=1).Create()#908
Domoticz.Device(Name="Heure chauffage au niveau 1", Unit=9, TypeName="Text", Used=1).Create()#912
Domoticz.Device(Name="Heure chauffage au niveau 2", Unit=10, TypeName="Text", Used=1).Create()#914
Domoticz.Device(Name="Filtre grosses particules, heure", Unit=11, TypeName="Text", Used=1).Create()#926
Domoticz.Device(Name="Filtre petites particules, heure", Unit=12, TypeName="Text", Used=1).Create()#928
Domoticz.Device(Name="Fonction dégel, heure", Unit=13, TypeName="Text", Used=1).Create()#940
Domoticz.Device(Name="Vol. air depuis al., m3", Unit=14, TypeName="Text", Used=1).Create()#946
Domoticz.Device(Name="Vol. air depuis ch. filtre, m3", Unit=15, TypeName="Text", Used=1).Create()#960
Domoticz.Device(Name="Heure au niveau 0", Unit=16, TypeName="Text", Used=1).Create()#962
Option1 = {"Scenes": "|||||", "LevelNames": "0|Vent. 1|Vent. 2|Vent. 3|Vent. red.", "LevelOffHidden": "false", "SelectorStyle": "0"}
Domoticz.Device(Name="Pui. Vent. en cours", Unit=17, Type=244, Subtype=62 , Switchtype=18, Options = Option1, Used=1).Create()#1066
Domoticz.Device(Name="Vitesse insuflation, %", Unit=18, TypeName="General",Subtype=0x06, Used=1).Create()#1092
Domoticz.Device(Name="Vitesse aspiration, %", Unit=19, TypeName="General",Subtype=0x06, Used=1).Create()#1094
Domoticz.Device(Name="Surpression insuflation, Pa", Unit=20, TypeName="Pressure", Used=1).Create()#1184
Domoticz.Device(Name="Vitesse Aspiration, rpm", Unit=21, TypeName="Text", Used=1).Create()#1186
Option2 = {"Scenes": "|||||", "LevelNames": "Off|Niveau 1|Niveau 2|Niveau 3|Auto.|Party", "LevelOffHidden": "false", "SelectorStyle": "0"}
Domoticz.Device(Name="Mode de fonctionnement", Unit=22, Type=244, Subtype=62 , Switchtype=18, Options = Option2, Used=1).Create()#5002
Domoticz.Device(Name="Thermostat", Unit=23, Type=242, Subtype=1, Used=1).Create()#5016
Domoticz.Device(Name="Changer filtre grosses particules", Unit=24, TypeName="Switch",Subtype=0x00, Used=1).Create()#7002
Domoticz.Device(Name="Changer filtre petites particules", Unit=25, TypeName="Switch",Subtype=0x00, Used=1).Create()#7004
Domoticz.Device(Name="Chauffage Pui. 1", Unit=26, TypeName="Switch",Used=1).Create()#1032
Domoticz.Device(Name="Chauffage Pui. 2", Unit=27, TypeName="Switch",Used=1).Create()#1034
Domoticz.Device(Name="Eau Chaude Pui. 1", Unit=28, TypeName="Switch",Used=1).Create()#1036
Domoticz.Device(Name="Eau Chaude Pui. 2", Unit=29, TypeName="Switch",Used=1).Create()#1038
#For slave ID 2.
Domoticz.Device(Name="Temperature Chauffeau", Unit=30, TypeName="Temperature", Used=1).Create() #212
Domoticz.Device(Name="Usage moteur comp, heure", Unit=31, TypeName="Text", Used=1).Create() #910
Domoticz.Device(Name="Usage valve mag. gaz liq., heure", Unit=32, TypeName="Text", Used=1).Create() #916
Domoticz.Device(Name="Usage valve mag. gaz, heure", Unit=33, TypeName="Text", Used=1).Create() #918
Domoticz.Device(Name="Usage valve mag. cond. air, heure", Unit=34, TypeName="Text", Used=1).Create() #920
Domoticz.Device(Name="Usage resistance eau chaude", Unit=35, TypeName="Text", Used=1).Create() #922
Domoticz.Device(Name="Pompe à chaleur", Unit=36, TypeName="Switch",Subtype=0x00, Used=1).Create() #1044
Domoticz.Device(Name="Ventilation Evacuation", Unit=37, TypeName="Text", Used=1).Create() #1184
Domoticz.Device(Name="Surchauffe eau", Unit=38, TypeName="Switch",Subtype=0x00, Used=1).Create() #1212
Domoticz.Device(Name="Function Bad+", Unit=39, TypeName="Switch",Subtype=0x00, Used=1).Create() #5036
Domoticz.Device(Name="Function Chauffage+", Unit=40, TypeName="Switch",Subtype=0x00, Used=1).Create() #5492
def onStop(self):
Domoticz.Log("Drexel und Weiss Modbus plugin stop")
def onHeartbeat(self):
# Get data from Drexel
self.runInterval -=1;
if self.runInterval <= 0:
# Get data from Aerosmart L
#Slave ID 1
self.rs232 = minimalmodbus.Instrument(Parameters["SerialPort"],1) #int(Parameters["Mode2"]) à changer pour l'addresse du slave du LU ou WP
self.rs232.serial.baudrate = Parameters["Mode1"]
self.rs232.serial.bytesize = 8
self.rs232.serial.parity = minimalmodbus.serial.PARITY_NONE
self.rs232.serial.stopbits = 1
self.rs232.serial.timeout = 1
self.rs232.debug = True
self.rs232.mode = minimalmodbus.MODE_RTU
TemperatureMaison = self.rs232.read_long(200, functioncode=4, signed=False) #1
time.sleep(float(Parameters["Mode4"]))
TemperatureExt = self.rs232.read_long(202, functioncode=4, signed=False) #2
time.sleep(float(Parameters["Mode4"]))
ppm_CO2 = self.rs232.read_long(230, functioncode=4, signed=True) #3
time.sleep(float(Parameters["Mode4"]))
Vent_Ins_h = self.rs232.read_long(900, functioncode=4, signed=False) #4
time.sleep(float(Parameters["Mode4"]))
Vent_Asp_h = self.rs232.read_long(902, functioncode=4, signed=False) #5
time.sleep(float(Parameters["Mode4"]))
h_niv_1 = self.rs232.read_long(904, functioncode=4, signed=False) #6
time.sleep(float(Parameters["Mode4"]))
h_niv_2 = self.rs232.read_long(906, functioncode=4, signed=False) #7
time.sleep(float(Parameters["Mode4"]))
h_niv_3 = self.rs232.read_long(908, functioncode=4, signed=False) #8
time.sleep(float(Parameters["Mode4"]))
Ch_niv_1 = self.rs232.read_long(912, functioncode=4, signed=False) #9
time.sleep(float(Parameters["Mode4"]))
Ch_niv_2 = self.rs232.read_long(914, functioncode=4, signed=False) #10
time.sleep(float(Parameters["Mode4"]))
h_fil_gr_part = self.rs232.read_long(926, functioncode=4, signed=False) #11
time.sleep(float(Parameters["Mode4"]))
h_fil_pt_part = self.rs232.read_long(928, functioncode=4, signed=False) #12
time.sleep(float(Parameters["Mode4"]))
h_fct_degel = self.rs232.read_long(940, functioncode=4, signed=False) #13
time.sleep(float(Parameters["Mode4"]))
Vol_vent_total = self.rs232.read_long(946, functioncode=4, signed=False) #14
time.sleep(float(Parameters["Mode4"]))
Vol_vent_depuis_chang = self.rs232.read_long(960, functioncode=4, signed=False) #15
time.sleep(float(Parameters["Mode4"]))
h_niv_0 = self.rs232.read_long(962, functioncode=4, signed=False) #16
time.sleep(float(Parameters["Mode4"]))
Puissance_vent = self.rs232.read_long(1066, functioncode=4, signed=True) #21
time.sleep(float(Parameters["Mode4"]))
Vit_insuflation = self.rs232.read_long(1092, functioncode=4, signed=True) #22
time.sleep(float(Parameters["Mode4"]))
Vit_aspiration = self.rs232.read_long(1094, functioncode=4, signed=True) #23
time.sleep(float(Parameters["Mode4"]))
Supr_asp = self.rs232.read_long(1184, functioncode=4, signed=True) #24
time.sleep(float(Parameters["Mode4"]))
Vit_asp_rpm = self.rs232.read_long(1186, functioncode=4, signed=True) #25
time.sleep(float(Parameters["Mode4"]))
Mode_fonct = self.rs232.read_long(5002, functioncode=4, signed=True) #26
time.sleep(float(Parameters["Mode4"]))
TemperatureConsigneChauffage = self.rs232.read_long(5016, functioncode=4, signed=False) #27
time.sleep(float(Parameters["Mode4"]))
Ch_fil_gro_part = self.rs232.read_long(7002, functioncode=4, signed=True) #28
time.sleep(float(Parameters["Mode4"]))
Ch_fil_pet_part = self.rs232.read_long(7004, functioncode=4, signed=True) #29
time.sleep(float(Parameters["Mode4"]))
Ch1_en_cours = self.rs232.read_long(1032, functioncode=4, signed=True) #17 signé?
time.sleep(float(Parameters["Mode4"]))
Ch2_en_cours = self.rs232.read_long(1034, functioncode=4, signed=True) #18
time.sleep(float(Parameters["Mode4"]))
Ch_eau_pompe_ch1 = self.rs232.read_long(1036, functioncode=4, signed=True) #19
time.sleep(float(Parameters["Mode4"]))
Ch_eau_pompe_ch2 = self.rs232.read_long(1038, functioncode=4, signed=True) #20
time.sleep(float(Parameters["Mode4"]))
#Slave ID 2
# Change of slave ID
self.rs232 = minimalmodbus.Instrument(Parameters["SerialPort"], 2) #int(Parameters["Mode2"]) à changer pour l'addresse du slave du LU ou WP
self.rs232.serial.baudrate = Parameters["Mode1"]
self.rs232.serial.bytesize = 8
self.rs232.serial.parity = minimalmodbus.serial.PARITY_NONE
self.rs232.serial.stopbits = 1
self.rs232.serial.timeout = 1
self.rs232.mode = minimalmodbus.MODE_RTU
TemperatureChauffeau = self.rs232.read_long(212, functioncode=4, signed=False) #30
time.sleep(float(Parameters["Mode4"]))
Usage_comp_mot_h = self.rs232.read_long(910, functioncode=4, signed=False) #31
time.sleep(float(Parameters["Mode4"]))
Usage_valve_gaz_liq_h = self.rs232.read_long(916, functioncode=4, signed=False) #32
time.sleep(float(Parameters["Mode4"]))
Usage_valve_gaz_h = self.rs232.read_long(918, functioncode=4, signed=False) #33
time.sleep(float(Parameters["Mode4"]))
Usage_valve_gaz_air_h = self.rs232.read_long(920, functioncode=4, signed=False) #34
time.sleep(float(Parameters["Mode4"]))
Usage_resistance_eau = self.rs232.read_long(922, functioncode=4, signed=False) #35
time.sleep(float(Parameters["Mode4"]))
Pompe_Chaleur = self.rs232.read_long(1044, functioncode=4, signed=True) #36
time.sleep(float(Parameters["Mode4"]))
VentilationEvacation = self.rs232.read_long(1184, functioncode=4, signed=False) #37
time.sleep(float(Parameters["Mode4"]))
Surch_eau = self.rs232.read_long(1212, functioncode=4, signed=True) #38
time.sleep(float(Parameters["Mode4"]))
Bad_plus = self.rs232.read_long(5036, functioncode=4, signed=True) #39
time.sleep(float(Parameters["Mode4"]))
Chauffage_plus = self.rs232.read_long(5492, functioncode=4, signed=True) #40
#Update devices
Devices[1].Update(0,str(TemperatureMaison/1000))
Devices[2].Update(0,str(TemperatureExt/1000))
Devices[3].Update(int(ppm_CO2),str(ppm_CO2))
Devices[4].Update(0,str(round(Vent_Ins_h/60)))
Devices[5].Update(0,str(round(Vent_Asp_h/60)))
Devices[6].Update(0,str(round(h_niv_1/60)))
Devices[7].Update(0,str(round(h_niv_2/60)))
Devices[8].Update(0,str(round(h_niv_3/60)))
Devices[9].Update(0,str(round(Ch_niv_1/60)))
Devices[10].Update(0,str(round(Ch_niv_2/60)))
Devices[11].Update(0,str(round(h_fil_gr_part/60)))
Devices[12].Update(0,str(round(h_fil_pt_part/60)))
Devices[13].Update(0,str(round(h_fct_degel/60)))
Devices[14].Update(0,str(Vol_vent_total))
Devices[15].Update(0,str(Vol_vent_depuis_chang))
Devices[16].Update(0,str(round(h_niv_0/60)))
Devices[17].Update(0,str(Puissance_vent*10))
Devices[18].Update(0,str(Vit_insuflation/100))
Devices[19].Update(0,str(Vit_aspiration/100))
Devices[20].Update(0,str(Supr_asp/1000))
Devices[21].Update(0,str(Vit_asp_rpm))
Devices[22].Update(0,str(Mode_fonct*10))
Devices[23].Update(0,str(TemperatureConsigneChauffage/1000))
Devices[24].Update(0,str(Ch_fil_gro_part))
Devices[25].Update(0,str(Ch_fil_pet_part))
Devices[26].Update(int(Ch1_en_cours),str(Ch1_en_cours*100))
Devices[27].Update(int(Ch2_en_cours),str(Ch2_en_cours*100))
Devices[28].Update(int(Ch_eau_pompe_ch1),str(Ch_eau_pompe_ch1*100))
Devices[29].Update(int(Ch_eau_pompe_ch2),str(Ch_eau_pompe_ch2*100))
#Slave ID 2
Devices[30].Update(0,str(TemperatureChauffeau/1000))
Devices[31].Update(0,str(round(Usage_comp_mot_h/60)))
Devices[32].Update(0,str(round(Usage_valve_gaz_liq_h/60)))
Devices[33].Update(0,str(round(Usage_valve_gaz_h/60)))
Devices[34].Update(0,str(round(Usage_valve_gaz_air_h/60)))
Devices[35].Update(0,str(round(Usage_resistance_eau/60)))
Devices[36].Update(int(Pompe_Chaleur),str(Pompe_Chaleur*100))
Devices[37].Update(0,str(VentilationEvacation))
Devices[38].Update(int(Surch_eau),str(Surch_eau*100))
Devices[39].Update(int(Bad_plus),str((Bad_plus)*100))
Devices[40].Update(int(Chauffage_plus),str(Chauffage_plus*100))
if Parameters["Mode6"] == 'Debug':
Domoticz.Log("Drexel und Weiss")
Domoticz.Log('TemperatureMaison: {0:.3f} °C'.format(TemperatureMaison))
Domoticz.Log('TemperatureExt: {0:.3f} °C'.format(TemperatureExt))
Domoticz.Log('ppm_CO2: {0:.3f} ppm'.format(ppm_CO2))
Domoticz.Log('Vent_Ins_h: {0:.3f} h'.format(Vent_Ins_h))
Domoticz.Log('Vent_Asp_h: {0:.3f} h'.format(Vent_Asp_h))
Domoticz.Log('h_niv_1: {0:.3f} h'.format(h_niv_1))
Domoticz.Log('h_niv_2: {0:.3f} h'.format(h_niv_2))
Domoticz.Log('h_niv_3: {0:.3f} h'.format(h_niv_3))
Domoticz.Log('Ch_niv_1: {0:.3f} h'.format(Ch_niv_1))
Domoticz.Log('Ch_niv_2: {0:.3f} h'.format(Ch_niv_2))
Domoticz.Log('h_fil_gr_part: {0:.3f} h'.format(h_fil_gr_part))
Domoticz.Log('h_fil_pt_part: {0:.3f} h'.format(h_fil_pt_part))
Domoticz.Log('h_fct_degel: {0:.3f} h'.format(h_fct_degel))
Domoticz.Log('Vol_vent_total: {0:.3f} m3'.format(Vol_vent_total))
Domoticz.Log('Vol_vent_depuis_chang: {0:.3f} m3'.format(Vol_vent_depuis_chang))
Domoticz.Log('h_niv_0: {0:.3f} h'.format(h_niv_0))
Domoticz.Log('Puissance_vent: {0:.3f} step'.format(Puissance_vent))
Domoticz.Log('Vit_insuflation: {0:.3f} %'.format(Vit_insuflation))
Domoticz.Log('Vit_aspiration: {0:.3f} %'.format(Vit_aspiration))
Domoticz.Log('Supr_asp: {0:.3f} Pa'.format(Supr_asp))
Domoticz.Log('Vit_asp_rpm: {0:.3f} rpm'.format(Vit_asp_rpm))
Domoticz.Log('Mode_fonct: {0:.3f} step'.format(Mode_fonct))
Domoticz.Log('TemperatureConsigneChauffage: {0:.3f} °C'.format(TemperatureConsigneChauffage))
Domoticz.Log('Ch_fil_gro_part: {0:.3f} On/Off'.format(Ch_fil_gro_part))
Domoticz.Log('Ch_fil_pet_part: {0:.3f} On/Off'.format(Ch_fil_pet_part))
Domoticz.Log('Ch_fil_pet_part: {0:.3f} On/Off'.format(Ch_fil_pet_part))
Domoticz.Log('Ch1_en_cours: {0:.3f} On/Off'.format(Ch1_en_cours))
Domoticz.Log('Ch2_en_cours: {0:.3f} On/Off'.format(Ch2_en_cours))
Domoticz.Log('Ch_eau_pompe_ch1: {0:.3f} On/Off'.format(Ch_eau_pompe_ch1))
Domoticz.Log('Ch_eau_pompe_ch2: {0:.3f} On/Off'.format(Ch_eau_pompe_ch2))
Domoticz.Log('TemperatureChauffeau: {0:.3f} °C'.format(TemperatureChauffeau))
Domoticz.Log('Usage_comp_mot_h: {0:.3f} h'.format(Usage_comp_mot_h))
Domoticz.Log('Usage_valve_gaz_liq_h: {0:.3f} h'.format(Usage_valve_gaz_liq_h))
Domoticz.Log('Usage_valve_gaz_h: {0:.3f} h'.format(Usage_valve_gaz_h))
Domoticz.Log('Usage_valve_gaz_air_h: {0:.3f} h'.format(Usage_valve_gaz_air_h))
Domoticz.Log('Usage_resistance_eau: {0:.3f} h'.format(Usage_resistance_eau))
Domoticz.Log('Pompe_Chaleur: {0:.3f} On/Off'.format(Pompe_Chaleur))
Domoticz.Log('VentilationEvacation: {0:.3f} rpm'.format(VentilationEvacation))
Domoticz.Log('Surch_eau: {0:.3f} rpm'.format(Surch_eau))
Domoticz.Log('Bad_plus: {0:.3f} rpm'.format(Bad_plus))
Domoticz.Log('Chauffage_plus: {0:.3f} rpm'.format(Chauffage_plus))
self.runInterval = int(Parameters["Mode3"]) * 6
global _plugin
_plugin = BasePlugin()
def onStart():
global _plugin
_plugin.onStart()
def onStop():
global _plugin
_plugin.onStop()
def onHeartbeat():
global _plugin
_plugin.onHeartbeat()
# Generic helper functions
def DumpConfigToLog():
for x in Parameters:
if Parameters[x] != "":
Domoticz.Debug("'" + x + "':'" + str(Parameters[x]) + "'")
Domoticz.Debug("Device count: " + str(len(Devices)))
for x in Devices:
Domoticz.Debug("Device: " + str(x) + " - " + str(Devices[x]))
Domoticz.Debug("Device ID: '" + str(Devices[x].ID) + "'")
Domoticz.Debug("Device Name: '" + Devices[x].Name + "'")
Domoticz.Debug("Device nValue: " + str(Devices[x].nValue))
Domoticz.Debug("Device sValue: '" + Devices[x].sValue + "'")
Domoticz.Debug("Device LastLevel: " + str(Devices[x].LastLevel))
return