Skip to content

Commit

Permalink
add bash-script to combine all yaml-files created by create_ha_sensor…
Browse files Browse the repository at this point in the history
…s.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
  • Loading branch information
Privatecoder committed Jan 24, 2024
1 parent 69a2b18 commit ca3e6b4
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 20 deletions.
43 changes: 43 additions & 0 deletions ha-sample/combine-yaml-files.sh
Original file line number Diff line number Diff line change
@@ -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"
28 changes: 22 additions & 6 deletions ha-sample/create_ha_sensors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
{
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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,
Expand Down
32 changes: 18 additions & 14 deletions src/fetch_bms_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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

Expand All @@ -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
Expand Down

0 comments on commit ca3e6b4

Please sign in to comment.