ESP to pass string to input_text in HA

As the title indicates, how do I pass a string from ESP to Home Assistant?
Neither of the “homeassistant.service:” below ever change the value of wash_person, a HELPER text_input in Home Assistant. Pictures below. The top one (5s interval) was created as a debug. It is the bottom (15s interval) that I really want to work. The text_input value is always set to “Unknown”

The ESP has been granted permission to perform Home Assistant actions.

interval:
  - interval: 5s
    then:
      homeassistant.service:
        service: input_text.wash_person #HELPER in HA
        data:
          value: "ABC123"
  - interval: 15s
    then:
      - if:
          condition:
            - lambda: |-
                return !id(recent_touch);
          then: 
            - light.turn_off:
                id: backlight
            - if:
                condition:     # id(laundry_person_wash) != ""
                  lambda: |-
                    return !id(laundry_person_wash).empty();
                then:
                  homeassistant.service:
                    service: input_text.wash_person #HELPER input_text in HA
                    data:
                      value: id(laundry_person_wash) #Global string in ESPHome


It is amazing how online code looks different after I make an online question.

This code works for the first interval

interval:
  - interval: 5s
    then:
      homeassistant.service:
        service: input_text.set_value    #<< changed
        data:
          entity_id: input_text.wash_person  #<< added
          value: "ABC123"

UPDATE:
Unfortunately, this code does not pass the string to HA,
If I change the very last line of the code above with the last line from the code below, it does not work.

interval:
  - interval: 5s
    then:
      - if:
          condition:
            - lambda: |-
                return !id(laundry_person_wash).empty();
          then:
            homeassistant.service:
              service: input_text.set_value 
              data:
                entity_id: input_text.wash_person #HELPER input_text in HA
                value: id(laundry_person_wash) #Global string in ESPHome

ESPHome does not like this line and I don’t know any alternative

value: id(laundry_person_wash)

how do I pass a string from ESP to Home Assistant

Are you trying to create a text sensor?

BTW, it appears that homeassistant.service is now called homeassistant.action in the documentation. Also that homeassistant.action has a data configuration option for static data - so you probably want the data_template and variables.

Something like

   - homeassistant.action:
        action: input_text.set_value
        data:
          entity_id: input_text.wash_person          #HELPER input_text in HA
        data_template:
          message: "{{ var }}"
        variables:
          var:  'return id(laundry_person_wash).state;'

@donburch888 Thank for the heads up
I changed my my code to “homeassistant.action:” for future compatibility.
https://esphome.io/components/api/

ESPHome did not like the .state in this line

var:  'return id(laundry_person_wash).state;'

In my many Google searches, I saw that ESPHome YAML may not always align with YAML that works in Home Assistant. Maybe this is the case?

Here is the code that ultimately worked for me

            homeassistant.action:
              action: input_text.set_value 
              data:
                entity_id: input_text.wash_person #HELPER input_text in HA
                value: !lambda |-
                  return id(laundry_person_wash);

I am not sure why, but !lambda works while lambda (without !) does not.
I am presently using ESPHome 2026.3.2

When you use lambda on any parameter on esphome, you need !lambda to tell that you use c++ for the value.

Huh? Can you explain further or provide a link ?

Any templatable parameter can be done with !lambda. If you browse esphome documentation, you will find hundreds of examples like:

    # Templated
    - light.turn_on:
        id: light_1
        brightness: !lambda |-
          // output value must be in range 0 - 1.0
          return id(some_sensor).state / 100.0;

YAML is not a “language”, but a framework (or syntax) for configuration files.

The ESPHome compiler reads the YAML configuration file and compiles/links/runs the ESPHome components that were indicated in the configuration file.
Home Assistant also uses the YAML style for its configuration files - but the components used by HA are not the same as those used by ESPHome.

As for lambdas … that is a method for placing actual C++ program code into ESPHome yaml files. It is very powerful … but very confusing because the program code and way of accessing variables in C++ is way different. What seems like a simple ESPHome field like a sensor is exposed to C++ as a structure which may have an attribute called “state” containing the actual current value. ESPHome globals are however simple C++ variables and so don’t have a state attribute. And so it goes…
Not being fluent in C++ programming I try to avoid doing much in lambdas, mostly copy and paste simple code … but on one occasion a dozen lines of C++ replaced a couple of pages of YAML.

I have the same problem, and and even then I can spend hours trying different tweaks before getting the syntax right. I eventually found useful documentation for lambda under Templates.
My explanation would be that some places in ESPHome YAML files expect lamdas (eg lamda filters and lamda calls) - but if you want to add some C++ functionality to another component configuration (such as the homeassistant.action: data: value: in your code above) you will need to use the !lambda syntax.