Reusable function for lambda's?

Tags: #<Tag:0x00007fc40e854308>

Hi all,

Still very new to ESPHome and finding it a very steep learning curve. Note that I am not using HA.

I would like to have some reusable functions that I can call in lambda’s. Can someone please point me in the right direction? I am not a C/C++ programmer and I’ve only used the Arduino IDE previously so if I need to write some C++ code, I might need a bit of a hand, sorry.

Thanks in advance.

You can just create a “.h” file next to your yaml file and reference it under includes like so:

In that header file you can just write C/C++ functions that you can use in lambdas. For example:

int add3(int x) {
    return x+3;

They should be able to access most of the normal ESPHome C++ api and also normal Arduino stuff and everything from the libraries you can add (also under “esphome:”)

1 Like

Oh and if you do not want to use C/C++ you can also look at scripts.

You can define a sequence of actions there (even with conditions) and call that from a lot of places in ESPHome.

(you should read the whole section about automations in the documentation)

1 Like

Many thanks for your responses, very helpful.

It was the C++ code that I needed as I need to manage some things in lambda’s that don’t really have an equivalent in ESPhome.

One area is some detailed timing - where I need the timing to be variable based on an inbound MQTT topic. This seems quite complex to achieve in yaml but should be fairly simple in code.

The other area is display positioning. I want to be able to configure pages using a more logical layout than having to trial and error pixel positions. Using rows and columns. Of course, these need to be based on either a fixed grid or on a grid that is dependent on the font size.

The other thing that I need custom code for is to integrate more of my M5stack Basic device functions like the card reader. I just about got the speaker working with existing yaml along with the Temperature/Humidity sensor in the base-unit. Buttons I’ve also done in yaml fairly easily.

Loving the fact that I can use a single set of yaml configurations though across multiple device types even between ESP32 and ESP8266 so despite the horribly steep learning curve, ESPHome is still proving its worth.

Depending on what you mean with “detailed timing” it may very well be possible with yaml and a little C in a lambda. Have a look at especially delay that can take the time from a lambda.

Compine that with the “on_json_message” trigger of the mqtt component: and you may be able to do it without a lot of C code.

That seems quite lot of work to do right. Maybe there a good librarys for that kind of thing? AdafruitGFX the most used library for graphics and text on displays is not good enough on its own.

I used to write my ESP32/8266 code in C/C++ with the Arduino IDE. But I have recently moved 100% to ESPHome. You can still use all the C/C++ but have a great base system for things like WLAN, OTA, MQTT, Home Assistant API, Time and so on. So even if you want more than just a simple sensor or switch ESPHome can be a good base for that.

If you have normal Arduino code most of the time it is easy to port it to ESPHome as a custom component. They also have a setup() and a loop() method.

Thanks, I think I’ve got it sorted. The MQTT msg is simply a command to change the timeout. For example, I’ve set my M5stack basic to turn off the backlight after a fixed 1 minute. But I want to be able to control that and make the time variable.

Indeed it is and it always surprises me that nobody has actually made the effort since, once done, it makes life so much easier. Line/column screen placement has been a fundamental of computing ever since displays existed. I shouldn’t need to keep messing with pixel placement. And I HATE having to repeat myself in code!

But I don’t have to get it perfect. It is enough to build up some standard variables that can be adjusted according to font size. Row placement is actually really easy. The font height + a line spacing variable and a top-left position. 3 variables and a bit of basic adding :slight_smile:

Horizontal positioning is a little harder when using variable-width fonts. But here we can take a leaf from web-based UX. Using a horizontal positioning grid.

I don’t like the AdaFruit library as it is far too complex and heavy.

Totally agree and I’m certainly getting there. Slowly. It has been a week or two of hard work getting my head around how it works and the documentation certainly needs some improvements to take out some of the assumptions so that beginners can make better speed. I think there is too much assumption that you know C++ and PlatformIO.

But I’ve already deconstructed my configuration so that I can reuse all of the common parts. I just need to now find the time to rebuild my various platforms.

Definitely some things I still need to learn how to integrate though.

SPIFFS for example, I’d like to be able to make use of that. I can do it in Arduino code so I know I’ll get there but surprised to see nothing for that built into ESPHome (unless I missed it). Reading from and writing to SPIFFS should be quite a common requirement I would have thought.

Battery management is another thing. Again, I’ve done that with Arduino code in the past so I know I’ll get there, just not had time yet.

Definitely getting there anyway and many thanks for your help.