Who can Explain lambda in a way a boer can understand?

Okay, so I need my ESP to do stuff on its own without HA automations, all while I still use the ESPhome HA integration for monitoring.

I know how to do what i want to do on a standalone ESP using C and C++ and Arduino IDE, but then need to set up the MQTT part as well so that i can monitor it all on HA.

Now in my boer way of explaining:

Because the integration uses .yaml, it seems like I can only use lambda to make it work, and it seems like lambda is a way to use Python for the function.

Am i correct in believing that the compiler will then take care of the python code and send the correct whatever to the esp and that the complete code will reside on the esp once I am done.

I hope my question makes sense?

In ESPHome, a lambda is an arbitrary chunk of C++ code which gets compiled and uploaded to the ESP.

https://esphome.io/automations/templates#config-lambda

It looks a bit like Python because of the indenting - but the indenting is part of YAML. After |- YAML will read every following line with the same (or deeper) indentation as a single block of text.

Once the indentation ends, that block of text is sent to the C++ compiler to produce the code which is then uploaded to the ESP.

Wherever a lambda appears in the .yaml config file, that code will execute and the return value of the code (if any) will be the value set where the lambda was used.

The API used to access the other ESP functions is a bit awkward, but well documented on that site.

1 Like

Thanks sir, so in short, lambda is a way to get code that you would normally do on a standalone ESP to compile before its uploaded. All this is a shortcut to get the code compiled as EspHome does not allow or can not accept code in a way I am use to. So all the magic lies in the compiler and lambda tells it when to apply the magic.

Please remember, I dont have any qualification in coding or software development and struggle to find the correct terms to properly formulate my questions. Even my previous experience with coding was all self taught.

Pretty much. You can embed lambdas in most parts of the .yaml file, and the compiler takes care of the rest. Whenever that piece of yaml would execute the C++ code executes instead. Compiling and all that other jazz is hidden in the magic of the YAML parser.

1 Like

You are asking very good questions.

@JacoDeJongh , let me ask you a question?

Are you becoming a developer of Apps?
Yes, it is your drive. Go for it!!!
Not really, just a DIY interest … consider paying a developer.

The answers you get, they will get more and more integrated and involved because … 10 years experience as a developer takes 10 years to get.

Use it, or not … but it is a slippery slope, this development thing.
Paying someone to do it can save you many hours … and a lot of frustrations resolved because of experience from developers who has dunnit.

Trust me. Been there, done that.
… and because I have learnt that asking the right questions results in developers taking the time to answers, help.

Hi, just a proof of concept on an idea i have. Up to this point it is truly to solve a problem for myself and understanding each step of the way.

Jaco, to be honest, started in the baking industry in the 90s and became the youngest maintenance manager in the history of Sasko. I think I was not even 25. Those years most companies used contractors to do PLC programming but they were asking exuberant fees for that service. I told my manager that I will soon take care of that. Watched the guys when they were on site and soon after asked the manager to buy me an laptop and the software and interface cables I needed to communicate with the PLC’s on my plant. Bought a spare PLC of each make and sat in my office teaching myself how to program them. Not long after I was traveling to other bakeries in the group to assist with programming issues and PLC replacements. I know PLC programming is seen as easy compared to what software developers do, but it was a good start on this journey.

Forward a few years and I started on the mine in Phalaborwa as an electrician. Worked closely together with the instrumentation team and before the end of my first year on the mine they transferred me to the instrumentation department as an Instrumentation specialist. Me and Marius (Been there since the dawn of time) were in charge of the Honeywell DC system with a total of roughly 10000 I/O,s, the servers and everything needed to connect all the nodes together. We had to program in a language similar to Fortran. The two of us was part of less than 10 people left in SA that could still code in that language. because it was not a commonly used language,there was no training available for us so we had to learn from manuals printed in the 70s and 80s and then by trial and error got the code to compile. It took me roughly 18 months to fully grasp the language, but I managed. The mine were stunned as i was the first person after Marius that managed to learn the language. There was a few coders before me that they have tried to teach, but they all left because they could not succeed. And here is this electrician with self taught experience in machine coding that gets it right. A while after that, Marius had an accident and went on early retirement, leaving me alone to manage the plant. Till the time I resigned to pursue a career in solar, they could not find an replacement for Marius. I have been in that position for over 10 years, that is a long time for someone with no official qualification in software development to be in such an position for an internationally owned company.

