I haven’t done anything using on_boot yet - want to deal with one thing at a time.
I’m also having trouble understanding string manipulation and usage in YAML. For instance, when I’ve tried to use logger.log "The new value is..."
, I’ve had trouble and have tried several different formats to be able to specify the text in a log entry. I found that I’d get gibberish and, no matter what I changed the text to, it was the same gibberish, which makes me think instead of getting the text, I’m getting a pointer to the text. I haven’t done anything in C++ in over a decade, so I’m not sure how to specify that I want what is being pointed to, not the pointer.
Here’s what I was using just to read the variable and have it printed to the log:
on_value:
- logger.log:
format: "DEBUG: Name changed to: %s"
args:
- MasterSwitchName.c_str()
(That was within the text component - going to paste in the entire text component after I removed that, below.)
So, somehow, with that, I was getting what looks more like a pointer than the text being pointed to. (Unless there’s some conversion issue, like C++ is reading UFT8 and YAML is reading ASCII, which I doubt is the issue.)
So I went on and removed the logging component and added in the lamda you provided. Here’s the full section:
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:
- lambda: |-
if (!x.empty()) {
id(MasterSwitch).set_name(x.c_str());
}
I used a different format than what you had, since I was using what I had been from earlier to display and let me change the value.
I found a few issues with the name of the master switch not showing up sometimes, but I think that’s another issue, so we can set that issue aside for now. I did find when I changed it, I would have to reload the web page to get it to show. (I think I may have had to reboot the chip - not sure at the moment.) But when I did that, here’s what I got:
Which looksl ike another pointer instead of the text. I got other things, too, once I got this:
I need to do more testing, but I have to take care of a few things with the family before I have time to do it. What I get from this:
- This is changing the name of the entity. I have not registered this in HA at this point, and don’t want to until I know I’ve got things working. Don’t want to keep adding and removing and dealing with extra things like that. Just want to make it work on the chip first.
- Somehow, the lamda, and the function I used to read the string, are not getting the text, but seem to get a pointer, or, from looking at that last screenshot, maybe it’s treating a string as a pointer to some random spot in memory.
Related to this: I mentioned I am having a hard time understanding string handling in YAML, or, at least, in ESPHome. It looks like a lot of it is done with C++ in lambda functions. Is that true? I’ve searched, but can’t find anything about how to work with strings (like compare them, see if they’re empty, or insert them into text, or set something else equal to them) in the YAML file. If anyone can tell me more about that or if there’s a page that covers that, it’d help me understand what’s going on.