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.
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
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;'
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.