Can anyone advise how to call HA services from within lambda functions? I’ve had a hunt around and couldn’t find any examples (and the Native API component page is fairly sparse about the what globals, objects, etc. are available within the lambda functions).
My use case is a rotary encoder modifying a global variable to move a cursor on a screen. I have a separate boolean_sensor which I’m looking to create a lambda action within the on_click trigger. I want to use a switch statement to fire different Home Assistant services depending on the value of the global variable.
It’s pretty straight forward. The only required key is the service: key. If you need to pass data like in the example to the service, then you include the data: key. Of course follow proper formatting of your YAML. This all goes in your ESPHome node, not in HA.
Thanks, yes - I know about being able to use the homeassistant.service action in automations and scripts, but my use case is significantly more simple in C++ rather than YAML.
I’d had a dig through the code and not seen anything obvious, but I’ll go back and see if I can figure something out.
Ahhh yes. You’re using the action there. I went a bit deeper for simplicity.
Keep in mind Otto is a refactor ninja and this is not documented/supported api. So be ready to face a compilation error in future versions.
In the current version set_service and set_data seems to be absent. Rather there is add_data, add_data_template, add_variable functions.
api::HomeAssistantServiceCallAction<> *ha_service;
ha_service = new api::HomeAssistantServiceCallAction<>(&*api_apiserver, true);
ha_service->add_data("entity_id", "input_boolean.cycle1_enable");
ha_service->play();
I made it this far but can’t figure out how to set the service to call. There seems to function called TEMPLATABLE_STRING_VALUE but I couldn’t get it to work. I would have set the service variable directly but it is private.
Also, I tried this in desperation, doesn’t work and makes it disconnect from HA and reconnect.
Hi, I’m having this exact problem. I have a custom component that defines a callback function. Inside this callback I want to call a HA service, but all this logic is defined as a lambda. Is there some new info about how to do that?
I’m also trying to call a home assistant service from within a lambda in ESPHome.
I’ve tried the various snippets of code in this topic but couldn’t get anything working.
Does anybody have a working example of how to call a home assistant service from within lambda code in ESPHOME?
Thanks!
I’m trying to call the service light.turn_on with rgb_color parameter, which requires an array of integers (I’ve tested and it also works with an array of strings of numbers).
This is how to call the service in Home Assistant:
Any idea on how to pass the array thru the API in a lambda call?
The resp.data.push_back(entity_id_kv); works fine with strings or numbers (converted to strings), but I’m struggling with this array for rgb_color.
Compiling .pioenvs/esphome-web-6a0ed4/src/main.cpp.o
src/main.cpp: In function 'void setup()':
src/main.cpp:2024:52: error: 'MY_API_KEY' was not declared in this scope
api_id = new globals::GlobalsComponent<char[50]>(MY_API_KEY);
I have searched the forums and done web searches, and I don’t see where/how api_id is supposed to be set.