Code not compiling although YAML syntax is correct

I have a persistent problem with a (GEMINI generated) code that will not be compiled. Tested the YAML syntax with syntax checker like yamllint.com and seems correct.

text_sensor:
  - platform: homeassistant
    entity_id: sensor.owg_g32_active_grill
    id: active_grill_name
    internal: True
    on_value:
      - if:
          condition:
            lambda: !lambda |-
              return !id(g32_zone1).has_entity_id();
          then:
            - script.execute: set_grill_entity_ids 

script:
  - id: set_grill_entity_ids
    then:
      - lambda: |-
          std::string grill_prefix = id(active_grill_name).state;
          
          std::string new_zone1_entity_id = "sensor." + grill_prefix + "_g32_zone_1";
          id(g32_zone1).set_entity_id(new_zone1_entity_id);

The result looks like follows:

INFO ESPHome 2025.6.3
INFO Reading configuration /config/esphome/g32-display.yaml...
Failed config

script: [source /config/esphome/g32-display.yaml:797]
  - id: set_grill_entity_ids
    then: 
      - 
        Couldn't find ID 'new_zone1_entity_id'. Please check you have defined an ID with that name in your configuration. These IDs look similar: "set_grill_entity_ids".
        lambda: !lambda |-
          std::string grill_prefix = id(active_grill_name).state;
          std::string new_zone1_entity_id = "sensor." + grill_prefix + "_g32_zone_1";
          id(g32_zone1).set_entity_id(new_zone1_entity_id);
    mode: single
    parameters: {}

The code seems to be straightforward and I am lost atm. Can someone please help?

BR, Jörg.

Why wont let it you compile it? Can you maybe post the errormessage?

The error gives you the problem. The ID new_zone1_entity_id sensor isn’t defined anywhere. I see you used that name as a string variable in the lambda, but a string variable isn’t an ID. An ID is set when you define a sensor.

It would help if you posted your entire YAML and not a snippet.

GEMINI-hallucinated, more like. Can you point to a reference document for the has_entity_id() function you’re calling?

The YAML may be syntactically-valid, but that doesn’t mean it works. Perhaps start from scratch: explain the situation and what you’re trying to do.

I missed the AI created part. Yea, that probably gave the OP syntactically correct garbage.

Ok, I did not expect that GEMINI would use non-existing functions, my first attempt using AI for code generation …

But I can just omit that function and the problem remains the same.
Error message says that it 'Couldn’t find ID ‘new_zone1_entity_id’.
I do not use this as an ID! I have it declared as a string and this string is used in id(g32_zone1).set_entity_id(new_zone1_entity_id).

Where is my fault ?

It is not your fault, but really, AI only sounds right, but isn’t. Starting from scratch is indeed the right approach. Start by telling what you want to achieve.

Pretty sure the set_entity_id() method buried in the ESPHome HA code isn’t exposed for public use either.

OK. I use a HACS integration which looks for Otto Wilde G32 grills in the Wifi network and when it finds one, it creates HA entities for 4 zone temperatures of the grill named ‘sensor.nickname_g32_zone_1’ to ‘sensor.nickname_g32_zone_4’ where ‘nickname’ is a name string read from the grill. If you own more than one of these grills, their names will have to be different and you get different entities in HA.
Now I have an ESPHome Display which displays these zone temperatures and which shall connect to HA, read if there is a grill (and its entities) present, read the nickname and shall display the abovementioned temperatures (entities). I do not want to hardcode the entity names in the Display, but they shall be dynamic!

The code above seemed to do the trick for me in an rather elegant way, but maybe this is not possible at all (redefine entity names on the fly)?

BR, Jörg.

…if it worked…

If I had to do this, I’d do the work in HA by creating template sensors (e.g. grill1, grill2 etc, up to the maximum number of grills you’re likely to have) that pull in the status of the dynamically-named grill entities. The ESP code would then just reference those hard-coded template sensor IDs, and read the nickname for the grill from them.

If you want help with that, we’d need to see the sort of sensor(s) that the integration is creating.

1 Like

GEMINI made me think that dynamically defined entity names would work in ESPHome! OK, that does not seem to be true.
I am in contact with the developer of the HACS G32 grill integration and he will now introduce extra fixed names for the entities, so that this problem will be solved soon.
But it looked like a sleek solution … :blush:

Thanks for all your help!
Jörg.

1 Like

set_entity_id expects (as the name strong implies) an entity_id as an argument. In this case it’s looking for the entity_id new_zone1_entity_id and not finding it. If you want the VALUE of new_zone1_entity_id to be used as the entity_id you’ll need to do something different.

I’m going to say again that without the actual complete YAML (especially given the garbage AI stuff we’re starting with), it’s going to be hard to help you.

I have not found an single example, for which purpose set_entity_id() can be used meaningfully.
GEMINI suggested that it can be used to replace a spaceholder ID (g32_zone1) later during runtime with a dynamically ‘created’ ID (in this case 'new_zone1_entity_id). OK, this seems to be wrong, and as far as I understand the ESPHome documentation there is no way to dynamically change IDs during runtime. Is this correct?

It wouldn’t matter if it would work anyways. Home assistant only honors the original entity_id when the entity is first onboarded. Any entity_id change beyond that is ignored because it will break automations.

Thank you . But I do not quite get the meaning of your answer. I do not want to change any ID in HA, I want to adapt the ESPHome reference to this ID in ESPHome.
If the integration decides to create an ID sensor.my_grill_temperature1, where my_grill is the serial number of my grill, I want my ESPHome display to automatically adapt to this ID.
The ID is never changed in HA after first installation!

I found a way now to get around this problem. I use a substitution and construct the ID strings during compile time (so easy, sorry that I made such a mess from it).

substitutions
..
  g32_nickname: "elgrillo"
..

sensor:
  - platform: homeassistant
    entity_id: 'sensor.${g32_nickname}_zone_1'

Thanks very much for all your help and suggestions, Jörg.

1 Like

This sounds like the same problem i have with the old lady!! She’s always compiling aka complaining even though im always right about everything!

If you find a Mute button for them, LMK!!