Is there a way to have multiple values returned by a sensor?

I have a distance sensor and I would like it to return both the raw value and a calculated one. I know I could just return the raw value and then setup some other things to do the math but I would rather the ESPHome device did it all and return all the values I want.

Second question would be how to you get LAMBDA to round a value?
I tried
- lambda: return (1 - ((x - 0.13720)/1.38)) * 100 | round(2);
But the system complains about it.

Below is what I have in the ESPHome config file for the device.

sensor:
  - platform: ultrasonic
    trigger_pin: D3
    echo_pin: D4
    update_interval: 5s
    unit_of_measurement: "%"
    name: "Water Tank Level"
    filters:
      - lambda: return (1 - ((x - 0.13720)/1.38)) * 100;
      - filter_out: nan

Any help/thoughts would be appreciated.
Drew

The documentation shows setting up 2 values for the same sensor, such as temp and humidity:

sensor:
  - platform: dht
    # ...
    temperature:
      filters:
        # ...
    humidity:
      filters:
        # ...

I don’t see any option to round the value in ESPHome, so you might need to do that in HA with a value template.

That is only for components that have more than one sensor, like the DHTxx.

Set up the sensor to return the raw value.

Set up a template sensor to return the calculated one.

See: Round number in ESPHome

Thank you for the replies. I did not like the template option because it means I have the water tank info being calculated in multiple places and was not keen on that.

I did not think to use round as a function, that will show me. Thanks for that one.

I did fine I could do this with the distance sensor to get the value and the % level. Not sure its the best idea but it works.

sensor:
  - platform: ultrasonic
    trigger_pin: D3
    echo_pin: D4
    update_interval: 5s
    unit_of_measurement: "%"
    name: "Water Tank Level"
    filters:
      - lambda: return round(((1 - ((x - 0.13720)/1.38)) * 100) * 10) / 10;
      - filter_out: nan
  - platform: ultrasonic
    trigger_pin: D3
    echo_pin: D4
    update_interval: 5s
    name: "Value"
    filters:
      - filter_out: nan

Copy Sensor

1 Like

@Didgeridrew Great! Thanks for this.

  - platform: bme280_i2c
    i2c_id: bus_a
    address: 0x76
    update_interval: 10s
    pressure:
      name: Pressure
    temperature:
      name: "Temperature"
      accuracy_decimals: 1
      filters:
        - offset: 0
    humidity:
      name: "Humidity-Raw"
      id: "humidity"
      accuracy_decimals: 1
  - platform: copy
    source_id: humidity
    name: "Humidity"
    filters:
        - offset: 0
    #     # - lambda: return x - (x * .3);
        - calibrate_polynomial:
           degree: 2
           datapoints:
           # Map 0.0 (from sensor) to 1.0 (true value)
            - 28.3 -> 13
            - 40.6 -> 34
            - 64.8 -> 69