Writing a state to be some text + value of a sensor

This is probably so simple, but I spent over an hour last night searching and reading and couldn’t find anything. It’s gotta be really simple but I just can’t figure out how to do it. Basically, I have a Home assistant text_input that can change whenever I want, and when someone enters a code on my garage keypad, ESPHome ensures that it matches my set keypad (these people are a variety of dog sitter/walkers/maid services/etc). But, I have a sensor set up (well, it’s not my code), but it reports the last user. Normally they are manually set: when I enter 6543 it says “Last User was Mark”.

But when I have a varying variable, I can’t get it to go. Here’s my code:

  - platform: template
    id: dog1match
    optimistic: true
    name: "Dog Walker Code 1 Match"
    lambda: |-
      if (id(keyCode).state == id(dogwalkercode1).state) {
        return true; 
      }
        else {
          return false;
      }  
    on_turn_on:
      - if:
          condition:
            - binary_sensor.is_on: dogwalkersentry
          then:
            - switch.turn_on: OvrHead2
            - text_sensor.template.publish:
                id: last_user
                state: "Custom Dog Walker"

But I don’t want it to just say “Custom Dog Walker”. I want it to say “Custom Dog Walker Code - 4157” (or whatever the code is. It’s the (dogwalkercode1).

So I want it to say

- text_sensor.template.publish:
    id: last_user
    state: "Customer Dog Walker - " + id(dogwalkercode1).state

But I feel like I’ve tried every damn thing and nothing works. Help please!!

Assuming that dogwalkercode1 is a string like for instance 4157, I think something like this should work:

- text_sensor.template.publish:
    id: last_user
    state: !lambda 'return "Custom Dog Walker Code - " + id(dogwalkercode1).state;'
1 Like

Unfortunately it gives me this error. I’m a very novice at this programming, and I’m completely confused by floats vs integers vs strings, and I think that this is the problem. I just don’t know how to fix.

Compiling .pioenvs/garage-keypad/lib64d/WiFi/WiFiAP.cpp.o
/config/esphome/garage-keypad.yaml: In lambda function:
/config/esphome/garage-keypad.yaml:106:42: error: invalid operands of types 'const char [26]' and 'float' to binary 'operator+'
                 state: !lambda 'return "Custom Dog Walker Code - " + id(dogwalkercode1).state;'
                 ~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
Compiling .pioenvs/garage-keypad/lib64d/WiFi/WiFiClient.cpp.o
*** [.pioenvs/garage-keypad/src/main.cpp.o] Error 1

Floats are numeric values with a floating point like 123.456 or 789.0
Integers are numeric values without a floating point like 1234 or 5
Strings are any ranges of characters without a numeric value, like abcd or ab23 but also 4321 can be a string.

Looking at your error message the dogwalkercode1 appears not to be a string but a float, so this has to be converted to a string before the two strings can be added together.
Try this:

- text_sensor.template.publish:
    id: last_user
    state: !lambda 'return "Custom Dog Walker Code - " + to_string(id(dogwalkercode1).state);'
1 Like

ok it worked, thank you! Now for a little more formatting! It returns the wiegand input codes like “4157.000” How can I get it to write only the first 4 numbers and skip the decimal and everything after?

In that case you first have to convert the floating value to an integer, which can be done like this:

int(id(dogwalkercode1).state)

And next step is to again convert this integer to a string so that you can add the two strings together, like this:

to_string(int(id(dogwalkercode1).state))

So the complete code is now:

- text_sensor.template.publish:
    id: last_user
    state: !lambda 'return "Custom Dog Walker Code - " + to_string(int(id(dogwalkercode1).state));'

Thank you thank you thank you!