Toggle Relay with Rules?
Moderators: grovkillen, Stuntteam, TD-er
Toggle Relay with Rules?
I have set this rule:
on sw#Switch do
if relay#Switch=1
gpio,16,1
else
gpio,16,0
endif
endon
On ESP-12F (build 107) GPIO0 is connected to a switch (device sw), GPIO16 is connected to a relay. Also this output is connected to GPIO12 (device relay) for read back the status of the relay.
I want to toggle the relay with the switch, but the rule above does not work.
Anybody a little help ?
on sw#Switch do
if relay#Switch=1
gpio,16,1
else
gpio,16,0
endif
endon
On ESP-12F (build 107) GPIO0 is connected to a switch (device sw), GPIO16 is connected to a relay. Also this output is connected to GPIO12 (device relay) for read back the status of the relay.
I want to toggle the relay with the switch, but the rule above does not work.
Anybody a little help ?
Re: Toggle Relay with Rules?
on my sonoff works:
on SW_1#Switch do
if [relais#Switch]=1
gpio,12,0
gpio,13,1
else
gpio,12,1
gpio,13,0
endif
endon
on SW_1#Switch do
if [relais#Switch]=1
gpio,12,0
gpio,13,1
else
gpio,12,1
gpio,13,0
endif
endon
Re: Toggle Relay with Rules?
How are these GPIO's connected?
Re: Toggle Relay with Rules?
On the sonoff GPIO12 is the relais and GPIO13 is a green LED that will light when the relais is activated.
GPIO12 does the switching via a FET switch, so when GPIO12 is 'high' the relais is 'ON'.
When GPIO13 is 'low' the LED is 'ON'.
See this page for the schematis of the sonoff: http://wiki.iteadstudio.com/images/6/6b ... hmatic.pdf
GPIO12 does the switching via a FET switch, so when GPIO12 is 'high' the relais is 'ON'.
When GPIO13 is 'low' the LED is 'ON'.
See this page for the schematis of the sonoff: http://wiki.iteadstudio.com/images/6/6b ... hmatic.pdf
Re: Toggle Relay with Rules?
Ok, i have tested it with your rule, but it does not toggle the relay.
Toggling means: 1 push of the pushbutton switches on the relay. The next push on the
pushbutton switches off the relay.
Maybe another solution with rules possible? Otherwise i will consider using a litte PIC microcontroller for the toggling function
of my pushbutton.
Toggling means: 1 push of the pushbutton switches on the relay. The next push on the
pushbutton switches off the relay.
Maybe another solution with rules possible? Otherwise i will consider using a litte PIC microcontroller for the toggling function
of my pushbutton.
-
- Normal user
- Posts: 30
- Joined: 31 Oct 2015, 08:00
Re: Toggle Relay with Rules?
Hey mgennip
Here's my setup -
Device1
Device: Switch input
Name: SwitchIn
Delay: 0
IDX/Var: 1
1st GPIO: GPIO-0
Pull UP: Yes
Inversed: No
Switch Type: Switch
Switch Button Type: Normal Switch
Send Boot State: No
Send Data: No
Value Name 1: Switch
Device2
Device: Switch input
Name: Relay
Delay: 0
IDX/Var: 2
1st GPIO: GPIO-12
Pull UP: Yes
Inversed: No
Switch Type: Switch
Switch Button Type: Normal Switch
Send Boot State: Yes
Send Data: Yes
Value Name 1: Switch
Device3
Device: Switch input
Name: PowerOn
Delay: 0
IDX/Var: 3
1st GPIO: GPIO-2
Pull UP: Yes
Inversed: No
Switch Type: Switch
Switch Button Type: Normal Switch
Send Boot State: Yes
Send Data: Yes
Value Name 1: Switch
And in the rules -
on SwitchIn#Switch=0 do
if [Relay#Switch]=1
gpio,12,0
gpio,13,1
else
gpio,12,1
gpio,13,0
endif
endon
The toggle works. Note that I don't worry about monitoring the switch as it is tied to the relay using the rules. All I do is monitor the relay. In OpenHAB, I get "esp_xxx/Relay/Switch 1" or "esp_xxx/Relay/Switch 0" when the relay is toggled.
Device3 is a kludge as I wanted a way for OpenHAB to know that the sonoff has just been turned on. In OpenHAB, I get a MQTT message "esp_xxx/PowerOn/Switch 1" when I first turn on sonoff. It seems to work and I don't have the time to figure out a better way.
For your setup, you will need to use GPIO-16 where I have GPIO-12. You can ignore GPIO-13 as you don't have an LED indicator (like I have on the sonoff) and also Device3 if you don't need OpenHAB or whatever you're using to know that you have just powered on the ESP.
Good luck!
Here's my setup -
Device1
Device: Switch input
Name: SwitchIn
Delay: 0
IDX/Var: 1
1st GPIO: GPIO-0
Pull UP: Yes
Inversed: No
Switch Type: Switch
Switch Button Type: Normal Switch
Send Boot State: No
Send Data: No
Value Name 1: Switch
Device2
Device: Switch input
Name: Relay
Delay: 0
IDX/Var: 2
1st GPIO: GPIO-12
Pull UP: Yes
Inversed: No
Switch Type: Switch
Switch Button Type: Normal Switch
Send Boot State: Yes
Send Data: Yes
Value Name 1: Switch
Device3
Device: Switch input
Name: PowerOn
Delay: 0
IDX/Var: 3
1st GPIO: GPIO-2
Pull UP: Yes
Inversed: No
Switch Type: Switch
Switch Button Type: Normal Switch
Send Boot State: Yes
Send Data: Yes
Value Name 1: Switch
And in the rules -
on SwitchIn#Switch=0 do
if [Relay#Switch]=1
gpio,12,0
gpio,13,1
else
gpio,12,1
gpio,13,0
endif
endon
The toggle works. Note that I don't worry about monitoring the switch as it is tied to the relay using the rules. All I do is monitor the relay. In OpenHAB, I get "esp_xxx/Relay/Switch 1" or "esp_xxx/Relay/Switch 0" when the relay is toggled.
Device3 is a kludge as I wanted a way for OpenHAB to know that the sonoff has just been turned on. In OpenHAB, I get a MQTT message "esp_xxx/PowerOn/Switch 1" when I first turn on sonoff. It seems to work and I don't have the time to figure out a better way.
For your setup, you will need to use GPIO-16 where I have GPIO-12. You can ignore GPIO-13 as you don't have an LED indicator (like I have on the sonoff) and also Device3 if you don't need OpenHAB or whatever you're using to know that you have just powered on the ESP.
Good luck!
Re: Toggle Relay with Rules?
Thank you!
I have tested it and changed a bit. Now it is working, this is my rule:
on SwitchIn#Switch=1 do
if [Relay#Switch]=1
gpio,16,0
else
gpio,16,1
endif
endon
BTW; Is there somewhere a programming manual about using rules? eg. commands, parameters?
I have tested it and changed a bit. Now it is working, this is my rule:
on SwitchIn#Switch=1 do
if [Relay#Switch]=1
gpio,16,0
else
gpio,16,1
endif
endon
BTW; Is there somewhere a programming manual about using rules? eg. commands, parameters?
-
- Normal user
- Posts: 30
- Joined: 31 Oct 2015, 08:00
Re: Toggle Relay with Rules?
I don't think there's a manual as such, I might be wrong though. I followed the tutorial to learn plus many trial and errors -
http://www.esp8266.nu/index.php/Tutorial_Rules
Have fun!
http://www.esp8266.nu/index.php/Tutorial_Rules
Have fun!
Re: Toggle Relay with Rules?
I'm using an Electrodragon running R121 controlled via MQTT for a garage door controller. Input 1 (switch 1) will be the garage door sensor, relay 1 is the garage door opener. I have it all working fine in Home Assistant but I want to be able to toggle the relay off automatically when it is turned on by MQTT.
Does anyone know if a rule can do that?
P.S If you want to toggle the two relays on the Electrodragon units when you push each button, here is the rule for that.
To use the buttons with rules in order to toggle the relay, the “Switch Button Type” needs to be defined as a “Push Button Active Low”. So you'll have four switch devices; two as buttons and two as relays.
Does anyone know if a rule can do that?
P.S If you want to toggle the two relays on the Electrodragon units when you push each button, here is the rule for that.
Code: Select all
on button1#state do
if [button1#state]=0
gpio,13,1
else
gpio,13,0
endif
endon
on button2#state do
if [button2#state]=0
gpio,12,1
else
gpio,12,0
endif
endon
Numerous Sonoff's and Shelly's, Home Assistant on Intel NUC i3, Aeotec Z-Stick (Gen 5), deCONZ Zigbee Stick, 3 x Echo Dot's and 5 x Google Home
Re: Toggle Relay with Rules?
Solved.
Works a treat in Home Assistant.
Update 14th Feb 2017. To prevent the relay from toggling on boot, you need to add a bit of code to the beginning.
Code: Select all
on relay1#state do
if [relay1#state]=1
timerSet 1,2 // 2 second timer
endon
on Rules#Timer=1 do
gpio,13,0 // Turn off relay
endon
Update 14th Feb 2017. To prevent the relay from toggling on boot, you need to add a bit of code to the beginning.
Code: Select all
on System#Boot do
gpio,14,0 // Prevent relay turning on during boot
endon
on relay#state do
if [relay#state]=1
timerSet,1,1 // 1 second timer
endon
on Rules#Timer=1 do
gpio,14,0 // Turn off relay
endon
Last edited by xbmcnut on 14 Feb 2017, 03:34, edited 1 time in total.
Numerous Sonoff's and Shelly's, Home Assistant on Intel NUC i3, Aeotec Z-Stick (Gen 5), deCONZ Zigbee Stick, 3 x Echo Dot's and 5 x Google Home
Re: Toggle Relay with Rules?
Just using this topic instead of openign a new one as I believe is pretty much connected.
I'm trying to properly setup as ESP8266 module to manage 3 devices:
- DHT22 (temp/hum monitoring)
- physical push button (mounted on the wall)
- relay (to control room light on/off)
Basically I've setup one rule in ESPEasy as below:
Which works fine but I'm experiencing a big delay; when I push the button the relay change status but if I press it again in the next 1 or 2 seconds (I can't measure accuratly) the command is doing nothing.
Monitoring the behaviour it seems there is a delay for the pushbutton device (it stays "1" for a while even if I release the pushbutton).
I can guess this is done to prevent some debouncing effect, so I was wondering if there's a way to change this delay or maybe to dela with this bouncing in a different and more reactive way.
My problem of feedback gets worst with my digital GUI as it is based on MQTT and this delay is clearly shown in the GUI updated (when I push the button the relay reacts immediately but the MQTT topic arrives to the broker with same delay explained above...)
Thanks a lot for you help and sorry if may explanation is maybe confusing!
I'm trying to properly setup as ESP8266 module to manage 3 devices:
- DHT22 (temp/hum monitoring)
- physical push button (mounted on the wall)
- relay (to control room light on/off)
Basically I've setup one rule in ESPEasy as below:
Code: Select all
on Pulsante1#Status=1 do
if [Rele1#Status]=1
gpio,5,1
else
gpio,5,0
endif
endon
Monitoring the behaviour it seems there is a delay for the pushbutton device (it stays "1" for a while even if I release the pushbutton).
I can guess this is done to prevent some debouncing effect, so I was wondering if there's a way to change this delay or maybe to dela with this bouncing in a different and more reactive way.
My problem of feedback gets worst with my digital GUI as it is based on MQTT and this delay is clearly shown in the GUI updated (when I push the button the relay reacts immediately but the MQTT topic arrives to the broker with same delay explained above...)
Thanks a lot for you help and sorry if may explanation is maybe confusing!
My TINDIE Store where you can find all ESP8266 boards I manufacture --> https://www.tindie.com/stores/GiovanniCas/
My Wiki Project page with self-made PCB/devices --> https://www.letscontrolit.com/wiki/inde ... :Papperone
My Wiki Project page with self-made PCB/devices --> https://www.letscontrolit.com/wiki/inde ... :Papperone
Re: Toggle Relay with Rules?
I experienced the same thing once, but then I optimized the delay settings in ESPEasy, and I took care of the settings in the Arduino IDE, like flash mode QUI, 80MHz flash speed, etc.
I can't even tell exactly what setting was the most effective, but afterwards the lag was gone.
If this still doesn't help, you could also try to increase the processor speed to 160MHz.
I can't even tell exactly what setting was the most effective, but afterwards the lag was gone.
If this still doesn't help, you could also try to increase the processor speed to 160MHz.
Re: Toggle Relay with Rules?
Thanks uhrheber, about delays in ESPEasy I already have all to zero unfortuantely this did not helpuhrheber wrote:I experienced the same thing once, but then I optimized the delay settings in ESPEasy, and I took care of the settings in the Arduino IDE, like flash mode QUI, 80MHz flash speed, etc.
I can't even tell exactly what setting was the most effective, but afterwards the lag was gone.
If this still doesn't help, you could also try to increase the processor speed to 160MHz.

I was not aware that Arduino flash settings can impact the speed of the MP but I'll give it a try...
If you have more ideas/suggestions on what you've done will be more than welcome!!!
My TINDIE Store where you can find all ESP8266 boards I manufacture --> https://www.tindie.com/stores/GiovanniCas/
My Wiki Project page with self-made PCB/devices --> https://www.letscontrolit.com/wiki/inde ... :Papperone
My Wiki Project page with self-made PCB/devices --> https://www.letscontrolit.com/wiki/inde ... :Papperone
Re: Toggle Relay with Rules?
As I imagine overclocking my wemos' did not help...
I'm sure it's not a microprocessor speed issue (as I wrote the command from my GUI using MQTT is reactive as it should and I can press the switch without suffering any delays).
The delay is somehow in the ESPEasy way of managing "Switch input" when configured as "Normal Switch"; any idea?
I'm sure it's not a microprocessor speed issue (as I wrote the command from my GUI using MQTT is reactive as it should and I can press the switch without suffering any delays).
The delay is somehow in the ESPEasy way of managing "Switch input" when configured as "Normal Switch"; any idea?
My TINDIE Store where you can find all ESP8266 boards I manufacture --> https://www.tindie.com/stores/GiovanniCas/
My Wiki Project page with self-made PCB/devices --> https://www.letscontrolit.com/wiki/inde ... :Papperone
My Wiki Project page with self-made PCB/devices --> https://www.letscontrolit.com/wiki/inde ... :Papperone
Re: Toggle Relay with Rules?
It seems to be that the "lag" or delay (or whatever you call it!) is generated by the Events management 
Does anyone know how frequent the events are parsed and eventually actions taken?

Does anyone know how frequent the events are parsed and eventually actions taken?
My TINDIE Store where you can find all ESP8266 boards I manufacture --> https://www.tindie.com/stores/GiovanniCas/
My Wiki Project page with self-made PCB/devices --> https://www.letscontrolit.com/wiki/inde ... :Papperone
My Wiki Project page with self-made PCB/devices --> https://www.letscontrolit.com/wiki/inde ... :Papperone
Re: Toggle Relay with Rules?
I've got the same problem and would also be interested to reduce this lag as it means that people tend to push the button several times in a row thinking the first time didn' t work and then lights starts flashing ... not ideal.
- paveleremin
- Normal user
- Posts: 12
- Joined: 29 Dec 2016, 10:19
Re: Toggle Relay with Rules?
Hello every one!
This functionality works fine, I mean toggle relay with button via Rules.
BUT... all off you guys have some delay between pressing button?
I mean, it's not possible to press button, for example, 2 times in 1 second.
Maybe it's possible to make some Device that will connect button and relay directly?
This functionality works fine, I mean toggle relay with button via Rules.
BUT... all off you guys have some delay between pressing button?
I mean, it's not possible to press button, for example, 2 times in 1 second.
Maybe it's possible to make some Device that will connect button and relay directly?
Who is online
Users browsing this forum: Ahrefs [Bot] and 28 guests