Custom Component (values not published)

I am trying to create a custom component. See the zip file. I see some values showing up on the website of this component and in Home Assistant. But the 3 values that should be published are invisible. When I change the filename of the *.cpp file I do not get any error message, so it seems that this file is not used during the creation of the download file. Print statements are not executed and no numbers are showing up for actual_power, totalenergy and dayenergy.
Please keep in mind that I not a software programmer.

Could someone help me with this? Thanks in advance.

I am trying to create a custom component. I see some values showing up on the website of this component and in Home Assistant. But the 3 values that should be published are invisible. When I change the filename of the *.cpp file I do not get any error message, so it seems that this file is not used during the creation of the download file. Print statements are not executed and no numbers are showing up for actual_power, totalenergy and dayenergy.
Please keep in mind that I not a software programmer.

Could someone help me with this? Thanks in advance.


//
// create a new component to read the Kostal Piko inverter
//
#include "esphome.h"
#include "esphome/core/log.h"
#include "mykostalpikosensor.h"


class mykostalpikosensor : public PollingComponent, public Sensor {
 public:
  //
  //
  Sensor *actual_power = new Sensor();
  Sensor *total_energy = new Sensor();
  Sensor *dayenergy    = new Sensor();

  mykostalpikosensor() : PollingComponent(polling_time) { }
  
  void setup() override {

     ESP_LOGCONFIG("Trying to connected to KostalPiko");
     serial.println("Trying to connect to KostalPiko");
     printf("Trying to connect to KostalPiko");
  
  }

  void update() override {
    // This is the actual sensor reading logic.
    
    ESP_LOGCONFIG("Update");
    //
    actual_power -> publish_state(10);
    total_energy -> publish_state(20);
    dayenergy    -> publish_state(30);
  }
};

//
// create a new component to read the Kostal Piko inverter
//
#include "esphome.h"

const int polling_time = 15000; // in millisec


class mykostalpikosensor : public PollingComponent, public Sensor {
 public:
 
 Sensor *actual_power = new Sensor();
 Sensor *total_energy = new Sensor();
 Sensor *dayenergy    = new Sensor();


  mykostalpikosensor() : PollingComponent(polling_time) { }  // in millisec = 1.5 sec

  float get_setup_priority() const override { return esphome::setup_priority::IO;}

  void setup() override {
    //
  }

  void update() override {
    //
  }
};

#
#---
substitutions:
  device_name: kostalpiko42
    
esphome:
  name: ${device_name}
  platform: esp32
  board: node32s
  name_add_mac_suffix: false
  project:
    name: Presto.kostalpiko42
    version: "0.1"
  includes:
    - mykostalpikosensor.h  
  on_boot:
    then:
      - if:
          condition:
            lambda: return id(has_key);
          then:
            - lambda: |-
                std::string key(id(stored_decryption_key), 32);
#                id(mykostalpikosensor).set_decryption_key(key);
          else:
            - logger.log:
                level: info
                format: "Not using decryption key. If you need to set a key use Home Assistant service 'ESPHome:  ${device_name}_set_kostalpikosensor_key'"

wifi:
  # remove leading '#' and fill in your wifi details
  ssid:  !secret wifi_ssid 
  password: !secret wifi_password

captive_portal:

# Enable logging id baudrate unequal to zero
logger:
  baud_rate: 115200
#  level: INFO
  level: DEBUG  
#  level: VERBOSE
#  id: 1

api:

ota:

web_server:
  port: 80

globals:
  - id: has_key
    type: bool
    restore_value: yes
    initial_value: "false"
  - id: stored_decryption_key
    type: char[32]
    restore_value: yes

sensor:
  - platform: custom
    lambda: |-
      auto my_sensor = new mykostalpikosensor();
      App.register_component(my_sensor);
      return {my_sensor->actual_power, my_sensor->total_energy, my_sensor->dayenergy};
    sensors:
    - name: "KostalPiko Actual Power"
      unit_of_measurement: W
      accuracy_decimals: 3
    - name: "KostalPiko Total Energy"
      unit_of_measurement: kWh
      accuracy_decimals: 3
    - name: "KostalPiko Day Energy"
      unit_of_measurement: kWh
      accuracy_decimals: 3

  - platform: uptime
    name: "KostalPiko Uptime"
  - platform: wifi_signal
    name: "KostalPiko Wi-Fi Signal"
    update_interval: 60s

Screenshot 2023-08-09 181532

Welcome @AlbertHoe.

Please take a moment to figure out how to format your code as per the forum rules, otherwise it is too hard for people to read it and you may not attract as much support. Cheers.

Mahko, thanks for the replay. But how can I create a nice looking/formatted topic in the MD format? Totally new to this format. Are there special tools for?

The answer to that is in the link he provided. It tells you, among others, how to format code.

Made some update to the topic. At least programming code is now visible. Still the begin of the topic has some problem. Takes some time to learn Markdown. :grinning:

1 Like

I’m not experienced enough with custom components to offer any useful advice, but what you’ve provided should now be enough for someone to help.

I assume you have already read the help docs?
https://esphome.io/custom/custom_component.html

I suspect someone may ask for the logs when compiling but I’ll leave it to someone who can help to request more info if needed.

Good luck with the project.

Mahko, I used a lot of documentation and examples including the one in your link.