Analogue style rotary dimmer switch - Sonoff + ESPEasy + MQTT

This is cool.

One extra I thought of, add a couple of touch buttons (if there is room) which activate a change in mqtt topic. Press button 1 to control the lights. Press button 2 to control media, press button 3 to control a fan. Or whatever your situation demands!

There are not enough easily accessible GPIOs on a sonoff basic to do this.
You would have to solder to the esp chip itself and that’s way beyond my skills with an iron :slight_smile:

Yes I thought of that after I posted. Maybe a wemos or similar would do the trick.

Anyway nice hack, I love it.

1 Like

Yes the relay button still works because it’s configured to. It’s not hard wired.

If you use a diff esp platform you def could do it.
But sonoff basic has the built in psu, the relay, enclosure etc for $5

Yeah it is really good value.

The power supply used to power the wemos in HAswitchplate https://github.com/aderusha/HASwitchPlate is a pretty good option IMHO. But yes, the whole thing does end up being a quite a bit more.

For your average dimmer situation a sonoff is good, if you wanted a couple of more versatile rotaries in a couple of places, there are options. That’s the good thing about making, the possibilities are endless.

1 Like

That project is nice, and I agree about the PCB the OP designed.
Its probably a better high voltage design, but def costs more (money and work)

Hello Please Help. i have this dimmer and the code for it is (attached)
i can control the dimmer from the web http page provided by seller
i want to control it over mqtt i can turn it off and on sending mqtt commands from home assistant config page pay load commands R13_ON & R13_OFF

I am a beginner trying to learn everything the guy keeps telling me the dimmer command is Dimmer as i can see in the code but it does not do any thing my home assistant config for this is

my home assistant configs are , i am able to turn off & on but not brightness from the http panel of the product i can control brightness

light:

  • platform: mqtt
    name: “test light”
    command_topic: “bruno”
    state_topic: “bruno”
    payload_on: “R13_ON”
    payload_off: “R13_OFF”
    brightness_command_topic: “bruno/Dimmer”
    brightness_state_topic: “bruno/Dimmer”

please help Thanks in advance

boolean connectMQTT() {
if (mqttClient.connected()) {
return true;
}

Serial.print("Connecting to MQTT server “);
Serial.print(mqttServer);
Serial.print(” as ");
Serial.println(host);

if (mqttClient.connect(host)) {
Serial.println(“Connected to MQTT broker”);
if (mqttClient.subscribe((char*)subTopic.c_str())) {
Serial.println(“Subsribed to topic.”);
} else {
Serial.println(“NOT subsribed to topic!”);
}
return true;
}
else {
Serial.println("MQTT connect failed! ");
return false;
}
}

void disconnectMQTT() {
mqttClient.disconnect();
}

void mqtt_handler() {
if (toPub == 1) {
Debugln(“DEBUG: Publishing state via MQTT”);
if (pubState()) {
toPub = 0;
}
}
mqttClient.loop();
delay(100); //let things happen in background
}

void mqtt_arrived(char* subTopic, byte* payload, unsigned int length) { // handle messages arrived
int i = 0;
// Serial.print("MQTT message arrived: topic: " + String(subTopic));
// create character buffer with ending null terminator (string)

for (i = 0; i < length; i++) {
buf[i] = payload[i];
}
buf[i] = ‘\0’;
String msgString = String(buf);
Serial.println(" message: " + msgString);

if (msgString.substring(0,6) == “R13_ON”) **
** {

** Serial.println(“Dimmer:99”);**
** } **
** else if (msgString.substring(0,7) == “R13_OFF”) **
** {**
** Serial.println(“Dimmer:0”);**
}
else if (msgString.substring(0,6) == “R14_ON”)
{
Serial.println(“R_1 switched via web request to 1”);
}
else if (msgString.substring(0,7) == “R14_OFF”)
{
Serial.println(“R_1 switched via web request to 0”);
}
else if (msgString.substring(0,7) == “Dimmer:”)
{
Serial.print(“Dimmer:”);
Serial.println(msgString.substring(7,9));
}
}

boolean pubState() { //Publish the current state of the light
if (!connectMQTT()) {
delay(100);
if (!connectMQTT) {
Serial.println(“Could not connect MQTT.”);
Serial.println(“Publish state NOK”);
return false;
}
}
if (mqttClient.connected())
{
Serial.println("To publish state " + state );
if (mqttClient.publish((char*)pubTopic.c_str(), (char*) state.c_str())) {
Serial.println(“Publish state OK”);
return true;
}
else
{
Serial.println(“Publish state NOK”);
return false;
}
}
else
{
Serial.println(“Publish state NOK”);
Serial.println(“No MQTT connection.”);
}
}

String getValue(String data, char separator, int index)
{
int found = 0;
int strIndex[] = {
0, -1
};
int maxIndex = data.length() - 1;
for (int i = 0; i <= maxIndex && found <= index; i++) {
if (data.charAt(i) == separator || i == maxIndex) {
found++;
strIndex[0] = strIndex[1] + 1;
strIndex[1] = (i == maxIndex) ? i + 1 : i;
}
}
return found > index ? data.substring(strIndex[0], strIndex[1]) : “”;
}

On message for dimmer topic update brightness

  • alias: Switch office light
    trigger:
    platform: state
    entity_id: sensor.normalised_office_dimmer_brightness_256
    action:
    service: light.turn_on
    entity_id: light.hue_color_lamp
    data_template:
    brightness: ‘{{states.sensor.normalised_office_dimmer_brightness_256.state}}’

Changing out the on trigger automation for state resulted in no errors at the 0 rotation and full scale rotation.

  • platform: template
    sensors:
    normalised_office_dimmer_brightness:
    friendly_name: ‘Normalised Office Dimmer Brightness’
    value_template: “{{ states.sensor.office_dimmer_brightness.state | int **2 }}”

Using this in this string for the normalised dimmer brightness, gives a non linear or to the power of 2 increase in brightness. I set the rotary encoder at 1 pulse per revolution and for a maximum count of 16. 16 ** 2 = 256. It give alot finer control at low dimming where required and get’s to full brightness in 16 steps or just over 3/4 turn. Really like this project I learnt a lot from it. Thankyou.

1 Like

Nice! Thanks for sharing

1 Like

I’m getting so much out of this project, if you set up the input as a normal switch, then the mqtt output toggles 0 > 1 > 0 everytime you press it. You only need one automation to toggle the light in Home Assistant. In addition if you set up rules on the ESP easy and a second binary sensor you can get a long press on the button to give you a second functionality like a scene, ie turn off all the other lights ect…

2 Likes

You’re doing great!
If you find a way to do short/long press then I’d love to know!
Eg short to toggle light, long to to toggle relay or similar

I would too love to be able to toggle the relay on a long press! Great work from you both @hijinx @Paul_Fisher Just got a blanking plate and some rotary knobs so almost ready to install the final product! :smile:

1 Like

OK there are two ways to do it, I’m set up on button1 with state as the variable name.

Here is the first, the issue is you have to wait 1 sec before you get a message for a short press or a long press, so the button feels unresponsive

on Button1#State=1 do
timerSet,1,1
endon

on rules#timer=1 do
if [Button1#State]=0
//Action if button is short pressed
Publish cmnd/21cameron/lounge_dimmer1/button1/state,1
Delay 100
Publish cmnd/21cameron/lounge_dimmer1/button1/state,0
else
//Action if button is still pressed
Publish cmnd/21cameron/lounge_dimmer1/button1/state,2
Delay 100
Publish cmnd/21cameron/lounge_dimmer1/button1/state,0
endif
endon

1 Like

1 Like