You canāt pass a string to id. The preprocessor uses it to convert it to C++ format sht_dewpoint->update().
Components do not have a string name representation. So, if you want to execute a function on a component based on a string name: donāt see a way how this can be done.
Butā¦ components can have 1 or more sensors, each sensor has a name. If you want to execute a function on specific sensors: you can get the list of sensors through App. Below sample code to print all sensor names. You can modify it to get the sensor based on a string name.
auto sensors = App.get_sensors();
for(unsigned int i = 0; i < sensors.size(); i++)
ESP_LOGD("app", "Sensor: %s", sensors[i]->get_name().c_str());
Do note that there is a getter for each type of sensor. For binary sensors, need to use get_binary_sensors(). For text sensors, use get_text_sensors()
I have followed your code and I can get the name of the sensors, but I donāt know how can we acces to the state of each sensor with the recovered names instead of the id() method.
but how can I SET the state with this approach : I want to loop through the full set of binary sensors, find the one Iām looking for based on its name, and then SET its state
I had a similar issue and got help on the ESPhome Discord - I figured I should share it here incase anyone else needs to do something similar as this was the closest I found.