Configuration file - comparisons to zero?

Hi,

I’m trying to set up a basic display of power/voltage (not started implementing yet) on a esp32 device (LILYGO® TTGO T-Display ESP32 WiFi and Bluetooth Module Development Board For Arduino 1.14 Inch LCD). I’d like the output to be colored (based on power), and the below code (with all the mess with ifs) is getting me halfway there - it seems to not understand the places where I’m comparing values to zero. i.e. id(power_consumption).state is never displayed in id(my_green).
How can I fix that?
Bonus question: can I simplify the code (are there switch/elseif staement equivalents)?

# Create a new node in ESPHome. Add these lines to the yaml file 
substitutions:
  devicename: ttgo01
# just change the devicename above not the devicename below 

esphome:
  name: ttgo01
  platform: ESP32
  board: featheresp32
logger:
api:
ota:
  password: !secret esphome_ota
captive_portal:

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  ap:
    ssid: "${devicename} Fallback Hotspot"
    password: !secret esphome_ota

color:
  - id: my_red
    red: 100%
    green: 0%
    blue: 0%
  - id: my_yellow
    red: 100%
    green: 100%
    blue: 0%
  - id: my_green
    red: 0%
    green: 100%
    blue: 0%
  - id: my_blue
    red: 0%
    green: 0%
    blue: 100%
  - id: my_gray
    red: 50%
    green: 50%
    blue: 50%

font:
  - file: "SFCompact.ttf"
    id: sfcompact_48
    size: 48
  - file: "SFCompact.ttf"
    id: sfcompact_24
    size: 24
  - file: "SFCompact.ttf"
    id: sfcompact_S
    size: 14
# put a ttf font called sfcompact.ttf in the esphome folder

binary_sensor:
  - platform: gpio
    pin:
      number: GPIO0
      inverted: true
    name: ${devicename}top push
    id: tdisplay_button_input_0

  - platform: gpio
    pin:
      number: GPIO35
      inverted: true
    id: tdisplay_button_input_1
    name: ${devicename}bottom push

  - platform: status
    name: "Node Status"
    id: system_status

switch:
  - platform: restart
    name: ${devicename} restart

# replace my entity IDs with your entity IDs below
sensor:
  - platform: homeassistant
    id: power_generation
    entity_id: sensor.t_series_generation_power
    unit_of_measurement: 'W'
    state_class: measurement
  - platform: homeassistant
    id: power_consumption_phase_1
    entity_id: sensor.electricity_meter_power_active_phase_1
    unit_of_measurement: 'W'
    state_class: measurement
  - platform: homeassistant
    id: power_consumption_phase_2
    entity_id: sensor.electricity_meter_power_active_phase_2
    unit_of_measurement: 'W'
    state_class: measurement
  - platform: homeassistant
    id: power_consumption_phase_3
    entity_id: sensor.electricity_meter_power_active_phase_3
    internal: true
  - platform: homeassistant
    id: power_consumption
    entity_id: sensor.energia_czynna_aktualnie_pobierana
    internal: true

time:
  - platform: homeassistant
    id: esptime

spi:
  clk_pin: GPIO18
  mosi_pin: GPIO19

