ESPHome Wiegand to Home Assistant

Anyone here tried this kind on wiegand reader? im triying to connect it to esphome but it does not respond to LOGS when im pressing the keys… my config is D0 to GPIO4 and D1 to GPIO5 also GND to GND on both nodemcu and wiegand reader… hope someone here can help me…


I don’t see any wiegand branding on there. Are you sure?

I don’t see your yaml or your logs.

As you can see on the first picture there is a D0 and D1 pinout the pins use for wiegand… ill post my logs later when i get home.

I have reminded myself about wiegand and the fact that it is a protocol rather than a brand.

Don’t forget this note, dunno how you tell what mode it is in.

Some keypads are preconfigured by the factory to act as Wiegand input devices. In order to work with this component, they may need to be reconfigured to act as Wiegand 26 output or Wiegand 34 output devices.

Also my reading is that there are many variations on the protocol :frowning:

What do the instructions say about Wiegand26 output? Mine had to be manually put in that mode, it wasnt the default. The instructions should tell you…

Which esp board are you using?

Hopefully this is a good thread to post in.

I needed the (decoded) facility code and card code not the Hex tag or decimal value.

Using ChatGPT, I created the following code.

It works really well for my purposes

Strangely, I can’t figure out or get ChatGPT to simplify the code by using bitwise operations.

Hopefully this helps.

time:
  - platform: sntp
    id: sntp_time
    timezone: CST6CDT
    servers:
     - 192.168.1.1

web_server:
  port: 80
  version: 3
  local: true

sensor:
  - platform: uptime
    name: Uptime Sensor

text_sensor:
  - platform: template
    name: "Current time"
    lambda: |-
      char str[17];
      time_t currTime = id(sntp_time).now().timestamp;
      strftime(str, sizeof(str), "%Y-%m-%d %H:%M", localtime(&currTime));
      return  { str };
    update_interval: 60s
    web_server_sorting_weight: 5

  - platform: template
    name: "Wiegand Raw Bits"
    id: wiegand_bits
    web_server_sorting_weight: 10

  - platform: template
    name: "Wiegand Raw Value (Hex)"
    id: wiegand_value
    web_server_sorting_weight: 15

  - platform: template
    name: "Facility Code"
    id: wiegand_facility_code
    web_server_sorting_weight: 20

  - platform: template
    name: "Card Code"
    id: wiegand_card_code
    web_server_sorting_weight: 25

wiegand:
  - id: mykeypad
    d0: GPIO32
    d1: GPIO23
    #on_key:
    #   - lambda: ESP_LOGI("KEY", "received key %d", x);
    #on_tag:
    #  - lambda: ESP_LOGI("TAG", "received tag %s", x.c_str());
      #- lambda: id(wiegand_text).publish_state(x.c_str());
    # on_raw:
    #   - lambda: |-
    #     std::string tag_id = x;
    #     id(wiegand_text).publish_state(tag_id.c_str());
    on_raw:
    - lambda: ESP_LOGI("RAW", "received raw %d bits, value %llx", bits, value);
    - lambda: |-
        uint8_t tag_bits = bits;  // Use 'bits' from the Wiegand event
        id(wiegand_bits).publish_state(String(tag_bits).c_str()); // Publish decimal string to Wiegand Raw Bits Text Template
    - lambda: |-
        uint64_t tag_value = value;  // Use 'value' from the Wiegand event
        char hex_string[17];  // 16 characters for hex + null terminator
        sprintf(hex_string, "%llX", tag_value);  // Convert to hex string
        id(wiegand_value).publish_state(hex_string);  // Publish hex string to Wiegand Raw Value Text Template
    - lambda: |-
        uint8_t tag_bits = bits;  // Use 'bits' from the Wiegand event
        uint64_t tag_id = value;  // Use 'value' from the Wiegand event
        bool databits[26];  // Array to hold individual bits
        if (tag_bits==26){
          // Fill the databits array from the tag_id
          for (int i = 0; i < 26; i++) {
            databits[i] = (tag_id >> (25 - i)) & 0x01;  // Extract each bit
          }

          uint8_t facility_code = 0;
          uint32_t card_code = 0;

          // Extract facility code (bits 2 to 9)
          for (int i = 1; i < 9; i++) {  // Loop from bit 1 to bit 8
              facility_code <<= 1;
              facility_code |= databits[i];
          }

          // Extract card code (bits 10 to 23)
          for (int i = 9; i < 25; i++) {  // Loop from bit 9 to bit 22
              card_code <<= 1;
              card_code |= databits[i];
          }

          // Publish the values
          id(wiegand_facility_code).publish_state(String(facility_code).c_str());
          id(wiegand_card_code).publish_state(String(card_code).c_str());
        }