Rules GPIO state and toggle

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
User avatar
toffel969
Normal user
Posts: 469
Joined: 03 Jan 2017, 10:58
Location: Germany

Re: Rules GPIO state and toggle

#21 Post by toffel969 » 16 Feb 2017, 07:59

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.

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

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

paxi
Normal user
Posts: 121
Joined: 02 Feb 2017, 00:48
Location: Germany

Re: Rules GPIO state and toggle

#22 Post by paxi » 16 Feb 2017, 09:21

uhrheber wrote:... Can someone suggest a code tidy up for that below? ...

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
Other device has sendTo 2,GPIO,12,1.
Other device has sentto 1 I guess. ;)

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
Should do the work with the same rules in unit 2 (with sendto 1 of course). 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.

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

Re: Rules GPIO state and toggle

#23 Post by xbmcnut » 16 Feb 2017, 09:31

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.
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?
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: Rules GPIO state and toggle

#24 Post by xbmcnut » 16 Feb 2017, 10:04

paxi wrote:
uhrheber wrote:... Can someone suggest a code tidy up for that below? ...

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
Other device has sendTo 2,GPIO,12,1.
Other device has sentto 1 I guess. ;)

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
Should do the work with the same rules in unit 2 (with sendto 1 of course).
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.
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

paxi
Normal user
Posts: 121
Joined: 02 Feb 2017, 00:48
Location: Germany

Re: Rules GPIO state and toggle

#25 Post by paxi » 16 Feb 2017, 11:07

Too bad that TaskValueSet doesn't work via mttq...
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 
Btw. how was the toggle event triggered in your original code?

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

Re: Rules GPIO state and toggle

#26 Post by xbmcnut » 16 Feb 2017, 11:48

paxi wrote:Too bad that TaskValueSet doesn't work via mttq...
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 
Btw. how was the toggle event triggered in your original code?
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.

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
I figured that 'toggle' was natively supported and somehow it works fine. I can control both Sonoff's together by pushing either devices button or by sending MQTT to either devices GPIO. Reboot one Sonoff though and it comes back with the relay off (which is OK) but it creates and out of sync situation.
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

paxi
Normal user
Posts: 121
Joined: 02 Feb 2017, 00:48
Location: Germany

Re: Rules GPIO state and toggle

#27 Post by paxi » 16 Feb 2017, 12:14

"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

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%

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

Re: Rules GPIO state and toggle

#28 Post by xbmcnut » 17 Feb 2017, 11:28

paxi wrote:"toggle" is a user defined event, no native implementation in the firmware.
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!

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

paxi
Normal user
Posts: 121
Joined: 02 Feb 2017, 00:48
Location: Germany

Re: Rules GPIO state and toggle

#29 Post by paxi » 17 Feb 2017, 13:01

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

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

Re: Rules GPIO state and toggle

#30 Post by xbmcnut » 20 Feb 2017, 09:18

paxi wrote:You can still sum up the two seperate relay#state rules into one (like in my toggle example above). ;)
Thank you! I tidied up my code and it works religiously via MQTT so very happy. I appreciate your input. :)
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

marci
New user
Posts: 9
Joined: 18 Aug 2017, 20:53

Re: Rules GPIO state and toggle

#31 Post by marci » 19 Aug 2017, 13:04

uhrheber wrote: 27 Sep 2016, 21:14 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.

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

momon
New user
Posts: 6
Joined: 27 Nov 2017, 13:56

Re: Rules GPIO state and toggle

#32 Post by momon » 06 Dec 2017, 11:15

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

User avatar
grovkillen
Core team member
Posts: 3621
Joined: 19 Jan 2017, 12:56
Location: Hudiksvall, Sweden
Contact:

Re: Rules GPIO state and toggle

#33 Post by grovkillen » 06 Dec 2017, 12:44

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

momon
New user
Posts: 6
Joined: 27 Nov 2017, 13:56

Re: Rules GPIO state and toggle

#34 Post by momon » 06 Dec 2017, 21:33

grovkillen wrote: 06 Dec 2017, 12:44 What message delay you have?
i attached an image in the link bottom my post

leel1967l
Normal user
Posts: 73
Joined: 30 Nov 2017, 18:29

Re: Rules GPIO state and toggle

#35 Post by leel1967l » 01 Feb 2018, 21:34

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:

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
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!
Hi all,
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?

TD-er
Core team member
Posts: 8749
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Rules GPIO state and toggle

#36 Post by TD-er » 01 Feb 2018, 22:35

Please also check with later release builds.
Since dev12 and dev13, a lot of builds were made.

HAbuild
Normal user
Posts: 15
Joined: 10 Feb 2018, 08:16

Re: Rules GPIO state and toggle

#37 Post by HAbuild » 10 Feb 2018, 15:50

toffel969 wrote: 16 Feb 2017, 07:59
xbmcnut 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
Thanks Pulse control works nicely! In the rules no less.

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
MQTT

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. 
	
	 
I haven't figured out how to code the next bit yet, just started Today lol. Feel free to poke best practice suggestions on that code.

grhhm
Normal user
Posts: 29
Joined: 23 Oct 2016, 20:15

Re: Rules GPIO state and toggle

#38 Post by grhhm » 02 Mar 2018, 13:40

I use R147 over Sonoff S20. Not a problem to tie a button to a relay:

Code: Select all

On btn#Switch do
   gpio,12,[btn#Switch]
endon
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.

User avatar
grovkillen
Core team member
Posts: 3621
Joined: 19 Jan 2017, 12:56
Location: Hudiksvall, Sweden
Contact:

Re: Rules GPIO state and toggle

#39 Post by grovkillen » 02 Mar 2018, 14:53

The solution is to funnel all the inputs to an event.

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
MQTT message/payload (topic <unit_name/cmd):

Code: Select all

event,ToggleSwitch=1
Or

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

grhhm
Normal user
Posts: 29
Joined: 23 Oct 2016, 20:15

Re: Rules GPIO state and toggle

#40 Post by grhhm » 02 Mar 2018, 18:54

Thank you for quick response.
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
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.
Last edited by grhhm on 02 Mar 2018, 19:48, edited 1 time in total.

User avatar
grovkillen
Core team member
Posts: 3621
Joined: 19 Jan 2017, 12:56
Location: Hudiksvall, Sweden
Contact:

Re: Rules GPIO state and toggle

#41 Post by grovkillen » 02 Mar 2018, 19:37

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

grhhm
Normal user
Posts: 29
Joined: 23 Oct 2016, 20:15

Re: Rules GPIO state and toggle

#42 Post by grhhm » 03 Mar 2018, 13:10

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

kimot
Normal user
Posts: 190
Joined: 12 Oct 2017, 20:46

Re: Rules GPIO state and toggle

#43 Post by kimot » 05 Mar 2018, 14:04

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

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&param=switchlight&idx=19&switchcmd=Off
  else
    event,lamp_on
    SendToHTTP 192.168.1.253,8080,/json.htm?type=command&param=switchlight&idx=19&switchcmd=On
  endif
endon

on SW01#Switch do
  event,lamp_change
endon
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

jvm1000
Normal user
Posts: 11
Joined: 11 Feb 2017, 14:23

Re: Rules GPIO state and toggle

#44 Post by jvm1000 » 08 Oct 2018, 21:25

thanks a lot Kimot
works perfect

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 54 guests