I need some help with sensor 'float' and 'int', I think

When compiling this:

sensor:
# This is the actual reading of the various sensors
  - platform: adc 
    pin: A0
    id: moisture
    update_interval: 0.1s
    internal: true # Don't expose this sensor to HA, the template sensors will do that
    accuracy_decimals: 1
    filters:
      - sliding_window_moving_average: # averages the last 10 results, probably overkill
          window_size: 10
          send_every: 10
      - lambda: |-
          if (x > id(plant_soil_moisture_setpoint)) { 
            return 0;
          } else if (x < 0.264) { 
            return 100;
          } else {
            return (id(plant_soil_moisture_setpoint)-x) / (id(plant_soil_moisture_setpoint)-0.264) * 100.0; 	// use a linear fit for any values in between
          }


  - platform: homeassistant
    name: "Plant soil moisture setpoint"
    entity_id: sensor.plant_soil_moisture_setpoint # From HA input_number
    id: plant_soil_moisture_setpoint
    accuracy_decimals: 1

I’m getting the following errors:

src/main.cpp: In lambda function:
src/main.cpp:456:15: error: invalid operands of types 'float' and 'esphome::homeassistant::HomeassistantSensor*' to binary 'operator>'
       if (x > plant_soil_moisture_setpoint) {     
               ^
src/main.cpp:461:46: error: invalid operands of types 'esphome::homeassistant::HomeassistantSensor*' and 'float' to binary 'operator-'
         return (plant_soil_moisture_setpoint-x) / (plant_soil_moisture_setpoint-0.264) * 100.0;  
                                              ^
src/main.cpp:461:81: error: invalid operands of types 'esphome::homeassistant::HomeassistantSensor*' and 'double' to binary 'operator-'
         return (plant_soil_moisture_setpoint-x) / (plant_soil_moisture_setpoint-0.264) * 100.0;  
                                                                                 ^
src/main.cpp:463:3: warning: control reaches end of non-void function [-Wreturn-type]
   });
   ^

I’m pretty sure this has something to do with mixing float and int in the comparisons but I can’t figure out how to fix it.

Any ideas?

1 Like

Ever noticed how often detailing the problem for someone else changes ones thinking and solves it?

It’s happened again.

The solution, for completeness, is adding .state after each instance of id(plant_soil_moisture_setpoint).

Doh!

4 Likes
2 Likes