I have a towel warmer outside, that has a power button and a timer button. Both buttons gets pressed and returns to their original physical position, almost like a touch-button, but with a little more depression.
The power button gets pressed (and released) and the device gets powered on. The same button gets pressed again and it turns the power off. I can easily replace this action with a relay on, relay off to supply power and the same action to turn off the power.
The problem is the second button, the timer button.
This button has four possible results. Each time it gets pressed, it increments the timer by 15minute increments. When it reach 60 (or position 4) and gets pressed again, it goes back to 15 minutes. This can also be seen as a rotary switch with four possible positions/selections. 15, 30, 45 and 60 minutes (followed by 15, 30, 45…)
Each position of this timer select button, has its own led and the led turns on to indicate the selected time. When the time is up (say after 45 minute selection) the device turns off. When the power gets applied again, the selection stays on 45 minutes, it does not default to 15minutes or position 1.
I want to use a second relay to replace this timer-button. So basically its almost like a rotary switch. Press it, it moves to the next position, when it reach 60 (position 4), it goes to position 1 on the next press ( or 15 minutes), etc
I am no expert but I did something similar to sequence lights in a Halloween prop using a NodeMCU Mini (8266). The NodeMCU Mini controlled relays that turned the lights on or off.
#Global variable counts the button presses
globals:
- id: button_press_count
type: int
restore_value: no
initial_value: '0'
#Use a binary sensor for the button and a script for handling actions.
binary_sensor:
- platform: gpio
pin: GPIO0
name: "Next Light"
on_press:
then:
- lambda: |
id(button_press_count) += 1;
if (id(button_press_count) == 1) {
// Turn on the first switch
id(switch1).turn_on();
// Turn off all others
id(switch2).turn_off();
id(switch3).turn_off();
id(switch4).turn_off();
} else if (id(button_press_count) == 2) {
// Turn on the second switch
id(switch2).turn_on();
//Turn off all others
id(switch1).turn_off();
id(switch3).turn_off();
id(switch4).turn_off();
} else if (id(button_press_count) == 3) {
// Turn on the third switch
id(switch3).turn_on();
//Turn off all others
id(switch1).turn_off();
id(switch2).turn_off();
id(switch4).turn_off();
} else if (id(button_press_count) == 4) {
// Turn on the fourth switch
id(switch4).turn_on();
//Turn off all others
id(switch1).turn_off();
id(switch2).turn_off();
id(switch3).turn_off();
} else {
// Reset and turn everything off
id(button_press_count) = 0;
id(switch1).turn_off();
id(switch2).turn_off();
id(switch3).turn_off();
id(switch4).turn_off();
}
switch:
- platform: gpio
pin: GPIO1
id: switch1
- platform: gpio
pin: GPIO2
id: switch2
- platform: gpio
pin: GPIO3
id: switch3
- platform: gpio
pin: GPIO4
id: switch4
I will give this code a try, but I think I’m confused as to why the four switches gpio1-4,? I only have one switch and not 4.
The way you code that it seems that I need four relays.
What you exactly would like to do? Automate the button presses with relay in parallel to the device button or in series the way that hardware button is connected to Esp?
Parallel is easier, but that way Esp doesn’t know if device is used with hardware button.
Also, you probably have logic level voltage on that button, maybe you could wire it directly or through optocoupler.