reduce memory usage by using string for battery percentage template

defined at compile time
This commit is contained in:
Nicu Hodos 2025-10-06 17:49:58 +02:00
parent 3dc31ff585
commit 545a6fd2f6
2 changed files with 10 additions and 18 deletions

View File

@ -12,7 +12,7 @@ auto roomSensor = Builder<TemperatureSensor>::instance(TEMP_SENSOR)
.withArea("Basement") .withArea("Basement")
.withParent(gatewayDevice)) .withParent(gatewayDevice))
.withValueTemplate("{{ value_json.sensor.temperature }}") .withValueTemplate("{{ value_json.sensor.temperature }}")
.addPreconfigured(batterySensors<TemperatureSensor>(TEMP_SENSOR, 2.5, 4.5)) .addPreconfigured(batterySensors<TemperatureSensor>(TEMP_SENSOR, BATTERY_PERCENTAGE_TEMPLATE(2.5, 2)))
.build(); .build();
auto tankSensor = Builder<Sensor>::instance(new Sensor{ "Level", OIL_SENSOR }) auto tankSensor = Builder<Sensor>::instance(new Sensor{ "Level", OIL_SENSOR })
@ -34,7 +34,7 @@ auto tankSensor = Builder<Sensor>::instance(new Sensor{ "Level", OIL_SENSOR })
.withValueTemplate("{{ value_json.sensor.value }}") .withValueTemplate("{{ value_json.sensor.value }}")
.build() .build()
) )
.addPreconfigured(batterySensors<Sensor>(OIL_SENSOR, 4.0, 6.4)) .addPreconfigured(batterySensors<Sensor>(OIL_SENSOR, BATTERY_PERCENTAGE_TEMPLATE(4, 2.4)))
.build(); .build();
auto presenceTracker = Builder<BinarySensor>::instance(PRESENCE_SENSOR) auto presenceTracker = Builder<BinarySensor>::instance(PRESENCE_SENSOR)
@ -44,7 +44,7 @@ auto presenceTracker = Builder<BinarySensor>::instance(PRESENCE_SENSOR)
.withModel("AtTiny85") .withModel("AtTiny85")
.withParent(gatewayDevice)) .withParent(gatewayDevice))
.withValueTemplate("{{ value_json.sensor.state }}") .withValueTemplate("{{ value_json.sensor.state }}")
.addPreconfigured(batterySensors<BinarySensor>(PRESENCE_SENSOR, 2.7, 3.3)) .addPreconfigured(batterySensors<BinarySensor>(PRESENCE_SENSOR, BATTERY_PERCENTAGE_TEMPLATE(2.7, 0.6)))
.withOffDelaySeconds(5*60) .withOffDelaySeconds(5*60)
.withDeviceClass("presence") .withDeviceClass("presence")
.build(); .build();

View File

@ -1,6 +1,7 @@
#pragma once #pragma once
#define MAIN_DEVICE_ID "rc-gateway" #define MAIN_DEVICE_ID "rc-gateway"
#define BATTERY_PERCENTAGE_TEMPLATE(min, diff) "{{ ((value_json.sensor.diagnostic.voltage|float-" #min ")|round(2)*100/" #diff ")|int }}"
#include "esp.h" #include "esp.h"
#include "ha.h" #include "ha.h"
@ -72,22 +73,13 @@ struct EasyHomeSwitch : Switch {
} }
}; };
VoltageSensor* createVoltageSensor(const char* id) {
return new VoltageSensor{id, "Battery voltage", "{{ value_json.sensor.diagnostic.voltage }}"};
}
#define BATTERY_PERCENTAGE_TEMPLATE "{{ ((value_json.sensor.diagnostic.voltage|float-%.2f)|round(2)*100/%.2f)|int }}"
BatterySensor* createBatterySensor(const char* id, float min, float max) {
auto len = snprintf(nullptr, 0, BATTERY_PERCENTAGE_TEMPLATE, min, max - min) + 1;
char* value_json = new char[len];
snprintf(value_json, len, BATTERY_PERCENTAGE_TEMPLATE, min, max - min);
return new BatterySensor{id, "Battery level", value_json};
}
template <class T> template <class T>
auto batterySensors(const char* id, float min, float max) { auto batterySensors(const char* id, const char* batterySensorTemplate) {
return [id, min, max](Builder<T>& builder) -> Builder<T>& { return [id, batterySensorTemplate](Builder<T>& builder) -> Builder<T>& {
builder.addDiagnostic(createVoltageSensor(id)).addDiagnostic(createBatterySensor(id, min, max)).build(); builder
.addDiagnostic(new VoltageSensor{id, "Battery voltage", "{{ value_json.sensor.diagnostic.voltage }}"})
.addDiagnostic(new BatterySensor{id, "Battery level", batterySensorTemplate})
.build();
return builder; return builder;
}; };
} }