ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
pylontech_sensor.cpp
Go to the documentation of this file.
1#include "pylontech_sensor.h"
3#include "esphome/core/log.h"
4
5namespace esphome::pylontech {
6
7ESPHOME_LOG_TAG(TAG, "pylontech.sensor");
8
10 ESP_LOGCONFIG(TAG,
11 "Pylontech Sensor:\n"
12 " Battery %u",
13 this->bat_num_);
14 LOG_SENSOR(" ", "Voltage", this->voltage_sensor_);
15 LOG_SENSOR(" ", "Current", this->current_sensor_);
16 LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
17 LOG_SENSOR(" ", "Temperature low", this->temperature_low_sensor_);
18 LOG_SENSOR(" ", "Temperature high", this->temperature_high_sensor_);
19 LOG_SENSOR(" ", "Voltage low", this->voltage_low_sensor_);
20 LOG_SENSOR(" ", "Voltage high", this->voltage_high_sensor_);
21 LOG_SENSOR(" ", "Coulomb", this->coulomb_sensor_);
22 LOG_SENSOR(" ", "MOS Temperature", this->mos_temperature_sensor_);
23#ifdef PYLONTECH_SENSOR_CELL_COUNT
24 for (const CellSensors &c : this->cells_) {
25 ESP_LOGCONFIG(TAG, " Cell %u", c.cell);
26 LOG_SENSOR(" ", "Voltage", c.voltage);
27 LOG_SENSOR(" ", "Temperature", c.temperature);
28 }
29#endif
30}
31
33 if (this->voltage_sensor_ != nullptr) {
34 this->voltage_sensor_->publish_state(((float) line->volt) / 1000.0f);
35 }
36 if (this->current_sensor_ != nullptr) {
37 this->current_sensor_->publish_state(((float) line->curr) / 1000.0f);
38 }
39 if (this->temperature_sensor_ != nullptr) {
40 this->temperature_sensor_->publish_state(((float) line->tempr) / 1000.0f);
41 }
42 if (this->temperature_low_sensor_ != nullptr) {
43 this->temperature_low_sensor_->publish_state(((float) line->tlow) / 1000.0f);
44 }
45 if (this->temperature_high_sensor_ != nullptr) {
46 this->temperature_high_sensor_->publish_state(((float) line->thigh) / 1000.0f);
47 }
48 if (this->voltage_low_sensor_ != nullptr) {
49 this->voltage_low_sensor_->publish_state(((float) line->vlow) / 1000.0f);
50 }
51 if (this->voltage_high_sensor_ != nullptr) {
52 this->voltage_high_sensor_->publish_state(((float) line->vhigh) / 1000.0f);
53 }
54 if (this->coulomb_sensor_ != nullptr) {
55 this->coulomb_sensor_->publish_state(line->coulomb);
56 }
57 if (this->mos_temperature_sensor_ != nullptr) {
58 this->mos_temperature_sensor_->publish_state(((float) line->mostempr) / 1000.0f);
59 }
60}
61
62#ifdef PYLONTECH_SENSOR_CELL_COUNT
64 for (const CellSensors &c : this->cells_) {
65 if ((read & (1u << c.cell)) == 0) {
66 continue;
67 }
68 if (c.voltage != nullptr) {
69 c.voltage->publish_state(((float) cells[c.cell].volt) / 1000.0f);
70 }
71 if (c.temperature != nullptr) {
72 c.temperature->publish_state(((float) cells[c.cell].tempr) / 100.0f);
73 }
74 }
75}
76#endif
77
78} // namespace esphome::pylontech
virtual void on_line_read(LineContents *line)=0
Called only with records for this listener's battery.
std::array< CellContents, MAX_CELLS > CellArray
Definition pylontech.h:38
void on_cells_read(const CellArray &cells, cell_mask_t read) override
StaticVector< CellSensors, PYLONTECH_SENSOR_CELL_COUNT > cells_
ESPHOME_LOG_TAG(TAG, "pylontech")
uint16_t cell_mask_t
Definition pylontech.h:21