Device Register and Discovered but sensors dont show up

Hi All

I have a device which I created in ESPHome, after connecting it gets discovered by HA and added but no entities show up. I can see from the log on the device it thinkgs its publishing the state, and for another sensor showing a similar publish those sensors show up without any problems. I cant see any errors in either ESPHome or HA which gives me anything to go on. Any suggestions would be greatly appreciated:

Device yaml:

esphome:
  name: energy-meter
  includes:
    - powersensor.h
    - EmonLib.h
    - EmonLib.cpp
  
esp32:
  board: esp32-c3-devkitm-1
  framework:
    type: esp-idf
    
sensor:
  - platform: custom
    lambda: |-
      auto my_sensor = new MyPowerSensor();
      App.register_component(my_sensor);
      return {my_sensor->realpower_sensor, my_sensor->apparentpower_sensor, my_sensor->powerfactor_sensor,my_sensor->voltage_sensor,my_sensor->current_sensor};
      
    sensors:
    - name: "RealPowerSensor"
      unit_of_measurement: W
      accuracy_decimals: 2
    - name: "ApparentPowerSensor"
      unit_of_measurement: VA
      accuracy_decimals: 2
    - name: "PowerFactor"
      accuracy_decimals: 2    
    - name: "Voltage"
      accuracy_decimals: 2  
      unit_of_measurement: V
    - name: "Current"
      accuracy_decimals: 2   
      unit_of_measurement: A    
      
# Enable logging
logger:

# Enable Home Assistant API
api:

ota:
  password: "c0b9ee79e85817047d14787296677ce2"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot in case wifi connection fails
  ap:
    ssid: "Energy-Meter Fallback Hotspot"
    password: "E1FfewQ3GhUn"

Logs from device

10:57:48][D][api:102]: Accepted ::FFFF:10.0.0.200
[10:57:50][D][sensor:125]: 'RealPowerSensor': Sending state -0.80951 W with 2 decimals of accuracy
[10:57:50][D][sensor:125]: 'ApparentPowerSensor': Sending state 8.39622 VA with 2 decimals of accuracy
[10:57:50][D][sensor:125]: 'PowerFactor': Sending state -0.09641  with 2 decimals of accuracy
[10:57:50][D][sensor:125]: 'Voltage': Sending state 240.29095 V with 2 decimals of accuracy
[10:57:50][D][sensor:125]: 'Current': Sending state 0.03494 A with 2 decimals of accuracy
[10:57:50][D][api.connection:827]: Home Assistant 2022.6.7 (::FFFF:10.0.0.200): Connected successfully
[10:57:52][D][sensor:125]: 'RealPowerSensor': Sending state -0.69010 W with 2 decimals of accuracy
[10:57:52][D][sensor:125]: 'ApparentPowerSensor': Sending state 8.28012 VA with 2 decimals of accuracy
[10:57:52][D][sensor:125]: 'PowerFactor': Sending state -0.08334  with 2 decimals of accuracy
[10:57:52][D][sensor:125]: 'Voltage': Sending state 236.98177 V with 2 decimals of accuracy
[10:57:52][D][sensor:125]: 'Current': Sending state 0.03494 A with 2 decimals of accuracy

In HA, going to Settings → Devices & Services → Select the device in the ESPHome block, then click on the 1 device link gives me device info:

Device info
esp32-c3-devkitm-1
by espressif
Firmware: 2022.3.1 (Jul 5 2022, 10:53:35)

but no sensors.

I have no idea why this would happen?

Weird, maybe try to reload the integration :point_down:

three points to open the menu and hit reload.

If that doesn’t do the trick use the same menu to delete the device from ha and than add it again to see if it makes a difference.

And just to be sure you maybe want to one more sensor that isn’t part of your custom one just to rule out it is a problem with that custom platform, maybe add a safe mode button, always worth it :wink:

# Example configuration entry
button:
  - platform: safe_mode
    name: "energy-meter restart (Safe Mode)"

Hey, thanks a lot for the reply

I have tried that unforunatly. I have also done delete from ESPHome, delete integration, clean and rebuild in esphome, when I install the firmware again it gets picked up again with the same result.

I do have another sensor with a lambda as well, and that one works fine which is the bit that really confuses me

Can you confirm that this is visible/useable in ha once you add it to your device :point_down:

if it works then the problem is your custom sensor

Ok so tried the button now but still no luck, can see it shows the date for the new firmware compile but no button or sensors :confused:

esphome:
  name: energy-meter
  includes:
    - powersensor.h
    - EmonLib.h
    - EmonLib.cpp
  
esp32:
  board: esp32-c3-devkitm-1
  framework:
    type: esp-idf
    
