MCP3008 not exposing its channels to Home Assistant

Hi,

First post here!

I’m using an ESP32 + MCP3008 (8 channels 10bit ADC) to get more precise readings than the ESP32 built-in ADC.

Everything is working fine except for the fact that the channels that are exposed (named) as-is and not consumed by another sensor don’t show up in HA (but I see them in the logs)

I deleted all ESPHome devices in HA, restarted HA, no luck (I have 6 entities showing)
Updated ESPHome from 1.15.1 to 1.15.2

Here’s a sample of my .yaml file (theses DONT show in HA)

 - platform: mcp3008
    number: 2
    id: atl_boiler_hot_temp
    name: "atl_boiler_hot_temp"
    update_interval: 10s
    unit_of_measurement: "°C"
    icon: "mdi:temperature-celsius"
    filters:
      - calibrate_linear:
          - 0.46 -> 53.0
          - 1.64 -> 21.0

  - platform: mcp3008
    number: 3
    id: atl_boiler_cold_temp
    name: atl_boiler_cold_temp
    update_interval: 10s
    unit_of_measurement: "°C"
    icon: "mdi:temperature-celsius"
    filters:
      - calibrate_linear:
          - 0.46 -> 53.0
          - 1.64 -> 21.0

  - platform: mcp3008
    number: 4
    id: adc_hot_psi
    update_interval: 10s
    name: atl_boiler_hot_pressure
    icon: "mdi:gauge"
    unit_of_measurement: "PSI"
    internal: false
    filters:
      - calibrate_linear:
          - 0.105 -> 0.0
          - 0.95 -> 30.0

  - platform: mcp3008
    number: 5
    id: adc_cold_psi
    update_interval: 10s
    name: atl_boiler_cold_pressure
    icon: "mdi:gauge"
    unit_of_measurement: "PSI"
    internal: false
    filters:
      - calibrate_linear:
          - 0.105 -> 0.0
          - 0.95 -> 30.0

None of theses sensors appear in HA

But channels 6-7 are ‘consumed’ by an Resistance -> ntc and they show up as entities

These DO show up in HA (with right values)

# =============================
#Floor Temp ADC + R + NTC
#
  - platform: mcp3008
    number: 6
    id: adc_floor_temp
    update_interval: 10s

  - platform: resistance
    sensor: adc_floor_temp
    configuration: DOWNSTREAM
    reference_voltage: 1.024 
    resistor: 10.0kOhm
    id: r_floor_temp

  - platform: ntc
    sensor: r_floor_temp
    calibration:
      b_constant: 3950
      reference_temperature: 25°C
      reference_resistance: 10kOhm
    id: atl_floor_temp
    name: "atl_floor_temp"

Ok, here’s the solution I found ::

I create a custom sensor that simply act as a Pass-Thru and this sensor does get exposed in HomeAssistant

PassThru.h

#pragma once

#include "esphome/core/component.h"
#include "esphome/components/sensor/sensor.h"

namespace esphome {
namespace passthru {

static const char *TAG = "passthru";


class PassThruSensor : public Component, public sensor::Sensor {
 public:
  void set_sensor(Sensor *sensor) { sensor_ = sensor; }

  void setup() override {
    this->sensor_->add_on_state_callback([this](float value) { this->process_(value); });
    if (this->sensor_->has_state())
      this->process_(this->sensor_->state);
  }
  void dump_config() override;
  float get_setup_priority() const override { return setup_priority::DATA; }

 protected:
  void process_(float value);
  sensor::Sensor *sensor_;
};

void PassThruSensor::dump_config() {
  LOG_SENSOR(TAG, "PassThruSensor Sensor", this);
}

void PassThruSensor::process_(float value) {
  if (isnan(value)) {
    this->publish_state(NAN);
    return;
  }
  ESP_LOGD(TAG, "'%s' - Value %.1f", this->name_.c_str(), value);
  this->publish_state(value);
}


}  // namespace passthru
}  // namespace esphome

And here’s a fragment of the config :slight_smile:

esphome:
  name: heated_floor_control
  platform: ESP32
  board: esp32doit-devkit-v1
  includes:
  - passthru.h 
#.... 

- platform: mcp3008
    number: 5
    id: adc_cold_psi
   #moved the name from here to the custom sensor
    update_interval: 10s

  - platform: custom
    lambda: |-
      auto my_sensor = new esphome::passthru::PassThruSensor();
      my_sensor->set_sensor(id(adc_cold_psi));
      App.register_component(my_sensor);
      return {my_sensor};
    sensors:
      name: atl_boiler_cold_pressure
      unit_of_measurement: "PSI"
      accuracy_decimals: 1
      icon: "mdi:gauge"
      filters:
        - calibrate_linear:
            - 0.105 -> 0.0
            - 0.95 -> 30.0

I’m not versed enough (yet) in ESPHome architecture to figure why the MCP3008 sensor doesn’t expose it’s channels, hope I’ll find and fix it eventually

Cheers!