display:
  - platform: st7789v
    model: TTGO TDisplay 135x240
    backlight_pin: GPIO4
    cs_pin: GPIO5
    dc_pin: GPIO16
    reset_pin: GPIO23
    rotation: 90
    lambda: |-
      it.line(0, 25, 240, 25);
      if (id(power_consumption).state > 2000){
        it.printf(50, 0, id(sfcompact_24), id(my_red), TextAlign::TOP_CENTER, "%.0f W", id(power_consumption).state);
      }
      else{
        if (0 > id(power_consumption).state < 2000){
          it.printf(50, 0, id(sfcompact_24), id(my_yellow), TextAlign::TOP_CENTER, "%.0f W", id(power_consumption).state);
        }
        else {
          if (id(power_consumption).state < 0){
          it.printf(50, 0, id(sfcompact_24), id(my_green), TextAlign::TOP_CENTER, "%.0f W", id(power_consumption).state);
        }
        }
      }

      if (id(power_consumption_phase_1).state > 1000){
        it.printf(50, 30, id(sfcompact_24), id(my_red), TextAlign::TOP_CENTER, "%.0f W", id(power_consumption_phase_1).state);
      }
      else{
        if (id(power_consumption_phase_1) < 0){
          it.printf(50, 30, id(sfcompact_24), id(my_green), TextAlign::TOP_CENTER, "%.0f W", id(power_consumption_phase_1).state);
        }
        else {
          if (id(power_consumption_phase_1).state < 1000){
          it.printf(50, 30, id(sfcompact_24), id(my_yellow), TextAlign::TOP_CENTER, "%.0f W", id(power_consumption_phase_1).state);
        }
        }
      }

      if (id(power_consumption_phase_2).state > 1000){
        it.printf(50, 60, id(sfcompact_24), id(my_red), TextAlign::TOP_CENTER, "%.0f W", id(power_consumption_phase_2).state);
      }
      else{
        if (0 > id(power_consumption_phase_2).state < 1000){
          it.printf(50, 60, id(sfcompact_24), id(my_yellow), TextAlign::TOP_CENTER, "%.0f W", id(power_consumption_phase_2).state);
        }
        else {
          if (id(power_consumption_phase_2).state < 0){
          it.printf(50, 60, id(sfcompact_24), id(my_green), TextAlign::TOP_CENTER, "%.0f W", id(power_consumption_phase_2).state);
        }
        }
      }

      if (id(power_consumption_phase_3).state > 1000){
        it.printf(50, 90, id(sfcompact_24), id(my_red), TextAlign::TOP_CENTER, "%.0f W", id(power_consumption_phase_3).state);
      }
      else{
        if (0 > id(power_consumption_phase_3).state < 1000){
          it.printf(50, 90, id(sfcompact_24), id(my_yellow), TextAlign::TOP_CENTER, "%.0f W", id(power_consumption_phase_3).state);
        }
        else {
          if (id(power_consumption_phase_3).state < 0){
          it.printf(50, 90, id(sfcompact_24), id(my_green), TextAlign::TOP_CENTER, "%.0f W", id(power_consumption_phase_3).state);
        }
        }
      }

      if (id(power_generation).state > 0) {
        if (id(power_generation).state > 2000){
        it.printf(200, 0, id(sfcompact_24), id(my_green), TextAlign::TOP_CENTER, "%.0f W", id(power_generation).state);
        }
        else{
          if (1000 > id(power_generation).state < 2000){
            it.printf(200, 0, id(sfcompact_24), id(my_yellow), TextAlign::TOP_CENTER, "%.0f W", id(power_generation).state);
          }
          else {
            if (id(power_generation).state < 1000){
              it.printf(200, 0, id(sfcompact_24), id(my_red), TextAlign::TOP_CENTER, "%.0f W", id(power_generation).state);
            }
          }
        }
      }
      else {
        it.printf(200, 0, id(sfcompact_24), id(my_red), TextAlign::TOP_CENTER, "Offline");
      }

lambda’s are C++ code, so you have to use logical operators in your “if statement” if you want to test 2 boundaries,
See also Operators in C and C++ - Wikipedia

And yes your code can be simplified :slight_smile: No need to test the same boundary in the else part again. And don’t forget to test the boundaries correct, what happens in your if’s if the value is 0.

Personally I would change the lambda code (first part) to something like this

    lambda: |-
      it.line(0, 25, 240, 25);
      if (id(power_consumption).state <= 0) {    // include 0 value
        it.printf(50, 0, id(sfcompact_24), id(my_green), TextAlign::TOP_CENTER, "%.0f W", id(power_consumption).state);
      } else if (id(power_consumption).state <= 2000) { 
        it.printf(50, 0, id(sfcompact_24), id(my_yellow), TextAlign::TOP_CENTER, "%.0f W", id(power_consumption).state);
      } else {
        it.printf(50, 0, id(sfcompact_24), id(my_red), TextAlign::TOP_CENTER, "%.0f W", id(power_consumption).state);
      }