Everything worked until I decided to test the NaN code by temporarily removing the input_numbers from home assistant (rendering them unavailable) and everything stopped working. This continued even after restoring the input_numbers in home assistant (which should have made them available again.) Rebooting the ESP32 module restored function.
Here is the debug output when things are not working. Of note, the offset sensors show NaN when they should be showing the default. The max/mins are being interpreted as 7FFFFFFF hex instead of NaN or the default. That must mean something…
Can anyone see what might be wrong with the lambda (isnan(x)) code?
debug output:
[sensor 2si:496]: minval: 2147483647, maxval: 2147483647, inputval: 2147483647, offset: nan, normalized_input: 0.00, calcdegrees: 0.00, degrees: -2146531423
sensor:
# offsets from HA
- platform: homeassistant
id: "compressor_offset_2si"
entity_id: input_number.compressor_offset_2si
filters:
- lambda: 'if (isnan(x)) { return 0.0; } return x;'
# min and max values from HA
- platform: homeassistant
entity_id: input_number.compressor_2si_min
id: compressor_2si_min
filters:
- lambda: 'if (isnan(x)) { return 50.0; } return x;'
- platform: homeassistant
entity_id: input_number.compressor_2si_max
id: compressor_2si_max
filters:
- lambda: 'if (isnan(x)) { return 400.0; } return x;'
# ultimate sensor values
- platform: template
id: twostagein
name: "2nd stage input"
filters:
- offset: !lambda 'return id(compressor_offset_2si).state;'
script:
- id: read_sensors
mode: single
then:
# sensor 1
- switch.turn_on: bit0
- switch.turn_off: bit1
- switch.turn_off: bit2
- delay: 500ms
- component.update: tempread
- sensor.template.publish:
id: twostagein
state: !lambda 'return id(tempread).state;'
- lambda: |-
id(disp).set_component_text_printf("value_2si","%.0f", id(twostagein).state) ;
int minval=id(compressor_2si_min).state;
int maxval=id(compressor_2si_max).state;
int inputval = id(twostagein).state;
float normalized_input = (inputval < minval) ? 0 : (inputval > maxval) ? 1 : (inputval - minval) / (float)(maxval - minval);
int calcdegrees = normalized_input * 180; // Scale from 0 to 180 degrees
int degrees = max(0, min(calcdegrees, 180)); // Clamp between 0 and 180 degrees
id(disp).set_component_value("gauge_2si", degrees);
ESP_LOGD("sensor 2si", "minval: %d, maxval: %d, inputval: %d, offset: %2.f, normalized_input: %.2f, calcdegrees: %.2f, degrees: %d", minval, maxval, inputval, id(compressor_offset_2si), normalized_input, calcdegrees, degrees);