Decoding 433Mhz remote

Hi,
I have a 433Mhz receiver and paired it with a ESP8266 with ESPhome on it, that seems to work hardware wise.

I have several 4 button remotes, and my goal is to let ESPhome decode their signals, and send that to home assistant as an event.

My ESPhome code:

esphome:
  name: rf433-receiver
  friendly_name: RF433_Receiver

esp8266:
  board: esp01_1m

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: " xxx "

ota:
  password: " xxx "

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Rf433-Receiver Fallback Hotspot"
    password: " xxx "

captive_portal:

remote_receiver:
  pin: 4
  dump: rc_switch
  filter: 250us
  tolerance: 50%
  idle: 4ms
  buffer_size: 2kb

Which gives me the following “dump” output when i press buttons.
This is in the order: Remote 1 (buttons A, B, C, D), Remote 2 (buttons A, B, C, D)

[01:27:31][I][remote.rc_switch:261]: Received RCSwitch Raw: protocol=1 data='011010001110010001110100'
[01:27:35][I][remote.rc_switch:261]: Received RCSwitch Raw: protocol=1 data='011010001110010001111000'
[01:27:38][I][remote.rc_switch:261]: Received RCSwitch Raw: protocol=1 data='011010001110010001110001'
[01:27:40][I][remote.rc_switch:261]: Received RCSwitch Raw: protocol=1 data='011010001110010001110010'

[01:27:45][I][remote.rc_switch:261]: Received RCSwitch Raw: protocol=1 data='110111101100111000000100'
[01:27:45][I][remote.rc_switch:261]: Received RCSwitch Raw: protocol=1 data='110111101100111000001000'
[01:27:47][I][remote.rc_switch:261]: Received RCSwitch Raw: protocol=1 data='110111101100111000000001'
[01:27:47][I][remote.rc_switch:261]: Received RCSwitch Raw: protocol=1 data='110111101100111000000010'

As far as i can see, the last 4 bits are to identify the button, the rest seems to be the “serial number” of the remote.
How can i read in this raw code, and decode this to 2 separate variables that i can use to send to homeassistant as an event? (so 1 variable for the remote serial number, and 1 for the button that is pressed?)

I have read through several topics about the rc_switch protocol, but most of them are focussed on either transmitting, or about matching a single raw code to use in a binary sensor, and that is not what i’m trying to achieve.

If i missed a topic or a post in which this is already solved, please let me know what keywords you used to find it, that might at the very least improve my research skills.

Welcome @thorstenhag.

Did you see on_rc_switch?

Yes, i read through it, but got a little stuck.
As far as i can gather, that gets me to a variable called ‘x’ from which i can access ‘x.code’.
But that seems to give me an unsigned 64 bit integer.
And that is where i get stuck, my (at the moment small) knowledge of data types and structures is not sufficient to figure out how to split this into the 2 variables i want to get out of it.

Additionally i would like to convert the 2 split variables to something a bit more ‘human readable’ like hexadecimal, but that will be the next step to figure out at a later stage.

I don’t really have a clue either, but this might get you one step closer. I don’t know if it is a good approach. Seems a bit complicated.

It was mostly written by AI but is tested as working.

  on_rc_switch:
    - lambda: !lambda |-
          uint64_t code = x.code;
          char binary_str[25];    // +1 for null terminator
          binary_str[24] = '\0';  // Null terminator
          for (int i = 23; i >= 0; --i) {
              binary_str[23 - i] = (code & (1ULL << i)) ? '1' : '0';
          }
          ESP_LOGD("Custom", "Yay I parsed: %s", binary_str);
          ESP_LOGD("Custom", "Now I could for example publish this to a text sensor and work with it further");
          ESP_LOGD("Custom", "Like maybe split it on character counts and throw in some if actions");

[19:50:47][D][binary_sensor:036]: 'DeskRemote_Lights_Toggle': Sending state ON
[19:50:47][D][button:010]: 'rf Single High Beep' Pressed.
[19:50:47][D][rtttl:051]: Playing song beep
[19:50:47][D][Custom:157]: Yay I parsed: 010011001011010101101001
[19:50:47][D][Custom:158]: Now I could for example publish this to a text sensor and work with it further
[19:50:47][D][Custom:159]: Like maybe split it on character counts and throw in some if actions
[19:50:47][I][remote.rc_switch:261]: Received RCSwitch Raw: protocol=6 data='010011001011010101101001'