Rules GPIO state and toggle
Moderators: grovkillen, Stuntteam, TD-er
Rules GPIO state and toggle
Hello,
I searched the forum for GPIO status return within the Rules but without any luck.
I have a push button switch on input GPIO 14 (ONOFF#Switch) and a Solid State Relay on output GPIO 16
The objective is to toggle the status of GPIO 16 when there is an event on GPIO 14
Something like :
On ONOFF#Switch do
if [gpio,16]=1
then
gpio,16,0
else
gpio,16,1
endif
endon
I could not get the GPIO status return condition to work even after guestimating various syntax combintations.
Thank you
Dimitri
I searched the forum for GPIO status return within the Rules but without any luck.
I have a push button switch on input GPIO 14 (ONOFF#Switch) and a Solid State Relay on output GPIO 16
The objective is to toggle the status of GPIO 16 when there is an event on GPIO 14
Something like :
On ONOFF#Switch do
if [gpio,16]=1
then
gpio,16,0
else
gpio,16,1
endif
endon
I could not get the GPIO status return condition to work even after guestimating various syntax combintations.
Thank you
Dimitri
-D
Re: Rules GPIO state and toggle
similar question (and still without answer) here
http://www.esp8266.nu/forum/viewtopic.php?f=6&t=2029.
http://www.esp8266.nu/forum/viewtopic.php?f=6&t=2029.
Re: Rules GPIO state and toggle
I get the log of the pin with :
status,gpio,16
{
"log": "GPIO 16 Set to 1",
"plugin": 1,
"pin": 16,
"mode": "output",
"state": 1
}
One would expect the value of state (0 or 1)to be comperatable by default as Rules do not parse the entire expression.
status,gpio,16
{
"log": "GPIO 16 Set to 1",
"plugin": 1,
"pin": 16,
"mode": "output",
"state": 1
}
One would expect the value of state (0 or 1)to be comperatable by default as Rules do not parse the entire expression.
-D
Re: Rules GPIO state and toggle
What about using the switch type "push button" like shown in the wiki: http://www.esp8266.nu/index.php/Switch
Also, this pic shows how to get the state of a switch and change it's state accordingly (toggle it):

In both cases, you have to define the GPIO as a switch first.
Code: Select all
Instead of a conventional state or latching push button, you could also use a momentary push button switch. In this case the ESP Easy will toggle between on/off.

In both cases, you have to define the GPIO as a switch first.
Re: Rules GPIO state and toggle
The input Switch is defined as "input : pin mode" (under hardware tab) and assigned ONOFF name. The value of that returns without a problem.
It is the state of the output gpio I am looking for. The same gpio pin I want to toggle.
It is the state of the output gpio I am looking for. The same gpio pin I want to toggle.
-D
Re: Rules GPIO state and toggle
The example from the wiki works, it toggles the output.
So they just define also the output as a switch, and read it's state.
So they just define also the output as a switch, and read it's state.
Re: Rules GPIO state and toggle
"Input Switch" is the only switch option definition available. (R120)
What am i missing ?
What am i missing ?
-D
Re: Rules GPIO state and toggle
Define your switch as "Switch Button Type: Push Button Active Low".
The write the rule:
on Button#Value do
if [Button#Value] = 1
EXTGPIO,16,1
else
EXTGPIO,16,0
endif
endon
Just tried it, works as a toggle button.
The write the rule:
on Button#Value do
if [Button#Value] = 1
EXTGPIO,16,1
else
EXTGPIO,16,0
endif
endon
Just tried it, works as a toggle button.
Re: Rules GPIO state and toggle
This way GPIO,16 toggles based on the state of the push button switch on GPIO,14, but push button will go high when pressed and then back to low when released (or vice versa) so GPIO,16 will always end up with 1, no ? This works with a latch type switch, but I do not see how this would work with a push button.
Need to be able to compare the state of GPIO,16 within the IF condition. (Not the state of the input switch. This is just for triggering the event)
Need to be able to compare the state of GPIO,16 within the IF condition. (Not the state of the input switch. This is just for triggering the event)
-D
Re: Rules GPIO state and toggle
Just try it, it'll work with a push button.
The button will be debounced and the button value only changes when a high to low edge is detected (active low switch), or when a low to high edge is detected (active high switch).
Meaning, every time you push the button, the value Button#Value toggles.
It works, I tried it yesterday.
You can even configure it as a one button dimmer.
The button will be debounced and the button value only changes when a high to low edge is detected (active low switch), or when a low to high edge is detected (active high switch).
Meaning, every time you push the button, the value Button#Value toggles.
It works, I tried it yesterday.
You can even configure it as a one button dimmer.
Re: Rules GPIO state and toggle
Hi,
This way we toggle the output based on the state of the input (while assuming the state of the output has not changed in the meantime).
The project is a light switch that can be toggled manualy via push button, actually a capacitive touch sensor (http://www.aerial.net/shop/product/161_ ... ensor.html ) that acts as a push button (high while touch sensed then back to low when finger removed).
The same light switch can also be controlled by external HTTP request so the correct way to handle this is to read the state of the actual output switch.
This way we toggle the output based on the state of the input (while assuming the state of the output has not changed in the meantime).
The project is a light switch that can be toggled manualy via push button, actually a capacitive touch sensor (http://www.aerial.net/shop/product/161_ ... ensor.html ) that acts as a push button (high while touch sensed then back to low when finger removed).
The same light switch can also be controlled by external HTTP request so the correct way to handle this is to read the state of the actual output switch.
-D
Re: Rules GPIO state and toggle
Ok, found it.
Define device one as Switch Input, Push Button Active Low, pullup on, GPIO 13. Name it Lightswitch. Name the value1 as value.
Define device two as Switch Input, Normal Switch, pullup off, GPIO 12. Name it GPIO12. Name the value1 as value.
Connect a push button from GPIO 13 to ground, and an LED from GPIO 12 to ground.
Now write the following rules:
Now you can push the button to toggle the LED.
And every time you call http://<IP_of_your_ESP8266>/control?cmd=event,toggle , the LED toggles as well.
You can also switch the LED explicitly on or off by calling: http://<IP_of_your_ESP8266>/control?cmd=GPIO,12,on (or ,off).
The next button press or call of the toggle URL will toggle the LED to the opposite state.
Have fun!
Define device one as Switch Input, Push Button Active Low, pullup on, GPIO 13. Name it Lightswitch. Name the value1 as value.
Define device two as Switch Input, Normal Switch, pullup off, GPIO 12. Name it GPIO12. Name the value1 as value.
Connect a push button from GPIO 13 to ground, and an LED from GPIO 12 to ground.
Now write the following rules:
Code: Select all
on Lightswitch#value do
if [GPIO12#value] = 0
GPIO,12,1
else
GPIO,12,0
endif
endon
on toggle do
if [GPIO12#value] = 1
GPIO,12,0
else
GPIO,12,1
endif
endon
And every time you call http://<IP_of_your_ESP8266>/control?cmd=event,toggle , the LED toggles as well.
You can also switch the LED explicitly on or off by calling: http://<IP_of_your_ESP8266>/control?cmd=GPIO,12,on (or ,off).
The next button press or call of the toggle URL will toggle the LED to the opposite state.
Have fun!
Re: Rules GPIO state and toggle
You are the man !!!
I owe you a cold one (or two)
Thank you
Dimitri
I owe you a cold one (or two)
Thank you
Dimitri
-D
Re: Rules GPIO state and toggle
Glad I could help.
Next thing to try would be coupling two or more ESP8266 directly (without AP), and using one as relay driver and switch input, the other ones only as remote switches.
Maybe by just letting the remote ones call the event URL, or using the new EasyGlobalSync function.
BTW:
The Wiki is really great, but it could use some more real world examples. Would it be of interest if I put together some popular scenarios like this one and write a step-by-step
tutorial?
Next thing to try would be coupling two or more ESP8266 directly (without AP), and using one as relay driver and switch input, the other ones only as remote switches.
Maybe by just letting the remote ones call the event URL, or using the new EasyGlobalSync function.
BTW:
The Wiki is really great, but it could use some more real world examples. Would it be of interest if I put together some popular scenarios like this one and write a step-by-step
tutorial?
Re: Rules GPIO state and toggle
You got my vote !! documentation and examples could use all the help they can take.
-D
-
- Normal user
- Posts: 103
- Joined: 16 Sep 2015, 20:32
Re: Rules GPIO state and toggle
Please do!!uhrheber wrote: The Wiki is really great, but it could use some more real world examples. Would it be of interest if I put together some popular scenarios like this one and write a step-by-step tutorial?
Re: Rules GPIO state and toggle
So I tried to add another remote switch, using the udp sync method, as calling urls from rules isn't yet implemented.
I defined a toggle switch on the new board, and added the following rule:
This works, but isn't stable. After a while, the remote switch looses contact to the main node.
Anybody know of any better method? (Besides using mqtt).
I defined a toggle switch on the new board, and added the following rule:
Code: Select all
on Lightswitch2#value do
sendTo 1,event,toggle
endon
Anybody know of any better method? (Besides using mqtt).
Re: Rules GPIO state and toggle
I use MQTT and all works with multiple ESPEasy switches controlling a single relay.
As well using NodeRed you can create soft link (switch to relay) and control all node behaviour.
I only still need to find a solution to fix the >1 sec delay in switch-GPIO reset once button is released (bounce)
As well using NodeRed you can create soft link (switch to relay) and control all node behaviour.
I only still need to find a solution to fix the >1 sec delay in switch-GPIO reset once button is released (bounce)
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: Rules GPIO state and toggle
I'm using R148 and I have two switches, one on GPIO12 as a sensor for my garage door and another on GPIO14 to drive the relay. The relay needs to turn on for one second then off again to emulate the integrated garage controller. The GPIO12 switch is called 'reed' and is set to 'Push Button Active Low to toggle and the GPIO14 switch is called 'relay'. This is rule I applied to turn the relay off after 1 second. The board is a NodeMCU v3 but I'll be using a low voltage Sonoff in the production unit.
If I send a MQTT message to /devicename/gpio/14 with a payload on 1, the relay turns on for one second which is great! I have the Value Name 1: for the 'reed' switch set to state so that when the door is closed, I get /garage/reed/state with either 1 or 0 (opened or closed).
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
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: Rules GPIO state and toggle
Thank you! This is exactly what I needed. Can someone suggest a code tidy up for that below? I added code to yours to activate the LED on GPIO13 and popped some UDP commands at the bottom to allow two Sonoff's to work in unison in a 2-way light circuit. It my situation, the bulb (load) is only connected to one Sonoff which is common (power in on one switch, load on the other). This works a treat and both Sonoff's come on an off together as do the LED's. Send /devicename/gpio/12,1 also turns on both relays at the same time and the button/s can be used to turn off again.uhrheber wrote:Ok, found it.
Code: Select all
on Lightswitch#value do if [GPIO12#value] = 0 GPIO,12,1 else GPIO,12,0 endif endon on toggle do if [GPIO12#value] = 1 GPIO,12,0 else GPIO,12,1 endif endon
Code: Select all
on button#state do
if [relay#state] = 0
GPIO,12,1
else
GPIO,12,0
endif
endon
on toggle do
if [relay#state] = 1
GPIO,12,0
else
GPIO,12,1
endif
endon
on relay#state do
if [relay#state] = 1
gpio,13,0
else
gpio,13,1
endif
endon
on relay#state do
if [relay#state] = 1
sendTo 2,GPIO,12,1
else
sendTo 2,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: Rules GPIO state and toggle
xbmcnut wrote:I'm using R148 and I have two switches, one on GPIO12 as a sensor for my garage door and another on GPIO14 to drive the relay. The relay needs to turn on for one second then off again to emulate the integrated garage controller. The GPIO12 switch is called 'reed' and is set to 'Push Button Active Low to toggle and the GPIO14 switch is called 'relay'. This is rule I applied to turn the relay off after 1 second. The board is a NodeMCU v3 but I'll be using a low voltage Sonoff in the production unit.
If I send a MQTT message to /devicename/gpio/14 with a payload on 1, the relay turns on for one second which is great! I have the Value Name 1: for the 'reed' switch set to state so that when the door is closed, I get /garage/reed/state with either 1 or 0 (opened or closed).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
hi, in a similiar setup(using udp sento> i had problems with reliability. after some weeks i found, the 1-state was not detected--> the timer never fired --> relais stayed at1. now im using "pulse" command, which includes the off command. not sure how to use it with mqtt.
Short pulses
To send a pulse to a certain pin:
http://<ESP IP address>/control?cmd=Pulse,<pin>,<state>,<duration>
Example to send an active high pulse on GPIO 2 for 500 mSeconds:
http://<ESP IP address>/control?cmd=Pulse,2,1,500
Domoticz on Raspi 2 -- 14 ESP units (hacked Sonoff,NodeMCUs, Wemos, self-built units) running with RC140- Mega 2.0.0 dev8
Re: Rules GPIO state and toggle
Other device has sentto 1 I guess.uhrheber wrote:... Can someone suggest a code tidy up for that below? ...
Other device has sendTo 2,GPIO,12,1.Code: Select all
on button#state do if [relay#state] = 0 GPIO,12,1 else GPIO,12,0 endif endon on toggle do if [relay#state] = 1 GPIO,12,0 else GPIO,12,1 endif endon on relay#state do if [relay#state] = 1 gpio,13,0 else gpio,13,1 endif endon on relay#state do if [relay#state] = 1 sendTo 2,GPIO,12,1 else sendTo 2,GPIO,12,0 endif endon

Code: Select all
on button#state do
event,toggle
SendTo 2,event,toggle
endon
on toggle do
if [relay#state] = 1
GPIO,12,0
GPIO,13,1
else
GPIO,12,1
GPIO,13,0
endif
endon
Re: Rules GPIO state and toggle
Thank you! I've seen that already. Units do occasionally get out of sync. Not very often but enough to be annoying. Any change you could provide some sample code for your recommended approach using TaskValueSet?paxi wrote:uhrheber wrote:... Can someone suggest a code tidy up for that below? ...
The only problem with this is whenever a sendto command is missed (connection-less UDP!) both units will be out of sync - better replace sendto with sendtohttp. The remote command changes from gpio to TaskValueSet that alters the value of button#state.
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: Rules GPIO state and toggle
Unfortunately with your much cleaner version, I can no longer publish a MQTT topic to /sonoff2/gpio/12 with a payload of 1 to control both Sonoff's. It only turns on the one I instructed. The button on the Sonoff controls both however.paxi wrote:Other device has sentto 1 I guess.uhrheber wrote:... Can someone suggest a code tidy up for that below? ...
Other device has sendTo 2,GPIO,12,1.Code: Select all
on button#state do if [relay#state] = 0 GPIO,12,1 else GPIO,12,0 endif endon on toggle do if [relay#state] = 1 GPIO,12,0 else GPIO,12,1 endif endon on relay#state do if [relay#state] = 1 gpio,13,0 else gpio,13,1 endif endon on relay#state do if [relay#state] = 1 sendTo 2,GPIO,12,1 else sendTo 2,GPIO,12,0 endif endon
![]()
Should do the work with the same rules in unit 2 (with sendto 1 of course).Code: Select all
on button#state do event,toggle SendTo 2,event,toggle endon on toggle do if [relay#state] = 1 GPIO,12,0 GPIO,13,1 else GPIO,12,1 GPIO,13,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: Rules GPIO state and toggle
Too bad that TaskValueSet doesn't work via mttq...
We need another rule in the "master":
Btw. how was the toggle event triggered in your original code?
We need another rule in the "master":
Code: Select all
on relay#state do
if [relay#state]=1
GPIO 13,0
else
GPIO 13,1
endif
SendTo 2,event,toggle
endon
Re: Rules GPIO state and toggle
I had not tried the TaskValueSet method as I'm a hardware guy trying to work my way through the code. This is my full code that works well albeit with a 95% success rate due to UDP.paxi wrote:Too bad that TaskValueSet doesn't work via mttq...
We need another rule in the "master":
Btw. how was the toggle event triggered in your original code?Code: Select all
on relay#state do if [relay#state]=1 GPIO 13,0 else GPIO 13,1 endif SendTo 2,event,toggle endon
Code: Select all
on button#state do
if [relay#state] = 0
GPIO,12,1
else
GPIO,12,0
endif
endon
on toggle do
if [relay#state] = 1
GPIO,12,0
else
GPIO,12,1
endif
endon
on relay#state do
if [relay#state] = 1
gpio,13,0
else
gpio,13,1
endif
endon
on relay#state do
if [relay#state] = 1
sendTo 2,GPIO,12,1
else
sendTo 2,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: Rules GPIO state and toggle
"toggle" is a user defined event, no native implementation in the firmware. If you dont call it from elsewhere its just a dead piece of code that never runs. It does the same as your button#state rule, that's why you think it works. Takes a while to get the hang of event driven programming
Have a look at the global sync feature. It works only in one direction though, you'd need a dummy device for the other direction - and more rules...
Or you set up some pull/push rules triggered by system#boot.
Edit: like this

Have a look at the global sync feature. It works only in one direction though, you'd need a dummy device for the other direction - and more rules...
Or you set up some pull/push rules triggered by system#boot.
Edit: like this
Code: Select all
On System#Boot do Sendto (otherdevice),event,pushstate
On pushstate do Sendto (otherdevice),event,getstate=[relay#state]
On getstate do gpio 12,%eventvalue%
Re: Rules GPIO state and toggle
Thanks for clearing that up. So much for my copy and paste from other threads. It clearly was not needed. To get around my intermittent UDP issue, I went with MQTT instead. This works flawlessly. Ensure that 'send boot state' is off for the relay on each device and that the retain flag is checked under advanced settings. With those set, if one unit reboots for any reason, it will come back up, connect with the broker and return to the 'last known state' keeping the two switches it sync. Nailed!paxi wrote:"toggle" is a user defined event, no native implementation in the firmware.
Code: Select all
on button#state do
if [relay#state] = 0
GPIO,12,1
else
GPIO,12,0
endif
endon
on relay#state do
if [relay#state] = 1
gpio,13,0
else
gpio,13,1
endif
endon
on relay#state do
if [relay#state] = 1
Publish /sonoff2/gpio/12,1
else
Publish /sonoff2/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: Rules GPIO state and toggle
Glad you got it working now! And so easy - I don't use a sever thus wasn't aware of that tickbox.
You can still sum up the two seperate relay#state rules into one (like in my toggle example above).
You can still sum up the two seperate relay#state rules into one (like in my toggle example above).

Re: Rules GPIO state and toggle
Thank you! I tidied up my code and it works religiously via MQTT so very happy. I appreciate your input.paxi wrote:You can still sum up the two seperate relay#state rules into one (like in my toggle example above).

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: Rules GPIO state and toggle
I tried the use of EXTGPIO command inside a rule with a recent version (Mega dev10) and it doesn't work!
On which version did you try ? To understand when it broke.
Tks
Re: Rules GPIO state and toggle
Hi
Im following this thread. I tried the set up. I gave the result I want.
I use capacitive touch module. When i touch the module the led on.. then when i touch again it off..
The problem now is the second touch gave a delay result.. its mean i need to touch again n again until the led off.. from my serial connection i saw that the system gave a HTTP connection failed. I wonder what is wrong. Please help me.. Thanks
Attached picture.
https://imgur.com/a/rbEXF
Im following this thread. I tried the set up. I gave the result I want.
I use capacitive touch module. When i touch the module the led on.. then when i touch again it off..
The problem now is the second touch gave a delay result.. its mean i need to touch again n again until the led off.. from my serial connection i saw that the system gave a HTTP connection failed. I wonder what is wrong. Please help me.. Thanks
Attached picture.
https://imgur.com/a/rbEXF
- grovkillen
- Core team member
- Posts: 3621
- Joined: 19 Jan 2017, 12:56
- Location: Hudiksvall, Sweden
- Contact:
Re: Rules GPIO state and toggle
What message delay you have?
ESP Easy Flasher [flash tool and wifi setup at flash time]
ESP Easy Webdumper [easy screendumping of your units]
ESP Easy Netscan [find units]
Official shop: https://firstbyte.shop/
Sponsor ESP Easy, we need you

ESP Easy Webdumper [easy screendumping of your units]
ESP Easy Netscan [find units]
Official shop: https://firstbyte.shop/
Sponsor ESP Easy, we need you



Re: Rules GPIO state and toggle
i attached an image in the link bottom my post
Re: Rules GPIO state and toggle
Hi all,uhrheber wrote: ↑28 Sep 2016, 21:19 Ok, found it.
Define device one as Switch Input, Push Button Active Low, pullup on, GPIO 13. Name it Lightswitch. Name the value1 as value.
Define device two as Switch Input, Normal Switch, pullup off, GPIO 12. Name it GPIO12. Name the value1 as value.
Connect a push button from GPIO 13 to ground, and an LED from GPIO 12 to ground.
Now write the following rules:
Now you can push the button to toggle the LED.Code: Select all
on Lightswitch#value do if [GPIO12#value] = 0 GPIO,12,1 else GPIO,12,0 endif endon on toggle do if [GPIO12#value] = 1 GPIO,12,0 else GPIO,12,1 endif endon
And every time you call http://<IP_of_your_ESP8266>/control?cmd=event,toggle , the LED toggles as well.
You can also switch the LED explicitly on or off by calling: http://<IP_of_your_ESP8266>/control?cmd=GPIO,12,on (or ,off).
The next button press or call of the toggle URL will toggle the LED to the opposite state.
Have fun!
with version R147 the previous code works, with MEGA-dev12 or dev13 it doesn't work. Actually it works for a few clicks then the unit reboots.
To be more precise, what does not work anymore in MEGA is the fact to declare a relay as an Input Switch.
It crashes the ESP.
Any idea why?
Re: Rules GPIO state and toggle
Please also check with later release builds.
Since dev12 and dev13, a lot of builds were made.
Since dev12 and dev13, a lot of builds were made.
Re: Rules GPIO state and toggle
Thanks Pulse control works nicely! In the rules no less.toffel969 wrote: ↑16 Feb 2017, 07:59xbmcnut wrote:I'm using R148 and I have two switches, SHORTENED
hi, in a similiar setup(using udp sento> i had problems with reliability. after some weeks i found, the 1-state was not detected--> the timer never fired --> relais stayed at1. now im using "pulse" command, which includes the off command. not sure how to use it with mqtt.
Short pulses
To send a pulse to a certain pin:
http://<ESP IP address>/control?cmd=Pulse,<pin>,<state>,<duration>
Example to send an active high pulse on GPIO 2 for 500 mSeconds:
http://<ESP IP address>/control?cmd=Pulse,2,1,500
Code: Select all
On System#Boot do
gpio,15,0
endon
On garage#door=1 do
pulse,15,1,100
gpio,15,0
publish /MyHome/Garage/Door/,The Garage Door is Moving
//if else needed for garage#door=1 > 1000
//publish /MyHome/Garage/, RELAY FAULT - URGENT
endon
Code: Select all
// Topic == Payload
/%sysname%/Garage/cmd == GPIO,15,1,status,
/%sysname%/Garage/gpio/15/[b],[/b]1 //This one I don't like because I can't figure out how to get a JSON output to /$sysname$/status
/%sysname%/Garage/cmd == PULSE,15,1,status, // With this one I don't actually need the rule in the Code above but will keep it for redundancy, so I can't leave the relay on.
Re: Rules GPIO state and toggle
I use R147 over Sonoff S20. Not a problem to tie a button to a relay:
Button (btn) listens on GPIO0, active low. The problem is to control the relay from multiple sources:
- with button,
- with MQTT,
- with events.
The problem comes from button state: every press-release it changes from 0 to 1 and vice versa.
That means
if (button turned relay on and event turned relay off) then
on next button click the btn#switch value will be 0.
This won't activate relay despite it's off.
My question is:
How to toggle relay?
I.e. how change the GPIO status in dependency of its current state?
I.e. how to read the output GPIO status and invert it?
Tried This solution from earlier post. In R147 the [GPIO12#value] does not work: it's always (null). Maybe should I define something in "Devices"?
Thank you.
Code: Select all
On btn#Switch do
gpio,12,[btn#Switch]
endon
- with button,
- with MQTT,
- with events.
The problem comes from button state: every press-release it changes from 0 to 1 and vice versa.
That means
if (button turned relay on and event turned relay off) then
on next button click the btn#switch value will be 0.
This won't activate relay despite it's off.
My question is:
How to toggle relay?
I.e. how change the GPIO status in dependency of its current state?
I.e. how to read the output GPIO status and invert it?
Tried This solution from earlier post. In R147 the [GPIO12#value] does not work: it's always (null). Maybe should I define something in "Devices"?
Thank you.
- grovkillen
- Core team member
- Posts: 3621
- Joined: 19 Jan 2017, 12:56
- Location: Hudiksvall, Sweden
- Contact:
Re: Rules GPIO state and toggle
The solution is to funnel all the inputs to an event.
MQTT message/payload (topic <unit_name/cmd):
Or
Code: Select all
On btn#Switch Do
If [btn#Switch]=0
event,ToggleSwitch=1
Else
event,ToggleSwitch=0
EndIf
EndOn
On ToggleSwitch Do
gpio,12,%eventvalue%
EndOn
Code: Select all
event,ToggleSwitch=1
Code: Select all
event,ToggleSwitch=0
ESP Easy Flasher [flash tool and wifi setup at flash time]
ESP Easy Webdumper [easy screendumping of your units]
ESP Easy Netscan [find units]
Official shop: https://firstbyte.shop/
Sponsor ESP Easy, we need you

ESP Easy Webdumper [easy screendumping of your units]
ESP Easy Netscan [find units]
Official shop: https://firstbyte.shop/
Sponsor ESP Easy, we need you



Re: Rules GPIO state and toggle
Thank you for quick response.
Here is full code block made with your help:
There are 2 line which create event,ToggleSwitch=0: in a button and in timer end sections.
This works while no button pressed again BEFORE timer ends. Or timer shuts relay off.
If relays off by timer then next button click just skipped. The reason is the same - button keeps own state and flips it with no relation to relay status.
Is there a way to read ToggleSwitch value when button pressed to decide which new ToggleSwitch value to set?
I think the "status" variable can be help by Dummy Device. However, I can't catch how to work with it
Thank you.
Here is full code block made with your help:
Code: Select all
On btn#Switch Do
publish,shm/%sysname%/button,event[btn#Switch]
If [btn#Switch]=0
event,ToggleSwitch=1
Else
event,ToggleSwitch=0
EndIf
EndOn
On ToggleSwitch=0 Do
gpio,12,%eventvalue%
publish,shm/%sysname%/relay,%eventvalue%
EndOn
On ToggleSwitch=1 Do
gpio,12,%eventvalue%
publish,shm/%sysname%/relay,%eventvalue%
publish,shm/%sysname%/timerset,%systime%
timerset,1,10 // run timer for 10 sec
EndOn
On Rules#Timer=1 do
event,ToggleSwitch=0
publish,shm/%sysname%/timerend,%systime%
endon
This works while no button pressed again BEFORE timer ends. Or timer shuts relay off.
If relays off by timer then next button click just skipped. The reason is the same - button keeps own state and flips it with no relation to relay status.
Is there a way to read ToggleSwitch value when button pressed to decide which new ToggleSwitch value to set?
I think the "status" variable can be help by Dummy Device. However, I can't catch how to work with it

Thank you.
Last edited by grhhm on 02 Mar 2018, 19:48, edited 1 time in total.
- grovkillen
- Core team member
- Posts: 3621
- Joined: 19 Jan 2017, 12:56
- Location: Hudiksvall, Sweden
- Contact:
Re: Rules GPIO state and toggle
You could use a dummy device value to dump and check the current state, or use a switch device.
ESP Easy Flasher [flash tool and wifi setup at flash time]
ESP Easy Webdumper [easy screendumping of your units]
ESP Easy Netscan [find units]
Official shop: https://firstbyte.shop/
Sponsor ESP Easy, we need you

ESP Easy Webdumper [easy screendumping of your units]
ESP Easy Netscan [find units]
Official shop: https://firstbyte.shop/
Sponsor ESP Easy, we need you



Re: Rules GPIO state and toggle
OK, managed with it. Short guide for others.
Objective: Use Dummy device as internal variable to synchronize between different events and single actuator.
1. Add task DummyDevice. I use task #6, name 'glob' (global variable), type: SENSOR_TYPE_SINGLE, value name: rs (relay status)
2. In rules you can write into rs variable as: taskvalueset 6,1,0 // where 6 is a task#, 1 is a rs variable number, 0 is a value.
3. In rules to read the variable value you can use [glob#rs] // i.e. not numbers of task and variable, but their names. (grass?)
4. In rules you can not create trigger on task value change (at least, I could not in R147). I used taskvalueset to set required value and then event,Toggle,[glob#rs] to generate the event. Then event Toggle is processed as usual.
Hope this helps
Objective: Use Dummy device as internal variable to synchronize between different events and single actuator.
1. Add task DummyDevice. I use task #6, name 'glob' (global variable), type: SENSOR_TYPE_SINGLE, value name: rs (relay status)
2. In rules you can write into rs variable as: taskvalueset 6,1,0 // where 6 is a task#, 1 is a rs variable number, 0 is a value.
3. In rules to read the variable value you can use [glob#rs] // i.e. not numbers of task and variable, but their names. (grass?)
4. In rules you can not create trigger on task value change (at least, I could not in R147). I used taskvalueset to set required value and then event,Toggle,[glob#rs] to generate the event. Then event Toggle is processed as usual.
Hope this helps

Re: Rules GPIO state and toggle
Here is my solution to switch relay by push button, by event from another ESP ( sending command "lamp_change" ) or by command "lamp_on" or "lamp_off" from Domoticz
GPIO 12 relay, GPIO 13 LED ( sonoff )
Local switch is defined like SW01#Switch, relay like rele#Switch on GPIO 12
With this rule, you can control your sonoff by local switch, by switch on others ESPeasys by sending event "lamp_change"
In this cases state of relay is reported to Domoticz, so Domoticz dashboard switch state corresponds actual state of relay.
And you can control relay from Domoticz by sending events "lamp_off" and "lamp_on
mega 2.0-dev13
Code: Select all
on lamp_on do
gpio,12,1
gpio,13,0
endon
on lamp_off do
gpio,12,0
gpio,13,1
endon
on lamp_change do
if [rele#Switch]=1
event,lamp_off
SendToHTTP 192.168.1.253,8080,/json.htm?type=command¶m=switchlight&idx=19&switchcmd=Off
else
event,lamp_on
SendToHTTP 192.168.1.253,8080,/json.htm?type=command¶m=switchlight&idx=19&switchcmd=On
endif
endon
on SW01#Switch do
event,lamp_change
endon
Local switch is defined like SW01#Switch, relay like rele#Switch on GPIO 12
With this rule, you can control your sonoff by local switch, by switch on others ESPeasys by sending event "lamp_change"
In this cases state of relay is reported to Domoticz, so Domoticz dashboard switch state corresponds actual state of relay.
And you can control relay from Domoticz by sending events "lamp_off" and "lamp_on
mega 2.0-dev13
Re: Rules GPIO state and toggle
thanks a lot Kimot
works perfect
works perfect
Who is online
Users browsing this forum: Semrush [Bot] and 17 guests