This is just as info, I dont see myself as stupid when it comes to coding, but lack so much when it comes to modern languages like the software developers on this forum uses. This forum and the solar company created so many opportunities to meet people, and i must admit, I have met some of the most intellectually blessed people in my life through this forum (with a list of guys i hope to still meet in person). Some of the most brilliant coders is members here, I don’t even want to start mentioning names as i am afraid i will forget someone and offend him in the process. talking to these guys in person is humbling in so many ways. Sometimes I am to scared to even ask an question out of fear that I might sound stupid. I have all of their numbers and might as well ask them directly for advice, but only feel comfortable contacting two guys directly. Although they are both unbelievably good in their fields, it does not mean that they would be able to answer my questions as its not a given that they have worked with the integrations I chose to work with. I am sure they will be able to figure it out if they go read up on it, but i still feel bad asking them directly. Most of the realy realy good coders on this forum very seldom if ever answer coding related questions. They will participate in general discussions and I dont think anybody here would even suspect that they are coders.

I feel stupid to ask, i also feel stupid for not knowing, and most of all i feel stupid just talking to them. I ask questions here, and hope they will answer and give their opinions. I need to improve myself and learn modern languages, if only to not feel stupid. If i pay someone, I would not improve myself. If i truly can’t come right, i will pay someone.

Some time ago, I looked at different courses and wanted to enroll in them for online after hour classes to learn more, but @plonkster advised me not too. I can’t remember the reason, but I am sure it was a valid one.

2 Likes

Lambda is a Greek letter (λ), but as always certain conventions have grown around it in math and programming languages. The one that is important here, is a branch of mathematics known as “Lambda calculus”. It is a limited type of notation that allows you to write small functions, to make it really simple.

This made it into Python (and many others), and it allows me to write some simple computations inline. The terminology is then also borrowed here for this platform: Small inline functions of a sort.

As a python example, if I used something like functools.reduce (which reduces a sequence of items to a single item by successively calling a given function on the values), it allows me to reduce this:

from functools import reduce

def add_together(x, y):
    return x+y

reduce(add_together, [1, 2, 3, 4, 5])

To just this, by inlining the function, which is simple enough to be lamda’d.

from functools import reduce
reduce(lambda x, y: x+y, [1, 2, 3, 4, 5])

Edit: After looking at the ESPHome documentation a bit – I do not use it (yet) – it seems they are a bit cheeky with the term “Lambda”. It looks like it’s just a way to define a function you can call later.

Maybe they borrowed the term from Amazon, where lambdas are bits of programming small enough that you don’t need to run an entire (virtual) application server.

1 Like

I watched some tutorials till about 1:00 this morning and eventually found one that confirmed my suspicion.

EspHome was intended for Home automation integrations where the Automations were intended to be run from the HA side. Whenever you want to run all the code on the ESP without relying on the HA side or the wifi, while using ESP home, you need to use lambda’s to get the code compiled.

1 Like

This is arguably much more powerful than the Tasmota way. With Tasmota you can write some rules to fire on certain events (and this happens on the device), and it is probably sufficient for the kind of thing you’re doing. But having the full power of C, while more complex to get going, is so much more powerful, and probably more performant too.

1 Like

It does actually seem like a ‘real’ lambda. You can use a lambda in most parametric positions, and the code will be executed and the result placed in the parameter.

Template covers seem to be a way to wrap a lambda in a callable object that you can use like a normal function.

API looks ‘interesting’ - but if you can wrap your head around it, it should be possible to do just about anything.

2 Likes

Then this applies …

Note: The only stupid question is the one not asked. :slight_smile:

1 Like

There is no shame in asking a question, unless you have sought to portray yourself as an expert - then embarrasment is likely.

