Help creating conditional output in yaml

I’m working to create a fan based on the CC1101 chip (RF) and I have the signal processing part down, but now need to create my esphome components. Here’s the write action that I currently have for my fan:

    write_action:
      - lambda: get_cc1101(transciver).beginTransmission();
      - remote_transmitter.transmit_raw:
          code:
            - lambda: |-
                if (state < 0.25) {
                  return "[346,-692,346,-692,346,-692,346,-692,346,-346,692,-692,346,-692,346,-692, 346,-692,346,-692,346,-346,692,-692,346]";
                } else {
                  return "[346,-692,346,-692,346,-692,346,-692,346,-346,692,-692,346,-692,346,-692, 346,-692,346,-692,346,-346,692,-692,346]";
                }
          repeat:
            times: 5
            wait_time: 12.12ms
      - lambda: get_cc1101(transciver).endTransmission();

In this case, it complains that lambda as being an invalid option for [0].
I’ve also tried to remove code: and return it in my string after my test “code: […]” but then it’s not happy that “repeat” follows that since it expects the return to have the entire sub-block from what I can tell.

So how can I simply return a different code based on the value of state. I ultimately need 4 states, but I first want to make 2 states work.

Have a go at making your lambda more like this.

code: !lambda return 

I recall this kind of thing being possible but you really had to dig around the forum to discover the way to do it.

Edit: probably like this.

Edit 2:

I don’t think your retrieving state the right way either.

Breaking it up a bit using buttons is probably another decent approach rather than trying to do it all in one lambda.

Why return with quotation marks?
What is that state?

Thanks you nailed it. This post is exactly what I was looking for. I tried to dig for something like this, but none of my searched were successful. You’re exactly right; I had the remove the quotes and simply return an array of codes and the way I was executing lambda wasn’t correct. “!lambda” was the correct syntax here. It works now. Much appreciated!

1 Like

You’re correct no quotes required here. I was under the impression that this would append the string to the yaml which would then be processed but clearly that’s not how it works. It executes the CPP code and returns the objects.

Good to post your final code to make it a bit easier for the next person;)

Hhmm… I cant imagine ever buying a smart toilet seat but, reading through that custom component code makes me feel like I dont know crap and secondly its very interesting and I feel like bits and pieces of it or certain functions could be very helpful for lots of other projects. Good share! Im bookmarking that one.

1 Like

Certainly. This fix was pretty easy and pretty much what the reply mentioned. Lambda requires a ‘!’ and the return should be an array instead of a string.

write_action:
      - lambda: get_cc1101(transciver).beginTransmission();
      - remote_transmitter.transmit_raw:
          code: !lambda: |-
              if (state < 0.25) {
                return {346,-692,346,-692,346,-692,346,-692,346,-346,692,-692,346,-692,346,-692, 346,-692,346,-692,346,-346,692,-692,346};
              } else {
                return {346,-692,346,-692,346,-692,346,-692,346,-346,692,-692,346,-692,346,-692, 346,-692,346,-692,346,-346,692,-692,346};
              }
          repeat:
            times: 5
            wait_time: 12.12ms
      - lambda: get_cc1101(transciver).endTransmission();
1 Like

I was confused how you were using state but now I see it is legit for write_action:. Makes sense now;)