test - better rounding|?

This commit is contained in:
Tester23
2023-10-19 11:56:30 +02:00
parent 1574f210b5
commit 476af2825a

View File

@ -436,7 +436,27 @@ HassDeviceInfo* hass_init_power_sensor_device_info(int index) {
// generate string like "{{ float(value)*0.1|round(2) }}"
// {{ float(value)*0.1 }} for value=12 give 1.2000000000000002, using round() to limit the decimal places
// 2023 10 19 - it is not a perfect solution, it's better to use:
// {{ '%0.2f'|format(states('sensor.varasto2_osram_temp')|float + 0.7) }}
//
char *hass_generate_multiplyAndRound_template(int decimalPlacesForRounding, int decimalPointOffset, int divider) {
#if 1
char tmp[8];
int i;
strcpy(g_hassBuffer, "{{ '%0.");
sprintf(tmp, "%if", decimalPlacesForRounding);
strcat(g_hassBuffer, tmp);
strcat(g_hassBuffer, "'|format(float(value)");
if (decimalPointOffset != 0) {
strcat(g_hassBuffer, "*0.");
for (i = 1; i < decimalPointOffset; i++) {
strcat(g_hassBuffer, "0");
}
strcat(g_hassBuffer, "1");
}
strcat(g_hassBuffer, ") }}");
#else
char tmp[8];
int i;
@ -454,7 +474,7 @@ char *hass_generate_multiplyAndRound_template(int decimalPlacesForRounding, int
sprintf(tmp, "%i", decimalPlacesForRounding);
strcat(g_hassBuffer, tmp);
strcat(g_hassBuffer, ") }}");
#endif
return g_hassBuffer;
}