Changing name of Binary Sensor component - Inconsistent Results

I have one ESPHome device with 5 Binary Sensor entities on it. After testing, each one will read a toggle switch. I want to be able to change the names of these sensors from the web interface. I’ve received a lot of help in parts of this on this forum. On this thread I’m discussing one issue that’s making this difficult to do.

Here’s the parts of the YAML file that include the Binary Sensor and the Text Template I’m using to change the name of the Binary Sensor (the Text Template is first):

text:
  - platform: template
    id: MasterSwitchName
    name: "Master Switch Name"
    optimistic: true
    min_length: 0
    max_length: 64
    initial_value: "Master Switch"
    restore_value: false
    mode: text
    web_server:
      sorting_group_id: sorting_group_values
      sorting_weight: 2
    on_value:
      then:
       - logger.log:
           format: "DEBUG: Before name change, component name is: %s"
           args: ["id(PrimarySwitch).get_name()"]
       - logger.log:
           format: "DEBUG: New name from input: %s"
           args: ["x.c_str()"]
       - lambda: |-
           if (!x.empty()) {
             id(PrimarySwitch).set_name(x.c_str());
           }
       - logger.log:
           format: "DEBUG: Changed component name is: %s"
           args: ["id(PrimarySwitch).get_name()"]
          
binary_sensor:
  - platform: gpio
    id: "PrimarySwitch"
    name: "CNC System Master"
    web_server:
      sorting_group_id: sorting_group_master
      sorting_weight: 1
    pin:
      number: ${MasterSwitchPin}
      mode:
        input: true
        pulldown: true
    filters:
      delayed_on: 10ms
    device_class: power

(For testing the MasterSwitchPin can be replaced with any GPIO that can be used for binary input.)

For my explanation, it’ll help to see a screenshot fo the ESPHome webpage:


I know it’s not a complex setup, but, for convenience, I’ve boxed the important components. The red is around the status display for the Binary Sensor for the master switch, the one I’m working with. The blue box is around the Text Template for changing the name.

In the Text Template component, when the value of the text is changed, I do 4 things:

  1. I log the current name of the master switch Binary Sensor.
  2. I log the new value of the text field in the Text Template.
  3. I set the value of the name field in the master switch to the new value of the Text Template.
  4. I check if this worked by reading the name from the main switch and logging it.

I think this is pretty solid: It verifies I can read the name, then I can verify I’ve written the new text to the name.

I have had results that are so inconsistent that I can’t keep track of, or pin down, just what is going on. I’ve had times where that section in the red box doesn’t even show up at all, other times when it’s there, but the name in it is gibberish.

Here’s the first name change, with the logging, where I change the text value from “Master Switch” to “Master Switch Alpha”:


Note that this starts with what appears to be an empty text string for the master switch name. I tried this a number of times and even restore_value: false for the Text Template, for some reason, the changes to the Binary Sensor survive a reboot, and even survive re-installing the system wirelessly (over wifi)! So the empty string for the Binary Sensor name came from another attempt, then a re-install. (That’s part of what’s confusing - how can the name for this component persist across a reboot without setting restore_value: true in the component?) And, even more, how can a variable survive a re-install from Home Assistant?

(I know I was using restore_value: false in the Text Template and the issue seems to be the value in the Binary Sensor was persistent - I was

What does show up, in this screenshot, is that when I updated the text in the Text Component, it did update the name in the Binary Sensor as well, since it was able to read it with a get_name() function.

However, when I try to reload the page, to see if the new name will show up on the listing in the section I boxed in red, it does not:


Note the section for the master switch is missing. I don’t know why, but I’m wondering if it’s because the name field is a null string, so it doesn’t have a label to print there.

Using the same code, with the same lambda functions, I have had inconsistent results, like this:


or this:

I have noticed the length of the new text string plays a part of what’s going on. I did a test with different length strings for new values. Note that any values that were less than 16 characters resulted in a gibberish string being found in the Binary Sensor name:

Even though the length of the new text seems to make a difference in what shows up in the logger as the Binary Sensor name, it doesn’t (consistently) change what shows up on the web page as the name of the Binary Sensor in that red square.

I saw one post that said that there was no string representation of components in memory, so that would explain why this is problematic, but if that were true, then the get_name() function would not work. I haven’t used C++ in over a decade, so I don’t know if I’m doing something wrong with a pointer, or if there’s a reason the length of the text makes a difference. The logs show the new text value is being stored in the Binary Sensor, but the new name of the Binary Sensor, when I reload the webpage, often does not show up or shows up as something other than what was stored there when the value was updated.

My goal is to be able to let the user (me, or friends or others, if this works well enough to publish as open source) pick a name for all 5 of the switches the Binary Sensors represent, so if they’re using this as just ESPHome, or using it with other ESPHome devices, they can reference a “Friendly name” instead of names like, “Master Switch,” “Switch 1,” “Switch 2,” and so on. If there is another way to get a friendly or name the user prefers for each switch to show up on the web page, I’m open to that. I really just want to it to be possible to use the web page to see the status with the names of each switch set to what the user wants. (I’m working on two other items that will work with this, both without Home Assistant, and with it, so those will be reading this page with http requests and I think that’ll be easier to set up if the user can pick their own component names.)

More information:

I made a few changes, like adding an extra logging function when I press the button connected to the Binary Sensor I’m working with, so when that button is pressed, it’ll give the name of that sensor. Then I installed the new version of the YAML file and this is what I got:


So on a reboot, after a new install of the YAML file from Home Assistant, without using restore_value: true on anything (other than a Number Template not connected to this), the value of the name in the Binary Sensor is either a series of spaces or no text at all.

After that, I tried another rename, and I got this:


Which goes with what I said earlier: If I use too short a name, I get gibberish. (I’ve set the min length to 1 character, but that doesn’t seem to matter.)