Rules for sonoff dual R2

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
s_p2003
Normal user
Posts: 17
Joined: 02 Jan 2018, 11:34

Rules for sonoff dual R2

#1 Post by s_p2003 » 19 Dec 2020, 21:30

hi to all


i need help.
I would like to create a rule for the activation of the relays (on gpio 12 and gpio 5) when I press the button on the case (gpio 10).
The button on the case is set to "normal switch" so each press it switches 0-1-0.

How can it be done?

tnks
ru.PNG
ru.PNG (12.4 KiB) Viewed 7062 times

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

Re: Rules for sonoff dual R2

#2 Post by TD-er » 19 Dec 2020, 21:43

You can set the switch task to be "Push button active low"

Let's assume the task is named "Button1" with the taskvalue "switch" and relay is on GPIO 12.
Then add this to the rules:

Code: Select all

on Button1#switch do
  if [Button1#switch]=1
    gpio,12,1
  else
    gpio,12,0
  endif
endon

s_p2003
Normal user
Posts: 17
Joined: 02 Jan 2018, 11:34

Re: Rules for sonoff dual R2

#3 Post by s_p2003 » 20 Dec 2020, 00:06

this is my rule but it does not work


i tried with Active High, Active Low and normal switch

Code: Select all

on pulsante#stato do
  if [pulsante#stato]=1
    gpio,12,1
    gpio,5,0
  elseif
    gpio,12,0
    gpio,5,1
  elseif
    gpio,12,1
    gpio,5,1
  elseif
    gpio,12,0
    gpio,5,0
  endif
endon
sw.PNG
sw.PNG (109.8 KiB) Viewed 7051 times

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

Re: Rules for sonoff dual R2

#4 Post by TD-er » 20 Dec 2020, 00:25

What do you think this code will do?

Code: Select all

on pulsante#stato do
  if [pulsante#stato]=1
    gpio,12,1
    gpio,5,0
  elseif
    gpio,12,0
    gpio,5,1
  elseif
    gpio,12,1
    gpio,5,1
  elseif
    gpio,12,0
    gpio,5,0
  endif
endon
I think the rules parser also doesn't know what to do with it.

Strip it to something like this:

Code: Select all

on pulsante#stato do
  if [pulsante#stato]=1
    gpio,12,1
    gpio,5,0
  elseif
    gpio,12,0
    gpio,5,1
  endif
endon

s_p2003
Normal user
Posts: 17
Joined: 02 Jan 2018, 11:34

Re: Rules for sonoff dual R2

#5 Post by s_p2003 » 20 Dec 2020, 09:03

First, thank you.
I would like to replicate the behavior of a monostable relay.
first press relay 1 closed and relay 2 open
second press relay 2 open and relay 2 closed
third press relay 1 and 2 closed
fourth press relay 1 and 2 open.
on system boot all open

and maybe also set the gpio led

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

Re: Rules for sonoff dual R2

#6 Post by TD-er » 20 Dec 2020, 11:46

You should use a counter and set the counter and pin states at boot.

Code: Select all

on System#Boot do
  GPIO,12,0   // Relay 1 open
  GPIO,5,1    // Relay 2 closed
  let,1,0    // Set variable 1 to 0
endon

on pulsante#stato do
  if [pulsante#stato]=1
    let,1,[int#1]+1
    if [int#1]=1
      gpio,12,1
      gpio,5,0
    elseif [int#1]=2
      gpio,12,0
      gpio,5,1
    elseif [int#1]=3
      gpio,12,0
      gpio,5,0
    elseif [int#1]=4
      let,1,0
      gpio,12,1
      gpio,5,1
    endif
  endif
endon
Not tested, but it should be something like this.
See: https://espeasy.readthedocs.io/en/lates ... Rules.html

s_p2003
Normal user
Posts: 17
Joined: 02 Jan 2018, 11:34

Re: Rules for sonoff dual R2

#7 Post by s_p2003 » 20 Dec 2020, 17:46

it works, but need to press button 2 times for status change
the gpio of button is set to active high

User avatar
Ath
Normal user
Posts: 3506
Joined: 10 Jun 2018, 12:06
Location: NL

Re: Rules for sonoff dual R2

#8 Post by Ath » 20 Dec 2020, 19:57

I think the switch setting should be changed from push button to normal switch.
You might also want to set the debounce value to 50 or 100 msec, to avoid 'nervous' like switching when lightly touching the button.
/Ton (PayPal.me)

s_p2003
Normal user
Posts: 17
Joined: 02 Jan 2018, 11:34

Re: Rules for sonoff dual R2

#9 Post by s_p2003 » 20 Dec 2020, 20:25

Perfect!!

thank you

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

Re: Rules for sonoff dual R2

#10 Post by TD-er » 21 Dec 2020, 14:17

Yep, mistake from my side.
I first suggested it based on the idea of mimicking the switch state to the relay state, but now we have a state machine of > 2 states, you must use it as a normal switch again as Ath suggested.

Glad it is working.

Post Reply

Who is online

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