Combining the data from various sensors

I want to combine the data from:

  • different sensors, on
  • different devices, allocated to
  • different areas
    For example, combine the atmospheric pressures from various sensors into a single mean value (or other transformation) for passing on to the Home Assistant UI and connected databases.
    Do I:
  1. attach the necessary code to the .yaml of one of the existing devices and pull in the data from all the other sensors, or
  2. do I create some sort of virtual device that combines, then manipulates, the data for output

If the latter, is there a reference I can follow? (I have read this: How to: virtual esphome device and development using Windows (work in progress!) - #3 by Soepgroenten, but it seems difficult to port to my Raspberry Pi running docker containers; I’m looking for something more-in-house.)
Ric

You’re going to need to do template sensors for the math and/or mqtt sensors for the math and to attach the sensor to a device. Helpers (template sensors among them) can not be attached to a device, but you can attach an mqtt sensor (you’ll need an mqtt broker up and running) to a device by the device’s id (which can typically be found in the advanced diagnostics of a device).I attach mqtt sensors to the energy monitoring smart plug I use with automations to publish device status, last_completed_and last_emptied for my washer and dryer.

What are you trying to accomplish?
ESPHome config?
Trying to build an Automation or script?
trying to build an app? (add-on)
Trying to build an integration?

How to help us help you - or How to ask a good question.

Here’s what I have so far:

I have two BME688 devices attached to the same ESP32 device and referenced in the same configuration code. Since the entities are ‘local’ to the config code I can reference them easily, eg., for the mean:

  - platform: combination
    type: mean
    name: "BME688 Mean Pressure"
    id: bme688_mean
    sources:
      - source: pressure_adjusted_1
      - source: pressure_adjusted_2
    unit_of_measurement: hPa
    device_class: "pressure"
    state_class: "measurement"
    accuracy_decimals: 1

I can also obtain the local meteorological data for comparison, but when I try to, say, calculate the difference between the mean, above and the published met office value I run into the problem of how to reference a sensor not ‘local’ to the code:

  - platform: template
    name: "BME688 Pressure Varaince"
    id: bme688_var
    unit_of_measurement: hPa
    device_class: "pressure"
    state_class: "measurement"
    accuracy_decimals: 1
    lambda: 'return id(bme688_mean).state - id(met_office_yorkshire_pressure).state;'

It’s that last reference I can’t figure; the former is easy because it’s defined in the previous block. I’ve also tried ‘sensor.met_office_yorkshire_pressure’ but that’s also a no-no.

I’m trying to extend an ESPHome configuration, see my further comments regarding referencing ‘non-local’ sensor state data.
{‘non-local’: Not sure what terminology to use, I’m used to to C++ style referencing where variables are local to a function and are not visible outside that function or, if no other way, ‘globals’ can be referenced anywhere}

I think the issue is that ‘met_office_yorkshire_pressure’ exists only as an integration within Home Assistant and not ESPHome.

The issue is definitely with referencing the entity in HomeAssistant. Prefacing the id with ‘sensor.’ (as in 'sensor.met_office_yorkshire_pressure) reveals a useful error message regarding use of the homeassistant platform. Here’s my latest:

 - platform: homeassistant
    name: "BME688 Pressure Variance"
    entity_id: sensor.bme688_var
    unit_of_measurement: hPa
    device_class: "pressure"
    state_class: "measurement"
    accuracy_decimals: 1
    lambda: 'return id(bme688_mean).state - id(sensor.met_office_yorkshire_pressure).state;'

The referencing is still all wrong though… and lambda is disallowed in homeassistant platform.

OK, it seems the platform: homeassistant is there just to bring in the entity from HomeAssistant whereupon it can be given a new local id. That local id can then be used in the template platform to undertake the calculation. This seems to work:

  - platform: homeassistant
    name: "BME688 Pressure Variance"
    id: met_local_pressure    # new local id
    entity_id: sensor.met_office_yorkshire_pressure   # from HomeAssistant

  - platform: template
    id: press_var   # the 'local' entit to which is assigned the state value of 'sensor.met_office_yorkshire_pressure'
    unit_of_measurement: hPa
    device_class: "pressure"
    state_class: "measurement"
    accuracy_decimals: 1
    lambda: 'return id(bme688_mean).state - id(met_local_pressure).state;'

this correctly returns ‘press_var’