How does ESPHome differ from Home Assistant in parsing YAML files?

I’m learning to work with the YAML configuration file for ESPHome and, for some less used features, I find hits for Home Assistant and how to do something and not for ESPHome. If I find something that works in a YAML file for Home Assistant, how do I know if it’s the same for ESPHome?

For instance, does ESPHome handle things like Text Templates and Number Templates the same as HA does? Are things like handling values, comparing them, and so on, going to be the same in both?

It will not be. Especially templates. HA uses jinja and ESPHome uses C lambdas.

Use the ESPHome documentation. There is a search box on the front page.

That helps, since I know not to count on the HA docs.

I’ve been going through documentation, but there are things that aren’t covered as topics. I’ve also been using Google a lot. I think those that work with this a lot know it well, but there’s a gap in there. I’m getting used to reading code using lambdas and have used that some in what I’m doing, but there are still points that I either can’t find, or don’t know what terms to use for searching. For instance, I did find that, within the Text Template I’ve made, I can do this to get the text value:

    on_value:
      then:
       - logger.log:
           format: "DEBUG: Name changed to: %s"
           args: ["x.c_str()"]

So I’ve learned to use x.c_str() to get the value of that particular variable, but I only found that through example code and asking about it here. I couldn’t find the documentation that explained how to read the value of the string, or that x referred to the text that section was working with.

Now I’m running into something similar. I have a binary sensor:

binary_sensor:
  - platform: gpio
    id: "PrimarySwitch"
    name: "CNC System Master"
    ...

I have found that the name can be changed by using:

    on_value:
      then:
       - lambda: |-
           if (!x.empty()) {
             id(PrimarySwitch).set_name(x.c_str());
           }

from within the Text Template. I know that x.c_str() is retrieving the text value or state, but the resulting new name for PrimarySwitch is gibberish, which makes me think there’s a need for a pointer or pointer reference (not sure of the term, I haven’t used C++ in over 15 years) to actually read or write to the value of the name in PrimarySwitch. Yet I can’t find the documentation on how to address values within the Primary Switch binary sensor object.

It’s use like that does get called out at various points in the docs but it’s not always where you are looking.

@Mahko_Mahko, I can get the value or state of a Text Component, so that’s working. What I’m trying to do now is to access the name of a Binary Sensor. And I’m studying something now that indicates that might not be possible.

You can dig into the api reference too which is typically linked at the bottom of the page of a component.

https://esphome.io/api/classesphome_1_1_entity_base

https://esphome.io/api/gpio__binary__sensor_8h

https://esphome.io/api/entity__base_8cpp_source

Right now I’m studying this thread on the forum, from back in April. At one point (in the 2nd post on the thread), @ckxsmart states there is no string representation of components, so what I want to do may be impossible. Considering my inexperience with this platform and that I’ve forgotten everything I knew about C++ (other than that it uses a lot of pointers), it’s taking me time to parse all that’s said on that thread.

It may be best if I rethink what I’m doing, figure out my overall goal, and make a post that is specifically about this one point. I was trying to get an overall understanding so I could figure it out on my own, but it seems like this is a special situation that might not be something I can do.

Also, and this is part of what I’m trying to work out, in that post, the term “Component” is used. I’m not clear on the scope of some terms. For instance, I know “Device” would be my entire ESP32 system I’m working with, and an entity would be a sensor or switch on it. I’m not clear just what “component” covers - does it refer to any object created in the YAML file, for example? (I don’t think so, since there are string or number representations of Templates.)

This is a good plan I think.
Take a look here for tips on posts ( don’t be put off by the tone there).

You wouldn’t be the first to be a bit confused about scope of terms/ taxonomy as it can be hard to get a top level view. Keep at it.

I looked over the API page on the Binary Sensor that you linked to. There’s a link to the class, to this page:

https://esphome.io/api/classesphome_1_1gpio_1_1_g_p_i_o_binary_sensor

On that page, checking in inherited functions, I find this:

Screenshot 2024-11-17 at 2.58.49 PM

I think this is contrary to what I read on the other thread I linked to, since it shows there is a way to get or set the name of an EntityBase class, which would also let me get or set the name of a Binary Sensor. However, when I did that, with the text from a Text Template, I get gibberish as a result:

text:
  - platform: template
    id: MasterSwitchName
    name: "Master Switch Name"
    optimistic: true
    min_length: 0
    max_length: 64
    initial_value: "Master Switch"
    restore_value: true
    mode: text
    web_server:
      sorting_group_id: sorting_group_values
      sorting_weight: 2
    on_value:
      then:
       - logger.log:
           format: "DEBUG: Name changed to: %s"
           args: ["x.c_str()"]
          #  args: ['id(MasterSwitchName).state']
       - lambda: |-
           if (!x.empty()) {
             id(PrimarySwitch).set_name(x.c_str());
           }

I used the log function to verify that I was actually changing the value of the text, then used the lambda (given to me by someone else) to set the name of the Binary Sensor. Once I did that, I reloaded the webpage and the name of the sensor had changed, but was gibberish - not the text from the Text Template.

But I also notice the argument for set_name() is const char *name. Again, haven’t used C++ in a LONG time, but doesn’t the asterisk indicate something about a pointer? Not at all sure, but I’d basically like to set that name of the Binary Sensor.

(And if I can’t do that, I’d like some way to display the state of that sensor on the web page with a “Friendly Name” that can be changed, so if I have to create some other kind of component that can be renamed, that would report the state of the Binary Sensor, that would would be more complicated, but it would work.)

Also, along that line, if I have an ID and a Name for a Binary Sensor, and I add the device to Home Assistant, will Home Assistant identify the component by the name or ID? I realize if it identifies it by the name, that changing the name would make a mess on the Home Assistant configuration.