Length of a text sensor

I’m building an alarm system, and would like to show the progress of the keypad on a display.

In order to do that, I plan to build a custom sensor with a lambda against the length of the relevant text sensor. I’ve been unable to access the length of my text_sensor with lambdas - does anyone have an example of how to access the length of a text sensor that they could share?

I think the state of a text sensor is a std::string. An std::string has a length() method that will return the length as int. So somting like this should work:

id(text_sensor_id).state.length()

2 Likes

Thanks mate, that did the trick. Here’s the final outcome, compiled and working:

sensor:
  - platform: template
    name: "Keypad Length"
    lambda: |-
      return id(reader).state.length();
    update_interval: 100ms