ESPhome Labda retuning value only when greater than 0

Hi All,

I am working on integrating ESPhome into the new Energy feature and need to return a positive from one CT clamp that shows both positive and negative values. Positive is when i am buying power from the grid and negative when i am selling it.

I have defined 2 platform templates as below

#Total House Watts draw 
  - platform: template
    name: ${disp_name} Total House Watts draw
    id: gridenergydraw
    lambda: !lambda |-
        if (id(HouseWatts) > 1) {
          // global value is greater than 5
         return id(HouseWatts) * 1;
        } else {
          return id(HouseWatts) * 0;
        }
    accuracy_decimals: 1
    unit_of_measurement: W
    icon: "mdi:flash-circle"
    update_interval: ${update_time}

#Total House Watts return
  - platform: template
    name: ${disp_name} Total House Watts return
    id: gridenergyreturn
    lambda: !lambda |-
        if (id(HouseWatts) < 1) {
          // global value is greater than 1
         return id(HouseWatts) * -1;
       // convert negitave number into positive  
        } else {
          return id(HouseWatts) * 0;
        }
    accuracy_decimals: 1
    unit_of_measurement: W
    icon: "mdi:flash-circle"
    update_interval: ${update_time}
/config/energy-monitor.yaml: In lambda function:
/config/energy-monitor.yaml:162:24: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
         if (id(HouseWatts) > 1) {
                        ^
/config/energy-monitor.yaml:164:28: error: invalid operands of types 'esphome::sensor::Sensor*' and 'int' to binary 'operator*'
          return id(HouseWatts) * 1;
                            ^
/config/energy-monitor.yaml:166:29: error: invalid operands of types 'esphome::sensor::Sensor*' and 'int' to binary 'operator*'
           return id(HouseWatts) * 0;
                             ^
*** [.pioenvs/energy-monitor/src/main.cpp.o] Error 1
========================== [FAILED] Took 2.18 seconds ==========================

As below but the conditional lambda if then does not work with errors about the variable type

Please help

Thanks

Lachlan

Use this:

id(HouseWatts).state

wherever you have:

id(HouseWatts)

Assuming HouseWatts is a sensor id and not a global variable.