Push button dimmer - how?

I am trying to configure ESPHome and HASS.IO to use a two-way (on)-off-(on) momentary rockers switch to switch and dim a light defined on HASS (not directly connected to the ESP8266).

I have managed to get the switching action working but am stumped with the dimming part. I’ve tried in various ways, including based on this one by @DeanoX, but not succeeded yet.

What I would like to do is as follows:

Up button short push - light on
Down button short push - light off
Up button long push - light increases in brightness while button pushed
Down button long push - light decreases in brightness while button pushed

This is the code I have so far:

esphome:
  name: esp8266_002
  platform: ESP8266
  board: esp01_1m

wifi:
  ssid: "Secret"
  password: "TopSecret"

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:

binary_sensor:
  - platform: gpio
    pin:
      number: 00
      mode: INPUT_PULLUP
      inverted: TRUE
    name: "Up"
    device_class: light
    on_multi_click:
      - timing:
          - ON for 1s to 2s
          - OFF for at least 0.5s
        then:
          - logger.log: "Up Single Long Clicked"
# test code follows - to be replaced by dimmer code
          - homeassistant.service: 
              service: light.turn_on
              data:
                entity_id: "light.study1"
                flash: short
# end test code
      - timing:
          - ON for at most 1s
          - OFF for at least 0.5s
        then:
          - logger.log: "Up Single short Clicked"
          - homeassistant.service:
              service: light.turn_on
              data:
                entity_id: "light.study1"
                
  - platform: gpio
    pin:
      number: 02
      mode: INPUT_PULLUP
      inverted: TRUE
    name: "Down"
    device_class: light
    on_multi_click:
      - timing:
          - ON for 1s to 2s
          - OFF for at least 0.5s
        then:
          - logger.log: "Down Single Long Clicked"
# test code follows - to be replaced by dimmer code
          - homeassistant.service: 
              service: light.turn_on
              data:
                entity_id: "light.study1"
                flash: long
# end test code
      - timing:
          - ON for at most 1s
          - OFF for at least 0.5s
        then:
          - logger.log: "Down Single short Clicked"
          - homeassistant.service:
              service: light.turn_off
              data:
                entity_id: "light.study1"                

I really would appreciate any help or guidance provided. Thanks.

1 Like

What is the problem? Does it compile and upload? If it does, what does your esphome log say as you play with the switch?

This code works but does not do all I want. It only switches on and off, so points one and two from my list above. The dimming and brightening functions don’t work and this is what I am trying to sort. I have placeholder code there now that I would like to replace with the dimming code

# test code follows - to be replaced by dimmer code
          - homeassistant.service: 
              service: light.turn_on
              data:
                entity_id: "light.study1"
                flash: short
# end test code
1 Like

So I guess my question could be expressed better:

What do I need to add to the code above to make enable dimmer functionality, please?

Thanks.

Hi,
Im just thinking loud.

But looks like you have added automation into the esphome that will control something in hass?
I would try to put the automation inside home assistant and make the esphome switch a binary sensor.

Inside home assistant, I think you should be able to do something with node-red.
Im no expert just thinking loud.

If you take a trigger-state node with the switch entity_ID and add constraint of state = on
then you should end up with a node that has two outputs, one when state is ON and one for all other states.

Once the switch is pushed it will go to ON state and send the payload to a trigger-node that waits until x amount of time until it sends out the payload.
You will have to stop it from sending the first message.
Think you can use the OFF payload as a reset payload in case you did not long press the button.
Now when the wait time has passed, the payload is sent out, you will have to add a Change-node and set a flow.dimming = true. this is for knowing if we entered the dimming automation. And parallel you should try some type of loop with brightness increments as I dont think you can send a brightness 100% and stop it in midway right?

When you release the button it triggers the OFF state and outputs on output two and here you have to use a switch-node where one output is checking if flow.dimming is true and the rest on output two.
IF flow.dimming is true, use another change-node and set flow.dimming = false (this output will not do anything else and next short press will go out on output two of the switch-node).
On output two you trigger the light service.

Oh. man, Im not even sure if I would understand this :slight_smile:
Not sure if anything here would work, maybe I should try it my self and paste the node config.
Are you using node-red?

