Two way switch for light

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
Modestas
New user
Posts: 4
Joined: 18 Jan 2020, 13:26

Two way switch for light

#1 Post by Modestas » 18 Jan 2020, 20:34

Hi,
In the past I made light control project with wemos d1 mini (ESP8266), relay and act. But after two years something happening with wemos board ant I need to change it. So I decided use ESP EASY instead Arduino IDE for new wemos board.
In the proper operating system I can control light with physical switch on the wall and with Home assistant that means that I can turn On and off light how I want.
Image


For the beginner it's not so easy make Rules configuration on ESP Easy, so I need help for it. I want to make this if else logic ( it is taken from the system that preceded ).

Code: Select all

if((GPIO12 == 1 && GPIO14 == 0)||(GPIO12 == 0 && GPIO14 == 1)){
  if ( (msgString == "1")) {
    digitalWrite(GPIO12,LOW);
  } 
    if ( msgString == "0") {
    digitalWrite(GPIO12,HIGH); 
  }
}
else{
    if ( (msgString == "1")) {
    digitalWrite(GPIO12,HIGH);
  } 
    if ( msgString == "0") {
    digitalWrite(GPIO12,LOW); 
  }
  }
}
msgString is massages from Home assistant MQTT server ( ON =1, Off=0). Please help me how to make rules on ESP Easy with same logic like was on Arduino IDE.

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

Re: Two way switch for light

#2 Post by grovkillen » 18 Jan 2020, 22:18

In you case you could simplify like this perhaps:

Code: Select all

On System#Boot Do
  Monitor,GPIO,12
  Monitor,GPIO,14
EndOn

On GPIO#12 Do
  Let,12,%eventvalue%
  Let,1,[INT#12]*[INT#14]
EndOn

On GPIO#14 Do
  Let,14,%eventvalue%
  Let,1,[INT#12]*[INT#14]
Endon

On msg Do
  Let,2,%eventvalue%
  Event,ToggleSwitch
EndOn

On ToggleSwitch Do
  If [INT#1]=0 and [INT#2]=0
   GPIO,12,1
  Else
   GPIO,12,0
   EndIf
EndOn
From HA you send the http request to:
http://ipadress/cmd?event,msg=1
Or
http://ipadress/cmd?event,msg=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:

Modestas
New user
Posts: 4
Joined: 18 Jan 2020, 13:26

Re: Two way switch for light

#3 Post by Modestas » 19 Jan 2020, 21:17

Thank you for your help. I have tried this code, but unfortunately works wrong ( maybe because I used gpio 14 with Inversed Logic) . For example when system is starting:
  • relay state is 0 wall switch is 0 and Feedback is 0
  • then I send msg 1 than ralay = 1 , wall switch = 0 and Feedback = 1, Home assistant get signal from Feedback and I can see that light is turn ON
  • My girlfriend furn off light with Wall switch, so relay = 1, wall switch =1, Feedback = 0
  • further i see in HA that light is turn off ( feedback =0) and I decide turn on light from HA and send msg = 1, and nothing is happen, because relay still on state 1
Actually I don't familiar with rules and I don't understand it, I just copy and paste, maybe you can make a comment little bit maybe I need just change something

Modestas
New user
Posts: 4
Joined: 18 Jan 2020, 13:26

Re: Two way switch for light

#4 Post by Modestas » 22 Jan 2020, 19:17

grovkillen wrote: 18 Jan 2020, 22:18 From HA you send the http request to:
http://ipadress/cmd?event,msg=1
Or
http://ipadress/cmd?event,msg=0
It was wrong structure of the request . It should looks like this :
http://ipadress/control?cmd=event,msg=1
http://ipadress/control?cmd=event,msg=0

I don't know, maybe you not understand my if/else function and how it should works, but your advice was completely wrong
grovkillen wrote: 18 Jan 2020, 22:18 On System#Boot Do
Monitor,GPIO,12
Monitor,GPIO,14
EndOn

On GPIO#12 Do
Let,12,%eventvalue%
Let,1,[INT#12]*[INT#14]
EndOn

On GPIO#14 Do
Let,14,%eventvalue%
Let,1,[INT#12]*[INT#14]
Endon

On msg Do
Let,2,%eventvalue%
Event,ToggleSwitch
EndOn

On ToggleSwitch Do
If [INT#1]=0 and [INT#2]=0
GPIO,12,1
Else
GPIO,12,0
EndIf
EndOn
I found solution by myself approximately in two day :)

Code: Select all

On System#Boot Do
  Monitor,GPIO,12
  Monitor,GPIO,14
EndOn

On GPIO#12 Do
  Let,1,%eventvalue%
EndOn

On ESP_Easy_205#State Do
  Let,3,%eventvalue%
 	If [INT#3]=1 
  		Event,TurnOn
	Else
		Event,TurnOff
	EndIf
EndOn

On TurnOn Do
  If [INT#1] != [Virtuve_Light_State#State] 
   GPIO,12,0
  Else
   GPIO,12,1
  EndIf
EndOn

On TurnOff Do
	If [INT#1] != [Virtuve_Light_State#State] 
		GPIO,12,1
	Else
		GPIO,12,0
	EndIf
EndOn

Configuration looks like this.
Image

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

Re: Two way switch for light

#5 Post by grovkillen » 22 Jan 2020, 19:23

Yes it was completely wrong but got you doing the digging yourself. Good on you! ;)

And to my defence, I wrote that rule out of memory, on my phone.
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:

Modestas
New user
Posts: 4
Joined: 18 Jan 2020, 13:26

Re: Two way switch for light

#6 Post by Modestas » 23 Jan 2020, 17:56

Yes you are right :) You showed me the road, thank you for that.

deon.dup
Normal user
Posts: 10
Joined: 08 Sep 2019, 08:58
Location: South Africa

Re: Two way switch for light

#7 Post by deon.dup » 18 May 2020, 17:01

Hi there

I found your post and code very interesting, and want to try it on a Sonoff Mini. I'm however not getting the Rule right, and my first problem starts with the MQTT. Can you perhaps please show me your config on the MQTT Import? I'm using NR, and can send any MQTT Topic, but it needs to be correct for the Rule to work. Also, on your circuit, is your GPIO-14 connected to the physical switch and GPIO-12 to the relay? I have GPIO-4 and GPIO-12, so should be easy to change the rule.

Post Reply

Who is online

Users browsing this forum: Semrush [Bot] and 1 guest