Using sensor state as configuration value for another sensor

Hi,
Sometimes I have my difficulties finding the basics in the documentation. Instead of a code example feel free to point me into the right direction in the documentation :slight_smile:

I have a BME680 sensor that measures the air pressure:

sensor:
  - platform: bme680
    temperature:
      id: bme680_temperature
      name: "BME680 Temperature"
    pressure:
      id: bme680_pressure
      name: "BME680 Pressure"
    humidity:
      id: bme680_humidity
      name: "BME680 Humidity"

Then there is a SCD4x sensor which allow to configure a pressure compensation:

sensor:
  - platform: scd4x
    co2:
      name: "Workshop CO2"
    temperature:
      name: "Workshop Temperature"
    humidity:
      name: "Workshop Humidity"
    ambient_pressure_compensation: ?

I haven’t found a way to set the value to bme680_pressure. I tried the following:

ambient_pressure_compensation: id(bme680_pressure)

ambient_pressure_compensation: id(bme680_pressure).state

ambient_pressure_compensation:
  lambda: |-
    return id(bme680_pressure)

ambient_pressure_compensation:
  lambda: |-
    return id(bme680_pressure).state

What is the right way to do it?

Not 100% sure but try:

ambient_pressure_compensation: !lambda: return id(bme680_pressure).state

Unfortunately not :frowning:

--> ambient_pressure_compensation: !lambda return id(bme680_pressure).state;

This option is not templatable!.
  ambient_pressure_compensation: !lambda |-
    return id(bme680_pressure).state;

Ah, there’s your answer:

Looking at the code it is expecting a float.

Maybe put in a feature request on ESPHOMEs site:

I could solve it. I found in the source code (C++ and Python) an undocumented option ambient_pressure_compensation_source: <sensor_id> which allows to be set to an existing sensor id. As soon as the pressure sensor has a value, SCD4x is going to use it.

I solved this using a lambda:

    pressure:
      name: Pressure
      id: bme280_pressure
      accuracy_decimals: 0
      on_value:
        then:
          lambda: |-
            if (id(bme280_pressure).has_state())
            {
              float mBar = id(bme280_pressure).state * 68.9476;
              id(scd30_sensor).set_ambient_pressure_compensation(mBar);
            }