Switch input in Toggle mode (non-pushbutton)

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
cybermaus
Normal user
Posts: 16
Joined: 24 Nov 2016, 11:15

Switch input in Toggle mode (non-pushbutton)

#1 Post by cybermaus » 04 Nov 2018, 10:37

Apologies if asked before. I did search, but new to this Sonoff and ESP Easy stuff, so maybe not with the right terminology.
It seems such an obvious function, I am thinking I am completely overlooking the setting.

Got a Shelly1 working with last nights build, seems to work from a hardware point of view
Switch on GPIO-5 is configured as "Normal Switch" (because it is not a springloaded pushbutton)
Relay on GPIO-4 is configured as "Domoticz MQTT Helper"

How do I make the switch work as toggle, so it can inter-work with other activities from for example Domoticz?
In other words, the relay should not follow the button state 1:1, but any button state change should toggle the relay state.

So right now, I have:
- Light is off; Switch is in position 1
- Toggle switch (so to pos 2) light goes on
- Toggle switch (so to pos 1) light goes off
- Toggle switch (so to pos 2) light goes on
- Use Domoticz to toggle light, it goes off
- Toggle switch (so to pos 1) light stays off, nothing happens <=unwanted
- Toggle switch again (so to pos 2), this time the light goes on
I have to hit the switch twice!


What Shelly1 native firmware (and Tasmota) would do is:
- Light is off; Switch is in position 1
- Toggle switch (so to pos 2) light goes on
- Toggle switch (so to pos 1) light goes off
- Toggle switch (so to pos 2) light goes on
- Use Domoticz to toggle light, it goes off
- Toggle switch (so to pos 1) light goes on (it toggles relay based on relay state, not based on switch state)


Many thanks for your help

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

Re: Switch input in Toggle mode (non-pushbutton)

#2 Post by grovkillen » 04 Nov 2018, 15:08

Code: Select all

On button#switch Do
 If [button#switch]=0
  event,ToggleSwitch=1
 Else
  event,ToggleSwitch=0
 EndIf
EndOn

On ToggleSwitch Do
  gpio,4,%eventvalue%
EndOn

On SwitchOff Do
 event,ToggleSwitch=0
EndOn

On SwitchOn Do
 event,ToggleSwitch=1
EndOn
You can then send commands as switch on or off but the button will always toggle.
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:

cybermaus
Normal user
Posts: 16
Joined: 24 Nov 2016, 11:15

Re: Switch input in Toggle mode (non-pushbutton)

#3 Post by cybermaus » 04 Nov 2018, 17:28

Ok, thanks

I did in fact already suspected I could achieve this in Rules. In fact, the existence of rules is why I am trying ESP Easy instead of Tasmota.
But that is for some more complex switches I would like. Not for a simple light switch.

Still, it is a good rule example, as I am learning this may be useful.

Is there a deeper architectural reason why the ESP Easy switch does not have this simply toggle option, or simply a matter of no-one getting around to it yet?
I guess it does all take a lot of time. I noticed some people wanted to go Pro on this, I definitely will donate a few bucks, as I will likely be using this for my "special" cases, even if I stick to other firmware for simple on-of switches/

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

Re: Switch input in Toggle mode (non-pushbutton)

#4 Post by grovkillen » 04 Nov 2018, 18:56

We have started the donation pages already (see link below). And yes, a simple non rule toggle is in the scope of the future but not implemented yet.
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:

cybermaus
Normal user
Posts: 16
Joined: 24 Nov 2016, 11:15

Re: Switch input in Toggle mode (non-pushbutton)

#5 Post by cybermaus » 05 Nov 2018, 20:47

I am a $1 patreon now. Not much, I know, but the growing number of patreon based project means I have to cut the pie small.

So yesterday I played a lot with your script and made a lot of variants, including variables (dummy device)
I learned the following about ESP Easy, which means its concept is a little different then what I expected.
Not that it means it is bad, I still think I may want to use it for more advanced stuff, but not for simple light switches.

- There is no actual relay device.
- The switch device cannot actually control anything, you cannot link by definition it to another device, instead you have to either script something, or send out a MQTT to a controller.
- If I make a simple switch using the example using a "MQTT Helper", the "switch" sends a MQTT to my Domoticz, and it sends back a MQTT to the "Helper" and it switches.
- That explains the somewhat sluggish response time
- Also if the network is down, I would not be able to switch on the light.
- Ergo, one would always have to make a rule script, even for something simple like a light switch.
- Though thus you have to writing to GPIO directly, you cannot read its value, so need to make a parallel "variable" to remember state.
- Variable is quoted, because they do not exist either, instead you have to use a dummy device.

In the end, I had it working by not having a MQTT helper device at all, instead I had a dummy device as variable to remember the relay state,
and the switch device ran the rule-script to toggle both the GPIO directly as well as Publish the MQTT to Domoticz.
It meant very fast switch-to-light-on responses again, and was working when network down. But as stated, I think I will reserve ESP Easy for the more complex IoT.

Do take the above with some light-heartedness. I realise I am not an expert after 2 evenings of play.
Maybe I could have done it smarter or better.
But this if you want ESP easy to grow, these would be the hurdles for new users like me that you may want to consider to make simpler.


Off to test the Catalan firmware next....

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

Re: Switch input in Toggle mode (non-pushbutton)

#6 Post by grovkillen » 05 Nov 2018, 20:55

I will try to help you more tomorrow but short, you should not call GPIO commands from anywhere else than rules. If you do you make the unit misbehave. You can use toggle commands for it to toggle 1-->0 or 0-->1. But it's always good to use dummy's to keep track of states.

That being said. We know we're not straightforward as some other firmware, this is because we're a generic firmware whereas most others target specific hardware (Sonoff as an example). We still make some hardware configured out of the box but our goal is for anyone to build anything. That means that we don't know how the user want to combine hardware and how they should interact with each other. Thus the rules where created. But I think you're idea of connecting switches to GPIOs is a valid point. But not as straightforward as you might first think. We don't know the GPIO that should be combined and should one button control only one or multiple relays? Should the button trigger something else than just the relay? Etc etc.

And thanks for the support. $1 patron is awesome. With more people doing it we're reaching our goal soon :D
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:

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

Re: Switch input in Toggle mode (non-pushbutton)

#7 Post by leel1967l » 06 Nov 2018, 23:47

Hi @cybermaus,
which version of the firmware are you using?

cybermaus
Normal user
Posts: 16
Joined: 24 Nov 2016, 11:15

Re: Switch input in Toggle mode (non-pushbutton)

#8 Post by cybermaus » 09 Nov 2018, 09:31

I did this using the nightly build from one of the days of last weekend. (weekend of 2018-11-03)

I want to repeat my assessment that I do suspect ESP Easy is the most powerful of the 3 firmwares. Just that the fact it cannot do the "simple" stuff easily will hold it back from adoption by novice users. Not necessarily a bad thing maybe.

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

Re: Switch input in Toggle mode (non-pushbutton)

#9 Post by leel1967l » 09 Nov 2018, 09:39

Hi,
With the latest versions you can accomplish your task with a sinlge line of code using new commands:

Code: Select all

On button#switch Do
  gpiotoggle,4
EndOn
We are trying to make the usage as simple as possible and as flexible at possible.

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 14 guests