In this post I will show how to configure rpi_rf platform in HomeAssistant to control electric outlets. For this we need:

  • Home Assistant installed on Raspberry Pi
  • Arduino compatible board
  • FS100A 433 MHz Radio Receiver and Emitter
  • Stuff to control, in my case radio controled electric outlets with remote

Configuration part in Home Assistant

The rpi_rf switch platform allows you to control devices over 433 MHz signals with generic low-cost GPIO RF modules. Configuration in configuration.yaml looks like this:

switch: - platform: rpi_rf gpio: 3 switches: bedroom_humidifier: code_on: 1361 code_off: 1364 pulselength: 321 protocol: 1 signal_repetitions: 80 living_room_humidifier: code_on: 4433 code_off: 4436 pulselength: 321 protocol: 1 signal_repetitions: 80

As you can see we can have multiple switches. Each switch have separate code on and code off. This code is emitted by RF remote that controls outlets.

false

Recording codes sended by pilot

I have assembled this simple remote code recorder using Arduino compatible board. First I connected receiver to board in this way:

Receiver pins

  • GND - pin 1 (GND, blue wire)
  • D2 - pin 2 (DATA, yellow wire)
  • +5 V - pin 4(VCC, red wire)

With board it should look like this:

false

Install arduino RC Switch library in arduino IDE and run this code:

static const char* bin2tristate(const char* bin); static char * dec2binWzerofill(unsigned long Dec, unsigned int bitLength); void output(unsigned long decimal, unsigned int length, unsigned int delay, unsigned int* raw, unsigned int protocol) { if (decimal == 0) { Serial.print("Unknown encoding."); } else { const char* b = dec2binWzerofill(decimal, length); Serial.print("Decimal: "); Serial.print(decimal); Serial.print(" ("); Serial.print( length ); Serial.print("Bit) Binary: "); Serial.print( b ); Serial.print(" Tri-State: "); Serial.print( bin2tristate( b) ); Serial.print(" PulseLength: "); Serial.print(delay); Serial.print(" microseconds"); Serial.print(" Protocol: "); Serial.println(protocol); } Serial.print("Raw data: "); for (unsigned int i=0; i<= length*2; i++) { Serial.print(raw[i]); Serial.print(","); } Serial.println(); Serial.println(); } static const char* bin2tristate(const char* bin) { static char returnValue[50]; int pos = 0; int pos2 = 0; while (bin[pos]!='\0' && bin[pos+1]!='\0') { if (bin[pos]=='0' && bin[pos+1]=='0') { returnValue[pos2] = '0'; } else if (bin[pos]=='1' && bin[pos+1]=='1') { returnValue[pos2] = '1'; } else if (bin[pos]=='0' && bin[pos+1]=='1') { returnValue[pos2] = 'F'; } else { return "not applicable"; } pos = pos+2; pos2++; } returnValue[pos2] = '\0'; return returnValue; } static char * dec2binWzerofill(unsigned long Dec, unsigned int bitLength) { static char bin[64]; unsigned int i=0; while (Dec > 0) { bin[32+i++] = ((Dec & 1) > 0) ? '1' : '0'; Dec = Dec >> 1; } for (unsigned int j = 0; j< bitLength; j++) { if (j >= bitLength - i) { bin[j] = bin[ 31 + i - (j - (bitLength - i)) ]; } else { bin[j] = '0'; } } bin[bitLength] = '\0'; return bin; } #include <RCSwitch.h> RCSwitch mySwitch = RCSwitch(); void setup() { Serial.begin(9600); mySwitch.enableReceive(0); } void loop() { if (mySwitch.available()) { output(mySwitch.getReceivedValue(), mySwitch.getReceivedBitlength(), mySwitch.getReceivedDelay(), mySwitch.getReceivedRawdata(),mySwitch.getReceivedProtocol()); mySwitch.resetAvailable(); } }

After you run code on arduino, each time you press key on rc remote you should receive information like this: Decimal: 1361 (24Bit) Binary: 000000000000010101010001 Tri-State: 000000FFFF0F PulseLength: 321 microseconds Protocol: 1 Raw data: 9984,232,1040,236,1048,236,1044,232,1052,224,1064,216,1044,236,1056,220,1056,224,1052,228,1048,228,1044,236,1044,236,1056,864,404,212,40,72,48,64,1244,1136,16,36,16,52,76,192,32,616,1352,32,32,40,340,

false

What is interesting for us is:

  • decimal code - in this case it is 1361
  • pulse code - 321 microseconds
  • protocol - 1

With this information we can configure our first switch:

switch: - platform: rpi_rf gpio: 3 switches: bedroom_humidifier: code_on: 1361 code_off: 1364 pulselength: 321 protocol: 1

Wiring Raspberry PI

The last step is to wire up our RF with emitter raspberry pi:

false

After restarting home assistant, there should appear new switch in states tab that will enable us to switch on and off this outlet assigned to it.

https://www.youtube.com/watch?v=XthTKgmQkjY

Comments