From ca3e6b42d395874183d5b8f62ed5ef2b09336fb6 Mon Sep 17 00:00:00 2001 From: Privatecoder <45964815+Privatecoder@users.noreply.github.com> Date: Wed, 24 Jan 2024 17:47:40 +0100 Subject: [PATCH] add bash-script to combine all yaml-files created by create_ha_sensors.py, add min_cell_voltage and max_cell_voltage to telemetry feedback and home assistant telemetry sensor creation, adjusted suggested_display_precision for min_pack_voltage and max_pack_voltage to 2 --- ha-sample/combine-yaml-files.sh | 43 +++++++++++++++++++++++++++++++++ ha-sample/create_ha_sensors.py | 28 ++++++++++++++++----- src/fetch_bms_data.py | 32 +++++++++++++----------- 3 files changed, 83 insertions(+), 20 deletions(-) create mode 100755 ha-sample/combine-yaml-files.sh diff --git a/ha-sample/combine-yaml-files.sh b/ha-sample/combine-yaml-files.sh new file mode 100755 index 0000000..4e7e186 --- /dev/null +++ b/ha-sample/combine-yaml-files.sh @@ -0,0 +1,43 @@ +#!/bin/bash + +# This script concatenates YAML files starting with 'telemetry' into a single file. + +# Define the output file +combined_telemetry="combined_telemetry.yaml" +combined_telesignalization="combined_telesignalization.yaml" + +# Check if the output file already exists +if [ -f "$combined_telemetry" ]; then + echo "$combined_telemetry already exists. Removing it." + rm "$combined_telemetry" +fi + +# Check if the output file already exists +if [ -f "$combined_telesignalization" ]; then + echo "$combined_telesignalization already exists. Removing it." + rm "$combined_telesignalization" +fi + +# Loop through all yaml files starting with 'telemetry' and append them to the output file +for file in telemetry*.yaml; do + if [ -f "$file" ]; then + echo "Appending $file to $combined_telemetry" + cat "$file" >> "$combined_telemetry" + # Add a newline to separate files + echo "" >> "$combined_telemetry" + fi +done + +echo "Telemetry files have been concatenated into $combined_telemetry" + +# Loop through all yaml files starting with 'telemetry' and append them to the output file +for file in telesignalization*.yaml; do + if [ -f "$file" ]; then + echo "Appending $file to $combined_telesignalization" + cat "$file" >> "$combined_telesignalization" + # Add a newline to separate files + echo "" >> "$combined_telesignalization" + fi +done + +echo "Telesignalization files have been concatenated into $combined_telesignalization" diff --git a/ha-sample/create_ha_sensors.py b/ha-sample/create_ha_sensors.py index 0063efb..4a78ff6 100644 --- a/ha-sample/create_ha_sensors.py +++ b/ha-sample/create_ha_sensors.py @@ -31,15 +31,31 @@ def main(): "value_template_key": "min_pack_voltage", "device_class": "voltage", "unit_of_measurement": "V", - "suggested_display_precision": 1, - "icon": "mdi:battery" + "suggested_display_precision": 2, + "icon": "mdi:server" }, { "name": "Max Pack Voltage", "value_template_key": "max_pack_voltage", "device_class": "voltage", "unit_of_measurement": "V", - "suggested_display_precision": 1, + "suggested_display_precision": 2, + "icon": "mdi:server" + }, + { + "name": "Min Cell Voltage", + "value_template_key": "min_cell_voltage", + "device_class": "voltage", + "unit_of_measurement": "V", + "suggested_display_precision": 3, + "icon": "mdi:battery" + }, + { + "name": "Max Cell Voltage", + "value_template_key": "max_cell_voltage", + "device_class": "voltage", + "unit_of_measurement": "V", + "suggested_display_precision": 3, "icon": "mdi:battery" }, { @@ -82,7 +98,7 @@ def main(): "device_class": "voltage", "unit_of_measurement": "V", "suggested_display_precision": 3, - "icon": "mdi:battery" + "icon": "mdi:delta" }, { "name": "Ambient Temperature", @@ -122,7 +138,7 @@ def main(): "device_class": "voltage", "unit_of_measurement": "V", "suggested_display_precision": 2, - "icon": "mdi:battery" + "icon": "mdi:server" }, { "name": "Rated Capacity", @@ -594,7 +610,7 @@ def create_similar_sensors( file.write(f"\n# End Telemetry Sensors Seplos Pack-{pack_no}") print(f"{filename} created successfully.") - for pack_no in range(1, number_of_packs + 1): + for pack_no in range(0, number_of_packs): # create multiple cell-voltage-warning sensors cell_voltage_warning_sensors = create_similar_sensors( num_sensors=16, diff --git a/src/fetch_bms_data.py b/src/fetch_bms_data.py index d3a20d0..f8df0bc 100644 --- a/src/fetch_bms_data.py +++ b/src/fetch_bms_data.py @@ -733,16 +733,16 @@ def encode_cmd(self, address: int, cid2: int = None, info: bytes = b"01") -> byt return encoded # get cell with the lowest voltage - def get_min_cell(self) -> dict: - min_cell = self.telemetry.cell_voltage.index(min(self.telemetry.cell_voltage)) - min_cell_voltage = self.telemetry.cell_voltage[min_cell] - return { "min_cell": min_cell, "min_cell_voltage": min_cell_voltage } + def get_lowest_cell(self) -> dict: + lowest_cell = self.telemetry.cell_voltage.index(min(self.telemetry.cell_voltage)) + lowest_cell_voltage = self.telemetry.cell_voltage[lowest_cell] + return { "lowest_cell": lowest_cell, "lowest_cell_voltage": lowest_cell_voltage } # get cell with the highest voltage - def get_max_cell(self) -> dict: - max_cell = self.telemetry.cell_voltage.index(max(self.telemetry.cell_voltage)) - max_cell_voltage = self.telemetry.cell_voltage[max_cell] - return { "max_cell": max_cell, "max_cell_voltage": max_cell_voltage } + def get_highest_cell(self) -> dict: + highest_cell = self.telemetry.cell_voltage.index(max(self.telemetry.cell_voltage)) + highest_cell_voltage = self.telemetry.cell_voltage[highest_cell] + return { "highest_cell": highest_cell, "highest_cell_voltage": highest_cell_voltage } # decode battery pack telemetry feedback frame def decode_telemetry_feedback_frame(self, data) -> dict: @@ -764,6 +764,10 @@ def decode_telemetry_feedback_frame(self, data) -> dict: soh_offset = 126 port_voltage_offset = 130 + # set min and max pack voltage + telemetry_feedback["min_cell_voltage"] = MIN_CELL_VOLTAGE + telemetry_feedback["max_cell_voltage"] = MAX_CELL_VOLTAGE + self.telemetry.min_pack_voltage = MIN_CELL_VOLTAGE * number_of_cells self.telemetry.max_pack_voltage = MAX_CELL_VOLTAGE * number_of_cells @@ -787,19 +791,19 @@ def decode_telemetry_feedback_frame(self, data) -> dict: telemetry_feedback["average_cell_voltage"] = self.telemetry.average_cell_voltage # get lowest cell and its voltage - lowest_cell_data = self.get_min_cell() - self.telemetry.lowest_cell = lowest_cell_data['min_cell'] + lowest_cell_data = self.get_lowest_cell() + self.telemetry.lowest_cell = lowest_cell_data['lowest_cell'] # shift cell-index on return List by 1 telemetry_feedback["lowest_cell"] = self.telemetry.lowest_cell + 1 - self.telemetry.lowest_cell_voltage = lowest_cell_data['min_cell_voltage'] + self.telemetry.lowest_cell_voltage = lowest_cell_data['lowest_cell_voltage'] telemetry_feedback["lowest_cell_voltage"] = self.telemetry.lowest_cell_voltage # get lowest cell and its voltage - highest_cell_data = self.get_max_cell() - self.telemetry.highest_cell = highest_cell_data["max_cell"] + highest_cell_data = self.get_highest_cell() + self.telemetry.highest_cell = highest_cell_data["highest_cell"] # shift cell-index on return List by 1 telemetry_feedback["highest_cell"] = self.telemetry.highest_cell + 1 - self.telemetry.highest_cell_voltage = highest_cell_data["max_cell_voltage"] + self.telemetry.highest_cell_voltage = highest_cell_data["highest_cell_voltage"] telemetry_feedback["highest_cell_voltage"] = self.telemetry.highest_cell_voltage # calculate delta cell voltage