Home Automation with self-learning 433 power switches (revised)


About This Project

This is my 2nd project with Cayenne. First published in December 2016. I revised it a bit and also corrected a stupid error in the DST-calculation which only became evident when DST started in 2017. The project itself is based on a static sketch running for several months now where a handful of lights follow sunset to go on; major objective here is that there is no need to adjust time over the months (the farther you are away from the equator, the more you will require this). In December 2016 I converted this to Cayenne because I was missing manual controls (or have to use three different remotes), the sketch does not need to be updated when switches are added because they can be programmed from inside the dashboard and on/off actions can be scheduled and/or triggered!

The switches in this project are self-learning. How to recognise a self-learning switch? Self-learning switches do not have any dipswitches but when plugged in power you get some time to program them with either the supplied remote or now with Cayenne. In the Netherlands the switches are sold with names like Flamingo F500, some “klik aan klik” uit variants, Elro 8-serie and the cheapest from Action 3 for 10 euro. The Action switches are also easy because they give you more time for programming the switch. To be honest I am not sure if all self-learning switches are using the same protocol but it looks like it so far. The library is created by others who invested their time to understand what kind of 433 sequences the remote controls were sending to activate a switch. These codes are converted again by someone into the flamingo library. My contribution is limited with some simplification to make this library more Cayenne friendly; It used to have a remote-unit key, I converted it to (virtual) single code key so it can respond on the Cayenne’s’ virtual channel V0 to V7. More codes might be available.

What’s Connected

The project exits off:
• Arduino UNO
• W5100 LAN module
• 433-module
• RTC-module (DS3231)
• Self-learning power switches (*)

Added functionality:

  • Time library.
    Why use the time library? In the old project the device was instable this is solved with the timer function which only communicates with the Cayenne server in a 10 seconds interval. In this section the present time (V20) is calculated, DST, sunrise (V21), sunset V22) and “night time” (V25).
  • Daylight Saving Time (DST) for CET.
  • Sunset/Sunrise calculation and display; The Sunset/Sunrise is calculated with “TimeLord tardis” in combination with the GPS location. It also uses the variable “DST_check” to add hours for the DST-offset in summer.
  • The RTC-module (DS3231) is added for two reasons.
    • Unaware if the time can be retrieved from Cheyenne
    • Use the alarm function for DST (second only)
    Some lines are set to comment, you can use this section to reset your RTC-module and/or to set a new time. Please note that the present time will be about 20 seconds of (differences caused due time lapse between the compile time and actual reset of RTC-time in sketch, you might want to program the RTC-time in a simple sketch.
  • Display of the values
    How to display the sunset and sunrise time as best as possible? In the absence of the colon make a point (Amerika likely sees a comma).
  • attachment
    The attachment consist of the library folder with three files; the sketch, the routines and the flamingo library (in a zip file renamed as txt).
    sketch_433_Cayenne_timed.zip.txt (5.1 KB)

Triggers & Alerts

The standard alert when device goes offline
The trigger based upon “night time” to schedule light to go on/off on sunset/sunrise


When a switch needs to turn on or mostly off on a defined time e.g. midnight.

Dashboard Screenshots


The self-learning switches:


Trigger using Sunset:


video of project
f.y.i. Using my android phone only for display here. With my desktop I select the widget, this will result that the status of the widget and the light on the switch will change.

The Judges Have Spoken! ~CONTEST RESULTS~
MQTT - 433-module not reacting

##Small Improvements
Added a slider to the device in order to have more flexibility in the twilight zone. Ok, think a bit more explanation might be justified. In the Netherlands the sunset is changing daily with about 3 minutes (or so). But when the sun sets it is not dark yet so I wanted to introduce an offset to this sunset. The perfect way to do this is to add a slider and adjust the sunset with for example 30 minutes and lights can be switched automatic 30 minutes after sunset. The actual automation can be done with a trigger (V24).

if (time_now >= time_sunset + time_offset || time_now <= time_sunrise - time_offset) { //delay for twilight offset set by slider
   Cayenne.virtualWrite(V24, 1);
   } else {
   Cayenne.virtualWrite(V24, 0);

Another very, very small improvement was achieved when i realised something better with “invert logic”. Benefit is mostly a more condensed sketch (Because this part is repeated 8 times).

CAYENNE_IN(V0) // action switch lamp V0
if (getValue.asInt() == 1) {
Flamingo_Send(V0, 0); // ON
} else {
Flamingo_Send(V0, 1); // OFF

Is replaced by:

CAYENNE_IN(V0) // action switch lamp V0
Flamingo_Send(V0, getValue.asInt()); // ON or OFF


Small Improvements (part 2) transfer to Wemos D1 R2

Though, already in the sketch I tried to upload this to a Wemos D1 R2 but was surrounded by warnings and errors. This also happened with the the UNO+W5100 combination. Most of them could be tackled by refreshing the libraries in and I realised that people including myself should utilise comments in the sketch more frequently so my tip is: So put library references (location, used version) in your sketch!!!

The transfer to Wemos D1 R2 showed some other problems:

  1. How to address your pins e.g.int rfPin = D7;
  2. Reduce delay of 433-sensor from 2.0 to 0.2 seconds to avoid losing contact with server

I notice that this project is now also puplished in hackster.io but I changed my nickname to a better name “WJ4IoT” so have no access to this project (yet).

Hereby the latest sketch (please rename txt->zip):
sketch_433_Cayenne_timed.txt (6.0 KB)
Please note that when using the sketch for the UNO+W5100 it has consumed the majarity of available memory. For that reason it is advised to to put some parts on comment like:

  Serial.println("info: sketch_433_Cayenne_timed");
  Serial.print("compiled: ");
  Serial.print(" ");
  Serial.println("Following sensors are/should be connected");
  Serial.println("-> Analog 4 = SCL RTC DC3231");
  Serial.println("-> Analog 5 = SDA RTC DC3231");
  Serial.println("-> Digital 7 = Data 433");

Bit of a pity because i really like to see which sketch I put on my device :grinning:

Picture of version 3 with the Wemos D1 R2


Hi @wj4me, thanks for sharing your updates!

As far as the Android app and sliders, you should be able to set a custom range for them on the Android app to match what you see on the web (so you can have some values other than 0 — 1 ). To do this, go to the slider’s settings on the Android app, ensure the “Enable Custom Range” toggle is set to ON, and set your slider min/max values.

I am aware there is a little bugginess here, the settings don’t always sync the best between Android and web and sometimes require configuration on each side, and when writing this, I sometimes had to make this setting twice and make sure all other fields were filled out on the Android side to get it to stick. I’ll get these logged for fix in a future release. But it does seem possible, at least.


Hi @rsiegel, yes I see that my sliders remarks are invalid and were caused because my slider is used for calculation/offset of time and just representing a value; I realised I never changed data (not sure how I did the save, lilely old version). I like just data because none of the other options are valid in my case.


The project is still tweaked from time to time; the last tweak was to set the delay time (time2wait) to zero because this resulting too often in a disconnect from the server. F.y.i. it was introduced to avoid overlapping 433-codes which was required (?) in a stand alone environment (=UNO without wifi). Best to use the latest sketch which can be found in the Hackster.IO environment (though cannot edit time2wait=0 yet):

It is good we can share it nowadays to see how a project looks in action:



Thanks for updating your project!