-
Notifications
You must be signed in to change notification settings - Fork 5
Description
When I was experimenting with implementing a thermostat temperature component reporting in F and consistent with the Mitsubishi tables, I tried to follow the documentation, but:
- It was not immediately clear that the proposed source code lookup table and lambda needed to go into the ESPHome configuration, as opposed to the HA configuration. I finally figured out ESPHome configuration was the place. This might be better explicitly mentioned.
- I could not get it to work as described because:
- The lambda complained that it could not find
id(thermostat_temperature). I addressed this by adding anidto its definition under thenamecomponent - The corrected template definition seems to be missing:
device_class,state_class,unit_of_measurementandaccuracy_decimals. I added all these - The documents template sensor must be placed nested under the
sensordefinitions, which was not obvious from the documentation. Might want to mention this.
- The lambda complained that it could not find
The YAML for the global lookup table was added, as provided.
Next I realized that since the lambda has a fallback, as written, it will not return integer only values (as the table would). This can be corrected with a slighty different fallback where the result is properly rounded.
Then it seems that the code to compute the limit for the lookup loop, which is inside a lambda, is incorrect. The tables, inside the lambda are effectively pointers. Hence the first component in the expression will be 4 or 8, divided by the size of a float. On ESP32 platforms the expression evaluates to 1, which is incorrect for the intended purpose.
Another realization is that this long lookup table generally produces the same values as the fallback, with a few exceptions (18 of them). It is therefore possble to only have the exceptional cases in the lookup table. Below is the final replacement code:
globals:
- id: m2_ctof_len
type: int
initial_value: '18'
- id: m2_ctof_c
type: float[18]
initial_value: |-
{12,
12.5,
18,
18.5,
19,
20.5,
21,
21.5,
22,
22.5,
28,
28.5,
29,
29.5,
30,
30.5,
32,
32.5}
- id: m2_ctof_f
type: float[18]
initial_value: |-
{53,
54,
65,
66,
67,
68,
69,
70,
71,
72,
83,
84,
85,
86,
87,
88,
89,
90}
sensor:
- platform: template
name: "Thermostat Corrected Temperature"
id: thermostat_temperature_corrected
icon: "mdi:thermometer"
device_class: "temperature"
state_class: "measurement"
unit_of_measurement: "°F"
accuracy_decimals: 0
lambda: |-
float celsius = id(thermostat_temperature).state;
const float *ctab = id(m2_ctof_c);
const float *ftab = id(m2_ctof_f);
const int limit = id(m2_ctof_len);
for (int i = 0; i < limit; i++) {
if (celsius <= ctab[i] + 0.0001f) {
return ftab[i];
}
}
// Default fallback if exact match is not found
float fahrenheit = celsius * 9.0f / 5.0f + 32.0f;
fahrenheit += fahrenheit >= 0.0 ? 0.5 : -0.5;
return (int)fahrenheit;
update_interval: 60s