Lambda Help

I’ve reached the limit of my knowledge on this one and can’t seem to find the right google phrase to help me through.

I am trying to have an ESP check a url based on an RFID ID and the MAC Address.

If it gets a response, it turns on a relay. Otherwise nothing.

#Setup RFID Reader, tag read acknowledgement, HTTP Auth Request
pn532_i2c:
  update_interval: 1s
  on_tag:
    then:
      - light.turn_on:
          id: swled
          flash_length: 0.5s
      - http_request.get:
          url: !lambda |-
            "http://192.168.0.5/" 'return {WiFi.macAddress().c_str()};' "/" 'return x;' "/"
          headers:
            Content-Type: application/json
          verify_ssl: false
          on_response:
            then:
              - switch.turn_on: relay_1
#!lambda 'return {WiFi.macAddress().c_str()};' #Send MAC address for machine ID
#!lambda 'return x;' #This is the RFID UID

Ideally, I would like to flash the light if I got anything other than a 200 response but beyond me at the moment. Any help from the more software minded folk much appreciated.

For starters, note that a lambda is supposed to be actual C++ code. So the line that begins with a quoted string (the URL) is not valid C++ code.
You can provide the URL as a normal YAML element if it is a static string, or use a lambda and C[++] code to compose the URL string before returning it.

So, for example, I use HTTP from the ESPHome device to control another device.
It enslaves a second dimmer to the one running this code. It looks up the current brightness, and reads a global variable containing the target hostname (there are more than one) then composes the URL that will command them.
Your url: lambda should return only the URL string - it does not do processing of the returned value under the url: tag.
Here, I compose the URL string using some C string operation calls:

          url: !lambda |-
            char urlstring[254];
            if( id(light_dimmer).current_values.is_on() ) {
              sprintf( (urlstring), "http://%s/light/0?turn=on&brightness=%u", id(slave_host).c_str(),
                  max( 1, int( 100.0 * id(light_dimmer).current_values.get_brightness() ) )
                  );
            } else {
              sprintf( (urlstring), "http://%s/light/0?turn=off", id(slave_host).c_str() );
            }
            return (urlstring);

Separately, under the on_response statement, you may want to do any checking of that response to be sure what you got is the desired one.

Google for “lambda std::str” for help.

Being C code, I think you need to:

  1. declare the temporary string you are going to work with;
  2. assign a value to that string (you will need to fix up your string calculation code here);
  3. return that string.

Something like this…

#Setup RFID Reader, tag read acknowledgement, HTTP Auth Request
pn532_i2c:
  update_interval: 1s
  on_tag:
    then:
      - light.turn_on:
          id: swled
          flash_length: 0.5s
      - http_request.get:
          url: !lambda |-
            std::string str = "";
            str = "http://192.168.0.5/" 'return {WiFi.macAddress().c_str()};' "/" 'return x;' "/"
            return str;
          headers:
            Content-Type: application/json
          verify_ssl: false
          on_response:
            then:
              - switch.turn_on: relay_1
#!lambda 'return {WiFi.macAddress().c_str()};' #Send MAC address for machine ID
#!lambda 'return x;' #This is the RFID UID