I have read the docu from here, but cannot find an answer to my topic.
I have a battery which consists of battery packs and packs consists of cells. I want to store the cell voltages in home-assistant. Voltages are send out with:
BatteryPack/%packnumer/%cellnumber, so p.ex.
BatteryPack/1/4 = 5.839
I cannot get it running with the packnumber and cellnumber separated by “/”.
I have written this code to test the functionality:
import paho.mqtt.client as paho
import time
import random
broker = "192.168.103.44"
port = 1883
# homeassistant/sensor/ESP_Easy_Pufferspeicher/Puffer_unten/config
# { "name": "Puffer unten temperature", "unique_id": "Puffer_unten_temperature", "object_id": "Puffer_unten_temperature", "icon": "mdi:thermometer-lines", "state_topic": "ESP_Easy_Pufferspeicher/Puffer_unten/temperature", "unit_of_measurement": "°C", "retain": "true", "device": { "name": "ESP_Easy_Pufferspeicher", "manufacturer": "Bard", "model": "ESP", "identifiers": "ESP-Eigenbau" } }
# BatteryPack/1/0
if __name__ == '__main__':
client = paho.Client("control") # create client object
client.connect(broker, port) # establish connection
packnumber = 6
cellnumber = 12
config_send = 1
data_send = 1
if config_send:
for pack in range(0, packnumber-1):
print("Send out config")
for cell in range(0, cellnumber-1):
topic = "homeassistant/sensor/BatteryPack/" + str(pack) + "/config"
name = "BMW_Battery_Pack_"+ str(pack) + "_Cell_" + str(cell)
state_topic = "BatteryPack/" + str(pack)
message = '{ "name": "'+ name + '", "unique_id": "'+ name + '", "object_id": "' + name + '", "icon": "mdi:thermometer-lines", "state_topic": "' + state_topic + '", "unit_of_measurement": "mV", "retain": "true", "device": { "name": "BMW Akku Jonas2", "manufacturer": "Bard", "model": "unknown", "identifiers": "Jonas-Eigenbau" } }'
try:
ret = client.publish(topic, message) # publish
print(topic, message)
except Exception as e:
print(e)
pass
time.sleep(0.1)
print("Send out data")
for pack in range(0, packnumber-1):
for cell in range(0, cellnumber-1):
topic = "BatteryPack/" + str(pack)
message = round(random.random()*500,1)
try:
ret = client.publish(topic, message) # publish
print(topic, message)
except Exception as e:
print(e)
pass
time.sleep(0.3)
This code works, but does not contain the functionality as explained above, I only have one level (battery packs) here.
This is the intended code, but not working:
import paho.mqtt.client as paho
import time
import random
broker = "192.168.103.44"
port = 1883
# homeassistant/sensor/ESP_Easy_Pufferspeicher/Puffer_unten/config
# { "name": "Puffer unten temperature", "unique_id": "Puffer_unten_temperature", "object_id": "Puffer_unten_temperature", "icon": "mdi:thermometer-lines", "state_topic": "ESP_Easy_Pufferspeicher/Puffer_unten/temperature", "unit_of_measurement": "°C", "retain": "true", "device": { "name": "ESP_Easy_Pufferspeicher", "manufacturer": "Bard", "model": "ESP", "identifiers": "ESP-Eigenbau" } }
# BatteryPack/1/0
if __name__ == '__main__':
client = paho.Client("control") # create client object
client.connect(broker, port) # establish connection
packnumber = 6
cellnumber = 12
config_send = 1
data_send = 1
if config_send:
for pack in range(0, packnumber-1):
print("Send out config")
for cell in range(0, cellnumber-1):
topic = "homeassistant/sensor/BatteryPack/" + str(pack) + str(cell) + "/config"
name = "BMW_Battery_Pack_"+ str(pack) + "_Cell_" + str(cell)
state_topic = "BatteryPack/" + str(pack) + "/" + str(cell)
message = '{ "name": "'+ name + '", "unique_id": "'+ name + '", "object_id": "' + name + '", "icon": "mdi:thermometer-lines", "state_topic": "' + state_topic + '", "unit_of_measurement": "mV", "retain": "true", "device": { "name": "BMW Akku Jonas2", "manufacturer": "Bard", "model": "unknown", "identifiers": "Jonas-Eigenbau" } }'
try:
ret = client.publish(topic, message) # publish
print(topic, message)
except Exception as e:
print(e)
pass
time.sleep(0.1)
print("Send out data")
for pack in range(0, packnumber-1):
for cell in range(0, cellnumber-1):
topic = "BatteryPack/" + str(pack) + "/" + str(cell)
message = round(random.random()*500,1)
try:
ret = client.publish(topic, message) # publish
print(topic, message)
except Exception as e:
print(e)
pass
time.sleep(0.3)