ESP custom component how to get entity values from home assistant

I carried out successfully a custom component in C++ (arduino like) on a ESP8266 follwoing the amazing tutorial here Custom Sensor Component — ESPHome
Mainly I did that to move a bit of “smartness” to the ESP in order to control in realtime some critical parameters which must safe independently from a latency or momentary wifi loss connection)
It works fine!
However I would pass to ESP8266 code (c++) dinamically some parameters from home-assistent entity (or helpers) instead of a hard-coding parametrization.
How to do that from the C-code of the custom component? How to access to the HomeAsssistent entities ?

I think a homesssistant sensor with an id

sensor:
  - platform: homeassistant
    name: "Parameter from HA"
    entity_id: some.ha_entity
    id: param

Then in your lambda / c++ code

id(param).state
1 Like

Yeah!!!
Thanks a lot nickrout !!

So when I add id to a sensor in HA and use id(param).state in esphome it will use the entity value of the sensor?

Now I have this:

  - platform: homeassistant
    name: "Daytime"
    entity_id: input_boolean.scene_daytime
    id: "daytime"
    on_value:
      - if:
          condition:
            lambda: 'return id(daytime).state == "on";'
          then:
            - light.turn_on: # RED
                id: neopixel
                brightness: 100%
                red: 100%
                green: 0%
                blue: 0%

But if what I ask is right I can change this and remove the whole platform part and use only the

id(param).state

in the esphome automations

You don’t add the id in your HA code, you add it in your esphome code. You still need the platform: homeassistant to get the value into the esp, then to reference that value in a lambda you use id(param).state

I hope that is clearer now.

Yes. The way how I already do :slight_smile:

Hello thanks for your suggestions, I have another related issue:

I would like to pass several H.A. entities value to the class of my custom component library
let’say the entity is given by a helper
her my C code

---C code---
my personal .h file 

class cESPCustomDevice : public PollingComponent, public Sensor { 
  public:
    float param1, param2, param3;
    cESPCustomDevice(float p1, float p2, float p3) : PollingComponent(100) { //costructor
      param1 = p1;
      param2 = p2;
      param3 = p3;
    };
	Sensor *MySensor = new Sensor();
....
	MySensor->publish_state(val);
}

so once defined in H.A. a helper “input_number.param1”
I have this YAML

---YAML code---

- platform: homeassistant
  name: "Param_1"
  entity_id: input_number.param1
  id: par1
	
- ...Param_2
- ...Param_3

- platform: custom
  lambda: |-
    auto esp_customcomponent = new cESPCustomDevice(id(par1).state, ... , ...);
    App.register_component(esp_customcomponent);
    return {esp_customcomponent->MySensor};

it works but my question is: there is a simpler solutions to pass the entity value “input_number.param1” in the YAML file instead to define a local entity “par1” and passing it ?

I tried with

- platform: custom
  lambda: |-
    auto esp_customcomponent = new cESPCustomDevice(id(input_number.param1).state, ... , ...);

and

    auto esp_customcomponent = new cESPCustomDevice(id(input_number.param1).currentstate, ... , ...);

But it doesn’t work. Any suggestions?
Thanks a lot.

I have managed to transfer data by using the constructor and geting my instantiated component, then updating it’s value using homeassistant sensor on_value …
It’s not my idea I just found it.