Page 1 of 1

Button mqtt (espeasy) for turn ON sonoff on other place (SOLVED)

Posted: 27 Sep 2017, 22:39
by r16
Hello friends, i started a new Project in my home.
I use that scheme;
Image

and my configuration sonoff is:
Image

but i have a problem inside of portal esp8266, anyone knows how configure that?
Image


my goal is when I press the esp8266 button, the sonoff switches on, and when I click again the sonoff switches off.

Re: Button mqtt (espeasy) for turn ON sonoff on other place

Posted: 28 Sep 2017, 02:28
by r16
i tried to make this but no sucess, i cant turn on my sonoff throught my esp

Image

Image

Image

Image

but doesnt work :( what is wrong?

Re: Button mqtt (espeasy) for turn ON sonoff on other place

Posted: 28 Sep 2017, 06:15
by grovkillen
Only float is possible to use in the rules arithmetic tests. Please study the rule tutorial section in the wiki. Your syntax are generally wrong.

Re: Button mqtt (espeasy) for turn ON sonoff on other place

Posted: 28 Sep 2017, 11:08
by vader
1. The scheme shows button connected to RST, not GPIO0 (I assume you know that...)
2. Switch button type must be "Push Button Active Low"
3. The Sonoffs have the relay on GPIO12 (LED on GPIO13, reversed logic!)
4. Change the value name of the switch from "power" to "state", like in the rule used
5. "endif" is missing in the rules (I don't know if this is important?)
6. Only 0 and 1 as value are allowed in the rules for a switch device

Use the "SendTo" command to switch ON/OFF a device connected to another Sonoff unit (Global Sync enabled), or use the "Publish" command for MQTT...

Now it should work.... ;)

Re: Button mqtt (espeasy) for turn ON sonoff on other place

Posted: 29 Sep 2017, 01:07
by r16
@vader

hello again, ok..... i installed ESPEASY on my two devices.

----------------------------DEVICE 1----------------------------------------


**device is nodemcu with relay.With name sonoff1**

_the config is:_

Image

_rule:_
Image

**i defined UDP 65432**
this device i want control via HA and button of my esp.

Result with that rule:
HA control - OK
button ESP01- NOT OK

........................................DEVICE 2........................

**device is ESP01 with press button.With name sonoff2**

_the config is:_

Image

_rule:_
Image

**i defined UDP 65432**

this device i want when i press button the device 1 TURN ON when presse again device 1 TURN OFF

Result with that rule:

the state of button changes - OK
Device 1 not happens nothing.

where is the problem ?

Re: Button mqtt (espeasy) for turn ON sonoff on other place

Posted: 29 Sep 2017, 08:58
by grovkillen
On device one you should switch the 0 with 1 (but you don't need a device for setting the relay, if you don't want to, since relays are operated using the GPIO command):

Code: Select all

on relay#state do
 if [relay#state]=1
  GPIO,12,0
 else
  GPIO,12,1
 endif
endon
On device two you should publish a "cmd" command. See wiki!

Code: Select all

on button#state do
 if [button#state]=0
  Publish /sonoff1/cmd,GPIO,12,0
 else
  Publish /sonoff1/cmd,GPIO,12,1
 endif
endon
Given that the device one is subscribing to the "/sonoff1/#" on the FIRST controller. (Though the correct setting would state "/%sysname%/#", see wiki!)
You should not care about the GPIO-0 and try to set it to HIGH or LOW. This is not something you do since the microcontroller only alternates between 1/0 upon a toggle of the button.

Re: Button mqtt (espeasy) for turn ON sonoff on other place

Posted: 29 Sep 2017, 09:05
by vader
Set both Sonoffs --> "1st GPIO = GPIO0"
Switch Button Type --> "Switch Button Active Low"

In Sonoff2 remove GPIO0 setting in the rules

Check the MQTT communication in the logs

Re: Button mqtt (espeasy) for turn ON sonoff on other place

Posted: 29 Sep 2017, 19:47
by r16
grovkillen wrote: 29 Sep 2017, 08:58 On device one you should switch the 0 with 1 (but you don't need a device for setting the relay, if you don't want to, since relays are operated using the GPIO command):

Code: Select all

on relay#state do
 if [relay#state]=1
  GPIO,12,0
 else
  GPIO,12,1
 endif
endon
On device two you should publish a "cmd" command. See wiki!

Code: Select all

on button#state do
 if [button#state]=0
  Publish /sonoff1/cmd,GPIO,12,0
 else
  Publish /sonoff1/cmd,GPIO,12,1
 endif
endon
Now it Works.
thank youuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu :mrgreen: :mrgreen: :mrgreen: :mrgreen: :mrgreen: :mrgreen: :mrgreen:

Re: Button mqtt (espeasy) for turn ON sonoff on other place

Posted: 29 Sep 2017, 19:58
by vader
Because the button of the Sonoffs is connected to GPIO-0. The relay does not need an entry in the device/task list. Only a switch with GPIO-0 for turning on/off the relay over a rule and GPIO-12. LED on GPIO-13...

Re: Button mqtt (espeasy) for turn ON sonoff on other place

Posted: 29 Sep 2017, 20:35
by r16
vader wrote: 29 Sep 2017, 19:58 Because the button of the Sonoffs is connected to GPIO-0. The relay does not need an entry in the device/task list. Only a switch with GPIO-0 for turning on/off the relay over a rule and GPIO-12. LED on GPIO-13...
you dont read what i wrote.
i dont have sonoff´s
my device are:
1. esp8266-01
2. nodemcu v3

Re: Button mqtt (espeasy) for turn ON sonoff on other place

Posted: 29 Sep 2017, 21:13
by grovkillen
r16 wrote: 29 Sep 2017, 19:47 Now it Works.
thank youuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu :mrgreen: :mrgreen: :mrgreen: :mrgreen: :mrgreen: :mrgreen: :mrgreen:
Glad to have helped :D

Re: Button mqtt (espeasy) for turn ON sonoff on other place

Posted: 29 Sep 2017, 22:17
by r16
@grovkillen

now i have other problem.
The button Works fine, when i press it, the relay of device 2 changes from on to off and vice versa.
but when i click on button throught HOME ASSISTANT, the status of relay changes, but if i press the button of the device 1... it doesn´t Work.

did you have a home assistant?

rule device 1:

Code: Select all

on button#state do
 if [button#state]=0
Publish /sonoff1/cmd,GPIO,12,1
 else
Publish /sonoff1/cmd,GPIO,12,0
 endif
endon

rule device 2:
no rules

Re: Button mqtt (espeasy) for turn ON sonoff on other place

Posted: 30 Sep 2017, 14:23
by grovkillen
This happens because the "button" didn't know that Home Assistant has switched the relay. To fix this you might use many different techniques MQTT Import being my favorite.

On the relay device you can I publish a topic with a 1 or 0 when the relay is switched. Let MQTT Import sniff that topic and set task value of the button accordingly.

Re: Button mqtt (espeasy) for turn ON sonoff on other place

Posted: 01 Oct 2017, 05:06
by r16
grovkillen wrote: 30 Sep 2017, 14:23 This happens because the "button" didn't know that Home Assistant has switched the relay. To fix this you might use many different techniques MQTT Import being my favorite.

On the relay device you can I publish a topic with a 1 or 0 when the relay is switched. Let MQTT Import sniff that topic and set task value of the button accordingly.

What are you saying is:
on my device (sonoff2) where i have a button

put the rule:

Code: Select all

on button#state do
 if [button#state]=0
Publish /sonoff3/cmd,GPIO,12,0
 else
Publish /sonoff3/cmd,GPIO,12,1
 endif
endon
and other device (sonoff3) create something like this?

Image


and create that rule:

Code: Select all

on mqtt_import#GPIO0 do
  if [mqtt_import#GPIO0] = 1
    gpio,12,1
  else
    gpio,12,0
  endif
endon
is this? i am trying but i am very confused.

Re: Button mqtt (espeasy) for turn ON sonoff on other place

Posted: 02 Oct 2017, 09:45
by grovkillen
Sorry for not getting back to you until now.

What you need is this rule on the relay device:

Code: Select all

on relay#state do
 if [relay#state]=1
  GPIO,12,0
  Publish /sonoff1/RelayStatus,0
 else
  GPIO,12,1
  Publish /sonoff1/RelayStatus,1
 endif
endon
And on the button device (and set up the MQTT Import to listen to "/sonoff1/RelayStatus"):

Code: Select all

on button#state do
 if [button#state]=0
  Publish /sonoff1/cmd,GPIO,12,0
 else
  Publish /sonoff1/cmd,GPIO,12,1
 endif
endon

on mqtt_import#GPIO0 do
  if [mqtt_import#GPIO0] = 1
    TaskValueSet 2,1,1 //Given that the switch device is on TaskNr 2...
  else
    TaskValueSet 2,1,0 //Given that the switch device is on TaskNr 2...
  endif
endon
Please observe that I have not tested this in practice. You may test and see if it works. Please feel free to come back :)

Re: Button mqtt (espeasy) for turn ON sonoff on other place

Posted: 02 Oct 2017, 13:14
by r16
grovkillen wrote: 02 Oct 2017, 09:45 Sorry for not getting back to you until now.

What you need is this rule on the relay device:

Code: Select all

on relay#state do
 if [relay#state]=1
  GPIO,12,0
  Publish /sonoff1/RelayStatus,0
 else
  GPIO,12,1
  Publish /sonoff1/RelayStatus,1
 endif
endon
And on the button device (and set up the MQTT Import to listen to "/sonoff1/RelayStatus"):

Code: Select all

on button#state do
 if [button#state]=0
  Publish /sonoff1/cmd,GPIO,12,0
 else
  Publish /sonoff1/cmd,GPIO,12,1
 endif
endon

on mqtt_import#GPIO0 do
  if [mqtt_import#GPIO0] = 1
    TaskValueSet 2,1,1 //Given that the switch device is on TaskNr 2...
  else
    TaskValueSet 2,1,0 //Given that the switch device is on TaskNr 2...
  endif
endon
Please observe that I have not tested this in practice. You may test and see if it works. Please feel free to come back :)
hello friend with that configuration. my relay always on off on off on off... and not stop.


i tried to change this:

Code: Select all

on relay#state do
 if [relay#state]=1
  GPIO,12,0
  Publish /sonoff1/RelayStatus,0
 else
  GPIO,12,1
  Publish /sonoff1/RelayStatus,1
 endif
endon
for this:

Code: Select all

on relay#state do
 if [relay#state]=1
  GPIO,12,1
  Publish /sonoff1/RelayStatus,1
 else
  GPIO,12,0
  Publish /sonoff1/RelayStatus,0
 endif
endon
but does not work too

Re: Button mqtt (espeasy) for turn ON sonoff on other place

Posted: 03 Oct 2017, 08:49
by grovkillen
Oooh, sorry about that!

We need to use a counter (check value) since the TaskValueSet will trigger the button#state over and over again.

Code: Select all

on System#Boot do
  Delay,5000 //Just to be sure...
  TaskValueSet 3,1,0 //Just to be sure...
endon

on button#state do
 if [button#state]=0
  TaskValueSet 3,1,1 //Given that a Dummy device is on TaskNr 3...
  Event EventTriggerButton1
 else
  TaskValueSet 3,1,1 //Given that a Dummy device is on TaskNr 3...
  Event EventTriggerButton0
 endif
endon

on mqtt_import#GPIO0 do
  if [mqtt_import#GPIO0]=1
    TaskValueSet 2,1,1 //Given that the switch device is on TaskNr 2...
  else
    TaskValueSet 2,1,0 //Given that the switch device is on TaskNr 2...
  endif
endon

on EventTriggerButton1 do
 if [Dummy#Value]=1
  Publish /sonoff1/cmd,GPIO,12,1
  TaskValueSet 3,1,0 //Set the check value to 0...
 endif
endon

on EventTriggerButton0 do
 if [Dummy#Value]=1
  Publish /sonoff1/cmd,GPIO,12,0
  TaskValueSet 3,1,0 //Set the check value to 0...
 endif
endon



Re: Button mqtt (espeasy) for turn ON sonoff on other place

Posted: 03 Oct 2017, 11:05
by vader
And why don't you use the button with Global Sync enabled? So the state should be on all devices the same and you don't have the stress with complicate sync rules!?

Re: Button mqtt (espeasy) for turn ON sonoff on other place

Posted: 04 Oct 2017, 03:54
by r16
grovkillen wrote: 03 Oct 2017, 08:49 Oooh, sorry about that!

We need to use a counter (check value) since the TaskValueSet will trigger the button#state over and over again.

Code: Select all

on System#Boot do
  Delay,5000 //Just to be sure...
  TaskValueSet 3,1,0 //Just to be sure...
endon

on button#state do
 if [button#state]=0
  TaskValueSet 3,1,1 //Given that a Dummy device is on TaskNr 3...
  Event EventTriggerButton1
 else
  TaskValueSet 3,1,1 //Given that a Dummy device is on TaskNr 3...
  Event EventTriggerButton0
 endif
endon

on mqtt_import#GPIO0 do
  if [mqtt_import#GPIO0]=1
    TaskValueSet 2,1,1 //Given that the switch device is on TaskNr 2...
  else
    TaskValueSet 2,1,0 //Given that the switch device is on TaskNr 2...
  endif
endon

on EventTriggerButton1 do
 if [Dummy#Value]=1
  Publish /sonoff1/cmd,GPIO,12,1
  TaskValueSet 3,1,0 //Set the check value to 0...
 endif
endon

on EventTriggerButton0 do
 if [Dummy#Value]=1
  Publish /sonoff1/cmd,GPIO,12,0
  TaskValueSet 3,1,0 //Set the check value to 0...
 endif
endon




I appreciate your persistence, but it does not work. I think I'm going to give up.
I never thought this was so complicated to do.

Re: Button mqtt (espeasy) for turn ON sonoff on other place

Posted: 04 Oct 2017, 03:55
by r16
vader wrote: 03 Oct 2017, 11:05 And why don't you use the button with Global Sync enabled? So the state should be on all devices the same and you don't have the stress with complicate sync rules!?
Please, Put here the steps I have to take.

Re: Button mqtt (espeasy) for turn ON sonoff on other place

Posted: 04 Oct 2017, 05:55
by grovkillen
r16 wrote: 04 Oct 2017, 03:54
grovkillen wrote: 03 Oct 2017, 08:49 Oooh, sorry about that!

We need to use a counter (check value) since the TaskValueSet will trigger the button#state over and over again.

Code: Select all

on System#Boot do
  Delay,5000 //Just to be sure...
  TaskValueSet 3,1,0 //Just to be sure...
endon

on button#state do
 if [button#state]=0
  TaskValueSet 3,1,1 //Given that a Dummy device is on TaskNr 3...
  Event EventTriggerButton1
 else
  TaskValueSet 3,1,1 //Given that a Dummy device is on TaskNr 3...
  Event EventTriggerButton0
 endif
endon

on mqtt_import#GPIO0 do
  if [mqtt_import#GPIO0]=1
    TaskValueSet 2,1,1 //Given that the switch device is on TaskNr 2...
  else
    TaskValueSet 2,1,0 //Given that the switch device is on TaskNr 2...
  endif
endon

on EventTriggerButton1 do
 if [Dummy#Value]=1
  Publish /sonoff1/cmd,GPIO,12,1
  TaskValueSet 3,1,0 //Set the check value to 0...
 endif
endon

on EventTriggerButton0 do
 if [Dummy#Value]=1
  Publish /sonoff1/cmd,GPIO,12,0
  TaskValueSet 3,1,0 //Set the check value to 0...
 endif
endon




I appreciate your persistence, but it does not work. I think I'm going to give up.
I never thought this was so complicated to do.
Please upload your device page so I can see what names and task numbers you use. It's not complicated just different to what you have done before. Learning is power, once you got this working you will understand how to do more stuff. ;)

Re: Button mqtt (espeasy) for turn ON sonoff on other place

Posted: 04 Oct 2017, 12:18
by vader
Ooops, doesn't work. Work not for switches. Only sensor values.... 8-)

Re: Button mqtt (espeasy) for turn ON sonoff on other place

Posted: 04 Oct 2017, 15:23
by r16
grovkillen wrote: 04 Oct 2017, 05:55
r16 wrote: 04 Oct 2017, 03:54
grovkillen wrote: 03 Oct 2017, 08:49 Oooh, sorry about that!

We need to use a counter (check value) since the TaskValueSet will trigger the button#state over and over again.

Code: Select all

on System#Boot do
  Delay,5000 //Just to be sure...
  TaskValueSet 3,1,0 //Just to be sure...
endon

on button#state do
 if [button#state]=0
  TaskValueSet 3,1,1 //Given that a Dummy device is on TaskNr 3...
  Event EventTriggerButton1
 else
  TaskValueSet 3,1,1 //Given that a Dummy device is on TaskNr 3...
  Event EventTriggerButton0
 endif
endon

on mqtt_import#GPIO0 do
  if [mqtt_import#GPIO0]=1
    TaskValueSet 2,1,1 //Given that the switch device is on TaskNr 2...
  else
    TaskValueSet 2,1,0 //Given that the switch device is on TaskNr 2...
  endif
endon

on EventTriggerButton1 do
 if [Dummy#Value]=1
  Publish /sonoff1/cmd,GPIO,12,1
  TaskValueSet 3,1,0 //Set the check value to 0...
 endif
endon

on EventTriggerButton0 do
 if [Dummy#Value]=1
  Publish /sonoff1/cmd,GPIO,12,0
  TaskValueSet 3,1,0 //Set the check value to 0...
 endif
endon




I appreciate your persistence, but it does not work. I think I'm going to give up.
I never thought this was so complicated to do.
Please upload your device page so I can see what names and task numbers you use. It's not complicated just different to what you have done before. Learning is power, once you got this working you will understand how to do more stuff. ;)
on my sonoff1 i have this rule:

Code: Select all

on relay#state do
 if [relay#state]=1
  GPIO,12,1
  Publish /sonoff1/RelayStatus,1
 else
  GPIO,12,0
  Publish /sonoff1/RelayStatus,0
 endif
endon
Image




on my sonoff2 (buttoN) i have this rule:

Code: Select all

on System#Boot do
  Delay,5000 //Just to be sure...
  TaskValueSet 3,1,0 //Just to be sure...
endon

on button#state do
 if [button#state]=0
  TaskValueSet 3,1,1 //Given that a Dummy device is on TaskNr 3...
  Event EventTriggerButton1
 else
  TaskValueSet 3,1,1 //Given that a Dummy device is on TaskNr 3...
  Event EventTriggerButton0
 endif
endon

on mqtt_import#GPIO0 do
  if [mqtt_import#GPIO0]=1
    TaskValueSet 2,1,1 //Given that the switch device is on TaskNr 2...
  else
    TaskValueSet 2,1,0 //Given that the switch device is on TaskNr 2...
  endif
endon

on EventTriggerButton1 do
 if [Dummy#Value]=1
  Publish /sonoff1/cmd,GPIO,12,1
  TaskValueSet 3,1,0 //Set the check value to 0...
 endif
endon

on EventTriggerButton0 do
 if [Dummy#Value]=1
  Publish /sonoff1/cmd,GPIO,12,0
  TaskValueSet 3,1,0 //Set the check value to 0...
 endif
endon


that is my config on sonoff2:


Image
Image

with this configuration..my button not Works..
and the relay status not change.

Re: Button mqtt (espeasy) for turn ON sonoff on other place

Posted: 04 Oct 2017, 18:09
by vader
Try this:

Make sonoff1 = Unit Number 1 (see Config page)
Make sonoff2 = Unit Number 2
On sonoff2 rule:

on button#state do
if [button#state]=0
SendTo 1,gpio,12,0
else
SendTo 1,gpio,12,1
endif
endon

Re: Button mqtt (espeasy) for turn ON sonoff on other place

Posted: 04 Oct 2017, 18:16
by r16
vader wrote: 04 Oct 2017, 18:09 Try this:

Make sonoff1 = Unit Number 1 (see Config page)
Make sonoff2 = Unit Number 2
On sonoff2 rule:

on button#state do
if [button#state]=0
SendTo 1,gpio,12,0
else
SendTo 1,gpio,12,1
endif
endon
I already tried.
But the problem is;
If i press button relay of sonoff1 turn on.
But if i turn off relay through Home assistant, and press again button...the relay not turn on again.

Because button dont knows the state of sonoff1.do you undestand?

Re: Button mqtt (espeasy) for turn ON sonoff on other place

Posted: 04 Oct 2017, 18:28
by vader
Ah,ok. I know that problem (had the same with Domoticz). I had to press the button twice to be in sync again with the relay on other device.

Change the rule on sonoff1 in:

on relay#state do
if [relay#state]=1
GPIO,12,1
Publish /sonoff1/RelayStatus,1
SendTo 2,inputswitchstate 0,1
else
GPIO,12,0
Publish /sonoff1/RelayStatus,0
SendTo 2,inputswitchstate 0,0
endif
endon

So it inform sonoff2 to change the button state. And I found this:

on button#state do
if [button#state]=0
--> TaskValueSet 3,1,1 //Given that a Dummy device is on TaskNr 3...
Event EventTriggerButton1
else
TaskValueSet 3,1,1 //Given that a Dummy device is on TaskNr 3...
Event EventTriggerButton0
endif
endon

Shouldn't it be 0? And task #3 does not exist!
It's all just a kind of logic puzzle... ;)

Re: Button mqtt (espeasy) for turn ON sonoff on other place

Posted: 04 Oct 2017, 21:18
by r16
vader wrote: 04 Oct 2017, 18:28 Ah,ok. I know that problem (had the same with Domoticz). I had to press the button twice to be in sync again with the relay on other device.

Change the rule on sonoff1 in:

on relay#state do
if [relay#state]=1
GPIO,12,1
Publish /sonoff1/RelayStatus,1
SendTo 2,inputswitchstate 0,1
else
GPIO,12,0
Publish /sonoff1/RelayStatus,0
SendTo 2,inputswitchstate 0,0
endif
endon

Now. WORKS LIKE A CHARM
sooooo easy, thank you @Vader and grovkillen for all persistence.
Perfect.
Now i can make this:
https://www.youtube.com/watch?v=ImVK5cGVrpQ&t=1s
:mrgreen: :mrgreen: :mrgreen: :mrgreen: :mrgreen: :mrgreen: :mrgreen:

Re: Button mqtt (espeasy) for turn ON sonoff on other place

Posted: 04 Oct 2017, 21:28
by vader
No problem. You are welcome. :D I know this, because I have all the crap already behind me.... :mrgreen:

Re: Button mqtt (espeasy) for turn ON sonoff on other place

Posted: 04 Oct 2017, 21:30
by grovkillen
Sorry for not giving you the correct rules (didn't know your setup etc.). But glad you got it to work!! :D