create rc devices typical battery sensors

This commit is contained in:
Nicu Hodos 2025-10-05 09:49:22 +02:00
parent 264c1c1e80
commit 0a228df07d
2 changed files with 16 additions and 6 deletions

View File

@ -12,8 +12,8 @@ auto roomSensor = Builder<TemperatureSensor>::instance(TEMP_SENSOR)
.withArea("Basement")
.withParent(gatewayDevice))
.withValueTemplate("{{ value_json.sensor.temperature }}")
.addDiagnostic(new VoltageSensor{TEMP_SENSOR, "Battery voltage", "{{ value_json.sensor.diagnostic.voltage }}"})
.addDiagnostic(new BatterySensor{TEMP_SENSOR, "Battery level", "{{ ((value_json.sensor.diagnostic.voltage|float-2.5)|round(2)*100/2)|int }}"})
.addDiagnostic(createVoltageSensor(TEMP_SENSOR))
.addDiagnostic(createBatterySensor(TEMP_SENSOR, 2.5, 4.5))
.build();
auto tankSensor = Builder<Sensor>::instance(new Sensor{ "Level", OIL_SENSOR })
@ -35,8 +35,8 @@ auto tankSensor = Builder<Sensor>::instance(new Sensor{ "Level", OIL_SENSOR })
.withValueTemplate("{{ value_json.sensor.value }}")
.build()
)
.addDiagnostic(new VoltageSensor{OIL_SENSOR, "Battery voltage", "{{ value_json.sensor.diagnostic.voltage }}"})
.addDiagnostic(new BatterySensor{OIL_SENSOR, "Battery level", "{{ ((value_json.sensor.diagnostic.voltage|float-4.0)|round(2)*100/2.4)|int }}"})
.addDiagnostic(createVoltageSensor(OIL_SENSOR))
.addDiagnostic(createBatterySensor(OIL_SENSOR, 4.0, 6.4))
.build();
auto presenceTracker = Builder<BinarySensor>::instance(PRESENCE_SENSOR)
@ -46,8 +46,8 @@ auto presenceTracker = Builder<BinarySensor>::instance(PRESENCE_SENSOR)
.withModel("AtTiny85")
.withParent(gatewayDevice))
.withValueTemplate("{{ value_json.sensor.state }}")
.addDiagnostic(new VoltageSensor{PRESENCE_SENSOR, "Battery voltage", "{{ value_json.sensor.diagnostic.voltage }}"})
.addDiagnostic(new BatterySensor{PRESENCE_SENSOR, "Battery level", "{{ ((value_json.sensor.diagnostic.voltage|float-2.7)|round(2)*100/0.6)|int }}"})
.addDiagnostic(createVoltageSensor(PRESENCE_SENSOR))
.addDiagnostic(createBatterySensor(PRESENCE_SENSOR, 2.7, 3.3))
.withOffDelaySeconds(5*60)
.withDeviceClass("presence")
.build();

View File

@ -71,3 +71,13 @@ struct EasyHomeSwitch : Switch {
publisher(stateTopic, msg);
}
};
VoltageSensor* createVoltageSensor(const char* id) {
return new VoltageSensor{id, "Battery voltage", "{{ value_json.sensor.diagnostic.voltage }}"};
}
BatterySensor* createBatterySensor(const char* id, float min, float max) {
char value_json[128];
snprintf(value_json, 128, "{{ ((value_json.sensor.diagnostic.voltage|float-%f)|round(2)*100/%f)|int }}", min, max - min);
return new BatterySensor{id, "Battery level", value_json};
}