Toggle Relay with Rules?

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
mgennip
Normal user
Posts: 22
Joined: 17 Jun 2016, 19:22

Toggle Relay with Rules?

#1 Post by mgennip » 17 Jun 2016, 19:27

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 ?

kr0815
Normal user
Posts: 132
Joined: 18 Nov 2015, 18:24

Re: Toggle Relay with Rules?

#2 Post by kr0815 » 17 Jun 2016, 21:05

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

mgennip
Normal user
Posts: 22
Joined: 17 Jun 2016, 19:22

Re: Toggle Relay with Rules?

#3 Post by mgennip » 18 Jun 2016, 19:29

How are these GPIO's connected?

costo
Normal user
Posts: 490
Joined: 21 Nov 2015, 15:03

Re: Toggle Relay with Rules?

#4 Post by costo » 19 Jun 2016, 01:20

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

mgennip
Normal user
Posts: 22
Joined: 17 Jun 2016, 19:22

Re: Toggle Relay with Rules?

#5 Post by mgennip » 19 Jun 2016, 20:42

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.

CoolWombat
Normal user
Posts: 30
Joined: 31 Oct 2015, 08:00

Re: Toggle Relay with Rules?

#6 Post by CoolWombat » 19 Jun 2016, 23:12

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!

mgennip
Normal user
Posts: 22
Joined: 17 Jun 2016, 19:22

Re: Toggle Relay with Rules?

#7 Post by mgennip » 21 Jun 2016, 08:58

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?

CoolWombat
Normal user
Posts: 30
Joined: 31 Oct 2015, 08:00

Re: Toggle Relay with Rules?

#8 Post by CoolWombat » 23 Jun 2016, 13:51

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!

xbmcnut
Normal user
Posts: 49
Joined: 17 Mar 2016, 08:49
Location: Auckland, NZ
Contact:

Re: Toggle Relay with Rules?

#9 Post by xbmcnut » 05 Oct 2016, 11:00

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.

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
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.
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

xbmcnut
Normal user
Posts: 49
Joined: 17 Mar 2016, 08:49
Location: Auckland, NZ
Contact:

Re: Toggle Relay with Rules?

#10 Post by xbmcnut » 07 Oct 2016, 11:48

Solved.

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
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 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

papperone
Normal user
Posts: 497
Joined: 04 Oct 2016, 23:16

Re: Toggle Relay with Rules?

#11 Post by papperone » 12 Oct 2016, 14:43

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:

Code: Select all

on Pulsante1#Status=1 do
  if [Rele1#Status]=1
    gpio,5,1
  else
    gpio,5,0
  endif
endon
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!
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

uhrheber
Normal user
Posts: 22
Joined: 26 Sep 2016, 14:03

Re: Toggle Relay with Rules?

#12 Post by uhrheber » 13 Oct 2016, 15:51

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.

papperone
Normal user
Posts: 497
Joined: 04 Oct 2016, 23:16

Re: Toggle Relay with Rules?

#13 Post by papperone » 13 Oct 2016, 16:05

uhrheber 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.
Thanks uhrheber, about delays in ESPEasy I already have all to zero unfortuantely this did not help :(
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

papperone
Normal user
Posts: 497
Joined: 04 Oct 2016, 23:16

Re: Toggle Relay with Rules?

#14 Post by papperone » 13 Oct 2016, 17:10

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?
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

papperone
Normal user
Posts: 497
Joined: 04 Oct 2016, 23:16

Re: Toggle Relay with Rules?

#15 Post by papperone » 14 Oct 2016, 10:05

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?
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

dlefol
Normal user
Posts: 26
Joined: 11 Nov 2015, 23:28

Re: Toggle Relay with Rules?

#16 Post by dlefol » 25 Oct 2016, 22:12

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.

User avatar
paveleremin
Normal user
Posts: 12
Joined: 29 Dec 2016, 10:19

Re: Toggle Relay with Rules?

#17 Post by paveleremin » 04 Jan 2017, 11:35

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?

Post Reply

Who is online

Users browsing this forum: No registered users and 18 guests