ESPHome homeassistant.event

How do I create a homeassistant.event from a lambda expression (script) in Esphome? And how do I send the data with it?

Yes, I know about that method.
But I have a lambda (c++ code) in which I want to start an event. How do I do that?

Found it after a little digging in the source code files:

        auto capi = new esphome::api::CustomAPIDevice();
        capi->fire_homeassistant_event("esphome.touch1_event", 
                                       {{"channel", ch},{"data", onoff}});

I don’t know if this is the official way, but it works.

2 Likes

@helgemor you are allocating memory from the heap, so don’t forget to free it by calling the delete operation. Or just declare the object on the stack. Otherwise, sooner or later you will be left without free memory and the device will reboot.

esphome::api::CustomAPIDevice capi;
capi.fire_homeassistant_event("esphome.touch1_event", 
      {{"channel", ch}, {"data", onoff}});
1 Like