diff --git a/include/devices.h b/include/devices.h index a4e1124..fb7d813 100644 --- a/include/devices.h +++ b/include/devices.h @@ -12,8 +12,8 @@ auto roomSensor = Builder::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::instance(new Sensor{ "Level", OIL_SENSOR }) @@ -35,8 +35,8 @@ auto tankSensor = Builder::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::instance(PRESENCE_SENSOR) @@ -46,8 +46,8 @@ auto presenceTracker = Builder::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(); diff --git a/include/rc_devices.h b/include/rc_devices.h index f6da508..ebd37d0 100644 --- a/include/rc_devices.h +++ b/include/rc_devices.h @@ -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}; +}