Key_collector to text_sensor?

Hi everybody,

I am trying to create a text_sensor (it’d be fine if it were input_number as well) that will be updated when my key_collector runs the on_result action.

So let’s say somebody enters 1234 and presses # (within the timeout time frame); then I’d like the output to be written to a sensor that I can work with in Home Assistant.

Currently, the code below will literally cause the sensor to be set to {{ states(pincode_reader)}}. I tried all sort of variants of this (for example, {{ states("pincode_reader")}}, {{ %s }}) or different !lambdas, the latter of which always fail.

Thank you in advance for your help :slight_smile:

ESPHome yaml

matrix_keypad:
  id: testkeypad
  rows:
    - pin: GPIO32
    - pin: GPIO33
    - pin: GPIO25
    - pin: GPIO26
  columns:
    - pin: GPIO27
    - pin: GPIO14
    - pin: GPIO12
  keys: "1853A6874#0A"
  has_diodes: false

key_collector:
  - id: pincode_reader
    source_id: testkeypad
    min_length: 4
    max_length: 4
    end_keys: "#"
    end_key_required: true
    clear_keys: "*"
    allowed_keys: "0123456789"
    timeout: 6s
    on_progress:
      - logger.log:
          format: "input progress: '%s', started by '%c'"
          args: [ 'x.c_str()', "(start == 0 ? '~' : start)" ]
    on_result:
      - logger.log:
          format: "input result: '%s', started by '%c', ended by '%c'"
          args: [ 'x.c_str()', "(start == 0 ? '~' : start)", "(end == 0 ? '~' : end)" ]
      - text_sensor.template.publish:
          id: keypad
          state: "{{ states(pincode_reader)}}"
    on_timeout:
      - logger.log:
          format: "input timeout: '%s', started by '%c'"
          args: [ 'x.c_str()', "(start == 0 ? '~' : start)" ]

text_sensor:
  - platform: template
    name: "Keypad Code"
    id: keypad

      - text_sensor.template.publish:
          id: keypad
          state: !lambda "return id(pincode_reader).state;"

ESPHome doesn’t use Jinja templates, which is what your {{ states...}} is.

1 Like

Thank you. I had tried that as well, but that will not allow me to upload the new firmware:

Compiling /data/zzesp32test/.pioenvs/zzesp32test/src/main.cpp.o
/config/esphome/zz_testespvan.yaml: In lambda function:
/config/esphome/zz_testespvan.yaml:151:14: error: could not convert 'pincode_reader' from 'esphome::key_collector::KeyCollector*' to 'std::__cxx11::string' {aka 'std::__cxx11::basic_string<char>'}
           state: !lambda "return id(pincode_reader);"
              ^~~~~~~~~~~~~~
*** [/data/zzesp32test/.pioenvs/zzesp32test/src/main.cpp.o] Error 1

Yeah, I missed the .state off the end of the id, now fixed above. Try that.

1 Like

That doesn’t compile either:

/config/esphome/zz_testespvan.yaml: In lambda function:
/config/esphome/zz_testespvan.yaml:151:30: error: 'class esphome::key_collector::KeyCollector' has no member named 'state'
           state: !lambda "return id(pincode_reader).state;"
                              ^~~~~

(* reads up on the key collector component)

If the log statements are working, this should too?

    - text_sensor.template.publish:
        id: keypad
        state: !lambda "return x.c_str();"
1 Like

Brilliant, thank you.

While I find the Home Assistant documentation incredibly helpful, I can never wrap my head around the ESPHome docs.

I mean, I was aware that there was x.c_str(), but didn’t realize this was usable anywhere else. I’ll keep this in mind when there is another thing I don’t understand about ESPHome :slight_smile:

I’m trying to get this setup working with a NodeMCU has anyone done this with one of these boards?

You can use a nodemcu as well as generic ESP8266, wemos d1, or esp32.

I am currently not at home, so unfortunately I cannot look at my hardware setup, but I believe I was using an ESP32 in my approach.

You can simply adapt the yaml configuration from above by changing the used pins to those available on nodemcu.

Sorry, I am on mobile, so I don’t have all the links available to me atm… But you can search for which pins should work on nodemcu, it might even be on https://esphome.io on the nodemcu page.

You could also safe yourself a lot of annoyance (at least I thought so) by using Wiegand compatible hardware instead :slight_smile:

My Keypad never worked correctly (might have been me messing up somewhere, might have been a hardware issue, as it came from my used parts drawer), so I bought one of those for under 20 bucks (if I remember correctly) and everything worked perfectly.

1 Like