I am a trained developer - three years at Technikon at it then was. There was a mandatory requirement to take one year of accounting (since in those days we were almost certainly going to end up working on financial applications). I’d not done this at high school (I had a choice early on, but could not see what was coming in the future).

Anyway… the lecturer walks in, assumes everybody knows the rudiments, and starts drawing a ledger and a cashbook on the blackboard. Straight away my hand is up. “What’s a ledger?” “You don’t know? What are you doing here?” Well I had to be there. There was some giggling from various parts of the class.

Within 5 minutes I was asking him what a “journal entry” was. More giggling.

As time went by I carried on asking questions because I honestly knew nothing about this. They got less as the term went on, but I was still apt to ask about something that everybody else was taking for granted.

End of term we wrote our first exam. I had also hit the books hard.

Beginning of the second term he is giving us back our papers with the mark written on the front page (he didn’t call out the marks, which spared some folks some shame). When he called my name and I stood up he said “you will have to bring some ID that I can verify”. Lucky I had a student card and the old ID book present. He apologised and said he had to do it. I asked why. He said “look at the mark you got”.

A distinction.

Then he spoke to me in Afrikaans “jy’s nie neuskeurig nie, jy’s WEETkeurig.”

So because I’d been unafraid to show that I knew nothing I had been able to get the information that I needed and to absorb it.

Another interesting thing about all of this is that I was trained as a COBOL programmer (though I had one year of PL/1 and Dartmouth BASIC). I have never written a line of COBOL code for money (I do still have the text book).

When I started my first job as a programmer I immediately had to learn a new to me language. OK… not entirely new, because it was BASIC with some special features to support the multi-valued database that it was used with.

This then became my stock in trade. But since then I’ve had to do Pascal and C++ for a course at UNISA, and Perl, ABAP and Javascript for stuff at various jobs.

Now along this career line I worked for a man who was self-taught as a programmer. He was a chartered accountant. He told me that in any activity there are rudiments, and if you master those rudiments you can solve any problem. He used accounting as an example, saying that if you truly understand double entry accounting there is no accounting problem you cannot solve.

I think it’s the same with programming. I think the most valuable things I learned at Technikon were rudiments. Boolean algebra for one. Structured programming would be another. I find it still applies in the newer object oriented languages (though I first learned to program on a hobby machine that used a very stripped down version of BASIC that leaned very heavily on GOTO, so breaking the rules of structured programming was very likely and quite excusable). Things like flowcharting and truth tables.

And commenting. Lots of commenting. So when I look at a program 3 months down the line I know what I was trying to do (and so will any other developer).

In the days when I was having fun with Perl I wrote some code in that language that created a bit of a problem because it was being widely used but very few people understood it. Another developer who had no experience with Perl had to look at it. But she told me (and I took it as a complement) that because I’d commented the code so well she could understand what it was doing even though she didn’t understand the language.

So I think there are some rudiments that apply to programming in any language. After that it’s just getting used to the syntax of whatever it is.

I also have some books about programming. And one of them (“Code Complete” by Steve McConnell) puts forward the idea that you should write all the comments first, then write the code between the comments. He goes on to say that if you come to a comment and you can’t write the code, then you haven’t though about that particular task properly, or haven’t stated it clearly.

So that’s where your way of thinking and your way of expressing yourself come into play.

So I believe that. And I have told managers that: If a person really knows about programming (which you don’t always get from “teach yourself…” books) then switching to another language means some time reading a manual (which I had to do for Visual Basic in somewhat unfortunate circumstances). They don’t like to hear that. They want experts on whatever it is that they’ve backed. And “expert” means “certified”.

This is starting to change now, because companies are realising that finding people who can program in whatever is easy, but finding somebody who knows their business is harder.

Anyway… look on the bright side. Asking questions in this case is a way of getting information. As long as you take it on board you’re doing OK and nobody should resent giving it to you (unless you ask the same question too many times). And there are some rudiments - my theory goes - that once you understand and can use put you at least half way to being able to program in most languages.

Too many programming schools and books teach you all about the language but not enough about the boolean stuff and how to simply all your ORs and Ands and combinations thereof.

2 Likes