Thanks, @LCL. Yes, you are correct, I need the button on the ESP to control the dimmable light (actually a Hue light) controlled through HASS.

I haven’t used Node-Red, heard a lot about it but I’m trying to get semi-proficient :wink: on the HASS and ESP combo before doing that. If it is the answer I’ll try it (albeit at the risk of getting even more confused! :slight_smile: )

This single-button control (or something like it) would be a great addition to the HASS suite and I’m really keen to get it working - but my knowledge level isn’t there yet!

I did use HASS only with it’s configs etc YAML files in the beginning, but it got harder and harder to have a good view over all the automations.
Switching to node-red just made it so easy (when I finally pulled all my hair off and got the logics behind it :slight_smile: ).
And it’s easy to make more advanced functions etc.
Im running hass.io on rpi so node-red is on its own docker.
You can have it running in parallel also, so you don’t have to switch and convert everything.

Here is a node-red flow for the beginning av the function, you should be able to import this into a flow.

[{"id":"93a08907.a23fc8","type":"tab","label":"Flow","disabled":false,"info":""},{"id":"f2b267f3.fa7ae8","type":"trigger-state","z":"93a08907.a23fc8","name":"","server":"dad38061.b6ae9","entityid":"binary_sensor.switch","entityidfiltertype":"exact","debugenabled":false,"constraints":[{"id":"842h0d4hwus","targetType":"this_entity","targetValue":"","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is","comparatorValueDatatype":"str","comparatorValue":"on"}],"constraintsmustmatch":"all","outputs":2,"customoutputs":[],"outputinitially":false,"state_type":"str","x":220,"y":120,"wires":[["5c0ce372.9577ec"],["5c0ce372.9577ec","f5118d7e.c1edc"]]},{"id":"5c0ce372.9577ec","type":"trigger","z":"93a08907.a23fc8","op1":"","op2":"true","op1type":"nul","op2type":"bool","duration":"1","extend":false,"units":"s","reset":"off","bytopic":"all","name":"1s Hold","x":520,"y":160,"wires":[["5321d9f5.26a4b8","615e42ac.4c20cc"]]},{"id":"f5118d7e.c1edc","type":"switch","z":"93a08907.a23fc8","name":"If dimming","property":"dimming","propertyType":"flow","rules":[{"t":"true"},{"t":"else"}],"checkall":"false","repair":false,"outputs":2,"x":530,"y":120,"wires":[["57e4ec99.090da4"],["d0186d59.23fab"]]},{"id":"5321d9f5.26a4b8","type":"change","z":"93a08907.a23fc8","name":"","rules":[{"t":"set","p":"dimming","pt":"flow","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":740,"y":160,"wires":[[]]},{"id":"57e4ec99.090da4","type":"change","z":"93a08907.a23fc8","name":"","rules":[{"t":"set","p":"dimming","pt":"flow","to":"false","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":740,"y":80,"wires":[[]]},{"id":"d0186d59.23fab","type":"api-call-service","z":"93a08907.a23fc8","name":"Toggle Light","server":"dad38061.b6ae9","service_domain":"light","service":"toggle","data":"{\"entity_id\":\"light.light\"}","mergecontext":"","output_location":"payload","output_location_type":"msg","x":730,"y":120,"wires":[[]]},{"id":"615e42ac.4c20cc","type":"api-call-service","z":"93a08907.a23fc8","name":"Dim Light","server":"dad38061.b6ae9","service_domain":"light","service":"turn_on","data":"{\"entity_id\":\"light.light\",\"brightness_pct\":10}","mergecontext":"","output_location":"payload","output_location_type":"msg","x":720,"y":300,"wires":[[]]},{"id":"6676d449.d1afcc","type":"comment","z":"93a08907.a23fc8","name":"","info":"Here you should have some type of looping for the dimmer.\nProbably a reset if trigger state goes to OFF to stop the loop","x":720,"y":240,"wires":[]},{"id":"dad38061.b6ae9","type":"server","z":"","name":"Home Assistant"}]

Wow, that looks daunting but interesting. I’ll have to spend some time before I understand it, though!

haha, well in node-red it would look like this after you import all the json code :slight_smile:

Hi Guys
This is exactly what I am looking for. A push button (Physical Button) to turn on/off Hue Light, and also to Presses to change Dimmer level of Hue Light.
I have started reading about Node-Red and am starting to under how the flows work, however still lots to learn.
I also have a question about the Push Button. How to do I get HA to define the switch?
I have read lots about scipts and sensor and it looks like in your flow it is a Binary Sensor Switch.
If that is the case, that it is a Binary Sensor, What code do I enter into Configuration file to set the Push Button as sensor? I will be connecting the Physical push button to GPIO pins on Sonoff or D1 Mini as I assume it still needs to send command somehow

I hope that makes sense.
@LCL Well done on your code, I look forward to entering this into Node-Red later and trying it :slight_smile:

Thanks
Bruce

It’s a sensor, as it is sending on or off (1 or 0) and as long as you hold the button down, it will send ON to HA

In esphome config I have just this

binary_sensor:
  - platform: gpio
    pin:
      number: GPIO0
      mode: INPUT_PULLUP
      inverted: True
    name: "Switch 1"

Switch 1 will be added into HA. It will look on the state of GPIO0 that at default is pulled up. Your button then should connect to ground.

It’s pulled up so it will send status ON but the inverted: true makes it send OFF. If you pull it down to ground (ex pushing the button) it will send ON and OFF once you let it go.

My node-red code ex. is not fully copy paste functioning. you will have to make the dimming loop, but this is a start at least.

Thanks heaps for your reply. I have heard of ESPHome however I am not sure how it works. I am guessing this code should work in HA configurator?
I understand your comments thank you. My only questions is what device do you connect your push button too?
I ask as I do not see any reference to a Device just a GPIO pin number. So my question, or what I am trying to understand is, how to HA know which GPIO0 is the Binary sensor is attached too if you have multiple binary sensors using GPIO0 around the house??
Eg. using Sonoff GPIO0 in Hallway and GPIO0 of another sonoff in Bedroom, and GPIO0 of D1 Mini in Garage.
I hope my confusing is not a silly question and hope my explanation is clear enough to understand
Thanks again in Advance. Just trying to understand so I can replicate in other rooms of house

Ok, you are not running ESPHOME on your devices today?

I’m running ESPHOME, and I manage them within Hass.io by installing the esphome on all the sonoff/esp8266 devices and the esphome dashboard on hass.io.
You can add https://github.com/esphome/hassio into the repositories inside add-on stores.
Then you install the esphome dashboard.
check this guide: https://esphome.io/guides/getting_started_hassio.html

I think you should install ESPHOME on all your sonoff or ESP8266 modules, It will communicate with HA with the API and for me it worked better and easier than tasmota and mqtt.
Inside the esphome configuration for the sonoff/8266 device you can add the config I pasted in the previous post.

All the devices will be separated, so no conflict with everyone using GPIO0 for example.
Your devices will have their own entity_id within hass.io and thats what you use to get the status of the binary_sensor.

Ok cool. Thanks @LCL
I will review ESPHome and look to set it up to use with HA
I have only used Tasmota and have found it pretty good, I will flash a device tomorrow and see how it works. I have got use to the codes, command etc for tasmota so it will be very different I am guessing. Always keen to try other option.

I want to be able to use a push button (as above) to control a hue light and also to switch other sonoff device on/off. That is, the sonoff the button is connected to is not controlling the relay, the push button controls another relay on another sonoff as it is connected to MQTT. I hope I can do this with ESPHome

Will let you know how I get on. In the mean time, did you get the Dimming to work with push button?

cheers

I switched from tasmota (and mqtt) to esphome first just to test it.
I was using tasmota on a sonoff T1 1 button wall switch.
With tasmota I could have the button work as single press (mqtt) double press (relay toggle) and hold (mqtt)
The problem was, that it sends out one payload of single press or hold to mqtt, so HA could’t see if i still hold the button as it did’t send a new payload once released.
And I did’t like the double press to toggle the relay as I did’t have use for it.

With esphome I could make the button a sensor, so HA can see how long I press the button etc.
and I can control the relay independent, so double press will not toggle the relay.

Just to make it clear, I don’t use any automations inside esphome (you can have some automation inside your module in case it disconnects from HA), I use esphome only as sensors and switches for hass.io, HA is handling all the work.

This way I can toggle the ikea trådfri bulbs with single press, a double press sets the lights color temp, and triple press turns off all lights in the house and sets a timer on the bedroom light so that I can go into bed before it gets dark :slight_smile:

For the dimming, I have’t looked into this more, only got single button wall switches, hope the OP has been testing it out.
I might test it on a bare esp8266 with two buttons to see if I can get some node-red flows, I’m no expert so will do “try and error” style, but can’t guarantee I will do that soon :slight_smile:

Sounds awesome. I understand there is a difference in using ESPHome and Tasmota firmware so keen to try and test as you do. I like to experiment to find the best solution that works for us. That is all part of the Home Automation fun for me. I spend hours reading and researching how to do what I want to achieve and I am getting there.
I am no programmer however I have learnt a great deal from others trail and errors, alone with my own far share of wins and losses :slightly_smiling_face:
With not using automations in ESPHome, that is great as I am getting my head around automations in Hass.io so thats great
Making a button as Sensor sounds interesting. Based on what you are saying it is more granular in its control of the actions from the button. This means I guess you can apply rules that are more defined.
I like the 1, 2, and 3 press options you have especially the triple press to turn all lights off with timer for Bedroom :slight_smile:
Is this defined in your Node-Red Flow above? Yes looks like it is
Well lots to learn. ESPHome, Node-Red, and Flashing sonoff with ESPHome. Daunting challenge however a great challenge :slight_smile:

This is my solution by using a single touch button. works nice & clean.

light:
  - platform: monochromatic
    name: "Livingroom Ceiling light"
    id: light_0
    output: light_0_out

# Example output entry
output:
  - platform: esp8266_pwm
    id: light_0_out
    pin: D4
    inverted: true

binary_sensor:
  - platform: gpio
    pin: D2
    id: light_0_touch
    on_click:
      then:
        - if:
            condition:
              light.is_off: light_0
            then:
              light.turn_on: 
                id: light_0
                brightness: 1.0
            else:
              light.turn_off: light_0
    on_press:
      then:
      - if:
          condition: 
            light.is_off: light_0
          then:
          - delay: 0.5s
          - while:
              condition:
                binary_sensor.is_on: light_0_touch
              then:
                - light.dim_relative:
                    id: light_0
                    relative_brightness: 5%
                    transition_length: 0.1s
                - delay: 0.1s
          else:
          - delay: 0.5s
          - while:
              condition:
                and:
                  - binary_sensor.is_on: light_0_touch
                  - light.is_on: light_0
              then:
                - light.dim_relative:
                    id: light_0
                    relative_brightness: -5%
                    transition_length: 0.1s
                - delay: 0.1s
20 Likes

Thanks for this code, friend! This is exactly what I was looking for my nightlights with one touch button. Before this code, I only turned them on and off with a button, and set the brightness in HA

I tryed your node-red code for dimming a light when holding button and I couldn’t get it to work.
Recently I made a 4 momentary buttons using a NodeMCU running Tasmota to control my light and switches. But I would like to expand the possibilities, like double press, triple press and hold doing other tasks. If possible, could you kindly post the nodes you use to achieve this? I can help you with esp8266 configuration if you need any help. Thanks.

Yes, the dimming code is just an idea.
Did not test it in real life, also I have not added any dimming nodes. just that there is a flow to the dimming part.
You will have to add more nodes to do the dimming.
Im not sure if its enough with a call to dim, or if you need to make a looping function that will call the dimming with incremental steps.
You would probably have to check when the button is released to stop the loop.

The multi-press part I can help you with, but Im not home at the moment, but from memory, what I did was having a trigger node that looks at the sensor state (button press) and after that I installed a counter node (you have to search for counter in the repository or what its called :slight_smile: )
In the counter node, you have to set a timeout (think I went with 500ms), that will reset for each incoming messages. Once the timeout is reached a message is sent out from the counter node with a count number.

This message goes into a switch node, where you add multiple outputs depending on how many presses you like to handle.

So add like count == 1, 2, 3 etc in the switch node. You can just go with count == 2, 3 and then an else for “single” press. This will also failover to single press if you press too many times. This is up to you :slight_smile:

Hope this will help.