Analogue style rotary dimmer switch - Sonoff + ESPEasy + MQTT

If you make a dummy device called timer1 set the value name to state and the decimals to 0 with a 1 second interval.

You will get the value 1 on the mqtt immediately after you release the button on the short press, your cycle time is still 1 sec, it may be 2 before you can press again but the button feels responsive on the short press.

on Button1#State=1 do
TaskValueSet 3,1,0
TimerSet,1,1
Delay 200
endon

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

on Button1#State=0 do
if [Timer1#State]=0
Publish cmnd/21cameron/lounge_dimmer1/button1/state,1
Delay 200
Publish cmnd/21cameron/lounge_dimmer1/button1/state,0
endif
endon

1 Like

This is my latest implementation and what I recommend, this is the rule in espeasy configuration on the dimmer

on rotary1#state do
TaskValueSet 2,1,[rotary1#state]*[rotary1#state]
Publish cmnd/21cameron/%sysname%/dimmer1/state,[dimmer1#state]
endon

on button1#state=1 do
if [dimmer1#state]!=0 and [timer1#state]=2
TaskValueSet 2,1,0
Publish cmnd/21cameron/%sysname%/dimmer1/state,[dimmer1#state]
TaskValueSet 12,1,1
else
TaskValueSet 12,1,0
endif
TimerSet,1,1
endon

on button1#state=0 do
else
if [timer1#state]=0 and [dimmer1#state]=0
TaskValueSet 2,1,256
Publish cmnd/21cameron/%sysname%/dimmer1/state,[dimmer1#state]
TaskValueSet 12,1,0
Delay 100
endif
endon

on rules#timer=1 do
TaskValueSet 12,1,2
if [button1#state]=1
TaskValueSet 2,1,[rotary1#state]*[rotary1#state]
Publish cmnd/21cameron/%sysname%/dimmer1/state,[dimmer1#state]
endif
endon

lights.yaml from Home Assistant

  • platform: mqtt
    name: “house_dimmer2_3”
    command_topic: “cmnd/21cameron/house/dimmer2/pwm3”
    state_topic: “stat/21cameron/house/dimmer2/RESULT”
    state_value_template: ‘{{ value_json[“PWM”][“PWM3”] }}’
    payload_on: “512”
    payload_off: “0”
    on_command_type: ‘brightness’
    brightness_scale: “512”
    brightness_state_topic: “stat/21cameron/house/dimmer2/RESULT”
    brightness_value_template: ‘{{ value_json[“PWM”][“PWM3”] }}’
    brightness_command_topic: “cmnd/21cameron/house/dimmer2/pwm3”
    qos: 1

customize.yaml so you can see where the dimmer id ties to the master bed

light.house_dimmer2_3:
friendly_name: Master Bed 1
hidden: false

automations.yaml in a format the gui editor will accept from Home Assistant

  • id: ‘1527475466411’
    alias: master_bed lights dim
    trigger:
    • entity_id: sensor.master_bed_sw1_dimmer1
      platform: state
      condition: []
      action:
    • data_template:
      entity_id: light.house_dimmer2_3
      brightness: ‘{{ states.sensor.master_bed_sw1_dimmer1.state }}’
      service: light.turn_on

sensors.yaml you only need one sensor for everything from Home Assistant

  • platform: mqtt
    state_topic: “cmnd/21cameron/master_bed_sw1/dimmer1/state”
    name: “master_bed_sw1_dimmer1”
    retain: true

All of the button / encoder logic is done on the ESP easy. It works like this.

Button Press > On > Light On full brightness (256 published) > rotary encoder works and will dim light on turning it will dim from last position, 16 steps in non linear (to the power of 2 between 0 and 256)

Button Press > Off > Light Off zero brightness (0 published)

Long Button Press > On to the last dimmer setting

It took me hours to work at the logic with the limited nested statements available but it is the least amount of code and configuration in home assistant with the most functionality. It could be used to control a dimmer directly without home assistant and you can use multiple dimmer controls/locations on the same light, it can mimic the old hallway/2 way setups. Hope it helps someone

Thanks for sharing. You’ve taken my project and improved in so many ways! Big thanks

Thanks for the nice post. I do not get it set-up correctly though. My ESP Mega does not show a rules section. I have the rules1.txt file and can edit that I just put your content there. However nothing ever gets published.

Any idea why?

HI,

In your advanced config tab you will see enable rules check box.

I’ll do a screen shot.

Thanks
Paul

Hello. This an amazing project I really want to get implemented into my own design. I don’t suppose you are able to upload your config files and easyesp files?

Kind Regards

Jake

Try reading the thread.

I do apologise. I didn’t see the post…

Hi, I like this project very much. Thanks for sharing. Is the bulb in your project led bulb? Or the light bulb of the direct 240V?

Does this rotary encoder require 5V power supply? Is there a 5V on sonoff? Can 3V power supply?

The rotary encoder will work with 3v3. Not sure about 3v.

As I understand it this works with lights that are dimmable using wifi, zwave etc, They could be 240v, 120v, 24/12/5v leds, in fact it is voltage agnostic, as long as home assistant can control the light.

Thank you for the reply. It’s 3v3. It’s already purchased.

Should we be able to control the direct current bulbs, and the AC bulbs can’t control dimming?

This project supports only smart bulbs.
It’s essentially a mqtt controller.
It is not a standard dimmer and will not dim an incandescent or non smart led bulb

Thank you, understand!