sensor:
  - platform: custom
    lambda: |-
      auto my_sensor = new MyPowerSensor();
      App.register_component(my_sensor);
      return {my_sensor->realpower_sensor, my_sensor->apparentpower_sensor, my_sensor->powerfactor_sensor,my_sensor->voltage_sensor,my_sensor->current_sensor};
      
    sensors:
    - name: "RealPowerSensor"
      unit_of_measurement: W
      accuracy_decimals: 2
    - name: "ApparentPowerSensor"
      unit_of_measurement: VA
      accuracy_decimals: 2
    - name: "PowerFactor"
      accuracy_decimals: 2    
    - name: "Voltage"
      accuracy_decimals: 2  
      unit_of_measurement: V
    - name: "Current"
      accuracy_decimals: 2   
      unit_of_measurement: A    
      
  - platform: template
    name: "Test"
    id: test
    lambda: !lambda |-
      return 548.27;    
  - platform: template
    name: "Test2"
    id: test2
    lambda: !lambda |-
      return 1234.1234;        
# Enable logging
logger:

# Enable Home Assistant API
api:

ota:
  password: "c0b9ee79e85817047d14787296677ce2"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot in case wifi connection fails
  ap:
    ssid: "Energy-Meter Fallback Hotspot"
    password: "E1FfewQ3GhUn"

# Example configuration entry
button:
  - platform: safe_mode
    name: "energy-meter restart (Safe Mode)"    
    

Sorry, it didnt update on my 1st reply. Did try it now but no luck

Well, at this point to get rid (just for test) of your custom sensor to see if the button still isn’t available is the best choice to narrow done the troublesome component

Yes, it does seem that the custom sensor is the cause. If I remove the custom sensor the button shows up. If I then put the custom sensor back, then neither the button or the sensors shows up

Ok, some more troubleshooting:
This does work

esphome:
  name: energy-meter
  includes:
  #   - powersensor.h
    # - EmonLib.h
    # - EmonLib.cpp
  
esp32:
  board: esp32-c3-devkitm-1
  framework:
    type: esp-idf
      # auto my_sensor = new MyPowerSensor();
      # App.register_component(my_sensor);
      # return {my_sensor->realpower_sensor, my_sensor->apparentpower_sensor, my_sensor->powerfactor_sensor,my_sensor->voltage_sensor,my_sensor->current_sensor};
    
sensor:
  - platform: custom
    lambda: |-
      Sensor *realpower_sensor = new Sensor();
      realpower_sensor->publish_state(123);
      return {realpower_sensor,realpower_sensor,realpower_sensor,realpower_sensor,realpower_sensor};
      
    sensors:
    - name: "RealPowerSensor"
      unit_of_measurement: W
      accuracy_decimals: 2
    - name: "ApparentPowerSensor"
      unit_of_measurement: VA
      accuracy_decimals: 2
    - name: "PowerFactor"
      accuracy_decimals: 2    
    - name: "Voltage"
      accuracy_decimals: 2  
      unit_of_measurement: V
    - name: "Current"
      accuracy_decimals: 2   
      unit_of_measurement: A   
      
# Enable logging
logger:

# Enable Home Assistant API
api:

ota:
  password: "c0b9ee79e85817047d14787296677ce2"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot in case wifi connection fails
  ap:
    ssid: "Energy-Meter Fallback Hotspot"
    password: "E1FfewQ3GhUn"

# Example configuration entry
button:
  - platform: safe_mode
    name: "energy-meter restart (Safe Mode)"    
    

so my guess would be that my error is in this bit:

#include "esphome.h"
#include "EmonLib.h"

#define VOL_ADC_INPUT 3
#define CUR_ADC_INPUT 1
#define emonTxV3 1

class MyPowerSensor : public PollingComponent, public Sensor {
 public:
  EnergyMonitor emon1;
  Sensor *realpower_sensor = new Sensor();
  Sensor *apparentpower_sensor = new Sensor();
  Sensor *powerfactor_sensor = new Sensor();
  Sensor *voltage_sensor = new Sensor();
  Sensor *current_sensor = new Sensor();
  MyPowerSensor() : PollingComponent(1000) { }
  
  void setup() override {
   adc1_config_channel_atten(ADC1_CHANNEL_3,ADC_ATTEN_DB_11 );
      adc1_config_channel_atten(ADC1_CHANNEL_1,ADC_ATTEN_DB_11 );
   // analogSetPinAttenuation(VOL_ADC_INPUT, ADC_11db);
   // analogSetPinAttenuation(CUR_ADC_INPUT, ADC_11db);

   emon1.voltage(VOL_ADC_INPUT, 193, 0);
   emon1.current(CUR_ADC_INPUT, 7.5);
  }
  
  void update() override {
   emon1.calcVI(20,2000);
   float realPower = emon1.realPower;
   realpower_sensor->publish_state(realPower);
   float apparentPower = emon1.apparentPower;
   apparentpower_sensor->publish_state(apparentPower);
   float powerFactor= emon1.powerFactor;
   powerfactor_sensor->publish_state(powerFactor);
   float voltage= emon1.Vrms;
   voltage_sensor->publish_state(voltage);
   float current = emon1.Irms;
   current_sensor->publish_state(current);
   }
};

but I cant see anything that looks wrong :confused: