Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Button multipress #200

Closed
ionciubotaru opened this issue Mar 14, 2017 · 42 comments
Closed

Button multipress #200

ionciubotaru opened this issue Mar 14, 2017 · 42 comments
Labels
enhancement Type - Enhancement that will be worked on

Comments

@ionciubotaru
Copy link

When the button is press one time the relay toggle and a MQTT message is published
When the button is pressed two times sonoff do the same. (Except Sonoff Dual)

Sugestion:

    if (multipress != 2 or sysCfg.module == SONOFF_DUAL) {
      switch (state) {
      case 0: { // Off
        power &= (0xFF ^ mask);
        break; }
      case 1: // On
        power |= mask;
        break;
      case 2: // Toggle
        power ^= mask;
      }
      setRelay(power);
    } else {
//  publish a nice mqtt message
    }

Can you add this in the future release ?
I use this code to control a second less accesibile Sonoff from a Sonoff Touch.

@arendst
Copy link
Owner

arendst commented Mar 14, 2017

When the button is pressed once AND buttontopic is set it will execute a MQTT command. In that case a
button press twice will execute the command that otherwise was executed when button was pressed once.

See the cryptic message in wiki - Button usage (https://github.com/arendst/Sonoff-Tasmota/wiki/Button-usage).

@arendst arendst added the question Type - Asking for Information label Mar 14, 2017
@cruisinger
Copy link

I also would appreciate ionciubotaru's suggestion. I've test several times the buttontopic/switchtopic/switchmode command's, but I don't get it. I'm not able figuring out what the parameters are doing. I'm also looking for a possibility, to switch a fan in the bathroom, by tapping the sonoff touch twice.
For me, unfortunately, the commands mentioned are to confusing. I don't know what's the difference between switch and button(-topic), and also the parameters toggle/follow.

But Theo, Tasmota is the most fascinating development for ESP's at the moment. Thank you very much for your project!

@ionciubotaru
Copy link
Author

Yes, Tasmota is the best software for Sonoff. Looking inside the code I'm impressed of Theo's programming skills..

I set buttontopic to 'second_sonoff' and when the button is pressed once sonoff send the mqtt message:
cmnd/second_sonoff/POWER TOGGLE
When the button is pressed twice sonoff send the status mqtt message and toggle the relay.

Is it possible to swap ?

  • when the button is pressed once to toggle the relay
  • when the button is pressed twice to send the mqtt message to buttontopic

@davidelang
Copy link
Collaborator

you have run across a bug that's due to the special casing of the first button. The "correct" behavior afte you have set buttontopic is to not change any local relays, only to send the mqtt messages

@ionciubotaru
Copy link
Author

For all Sonoffs, except Touch this behavior is ok, but for Touch is better to turn on the light on the first press - is hard to teach all family members to toggle light with double click, when for many year they did it with a single one :)

@davidelang
Copy link
Collaborator

the fixed code would not toggle the relays at all with switchtopic set, it would send one message if you single-tap and a different message if you doubletap

The idea is that once you set the switchtopic, it should no longer control the relays directly, only send messages

@ionciubotaru
Copy link
Author

It's OK if everything works fine (wireless + MQTT), but if something is wrong nobody can turn on the light and this will be frustrating in a smart house (bad thinks always happened - Murphy's law)

@davidelang
Copy link
Collaborator

well, if you don't want the button to not turn on the local relays, why are you setting switchtopic? that's what it does.

@ionciubotaru
Copy link
Author

I'm sorry davidelang but switchtopic has no effect on double click
Only ButtonTopic has effect, but in a reverse order:

Excuse me for insitence, please tell me how to do:

  • when the button is pressed once to toggle the relay
  • when the button is pressed twice to send the mqtt message to buttontopic

If this is not possible it's ok, i will change the code manually

@davidelang
Copy link
Collaborator

davidelang commented Mar 17, 2017 via email

@davidelang davidelang added bug Type - Confirmated Bug and removed question Type - Asking for Information bug Type - Confirmated Bug labels Mar 19, 2017
@davidelang
Copy link
Collaborator

Ok, after re-reading this umpteen times I think I understand what @arendst was saying way up above.

double tapping button0 will do one of two things.

If you are on a sonoff dual, it will switch the second relay

If you are on anything else, it will switch the first relay (as long as it's not in the middle of connecting to a mqtt server), this provides a back-door to switch the first relay if you are disconnected from the network and have buttontopic set.

At this point, I think it would be less confusing to have the double-tap do nothing on devices other than the dual, or always act as if button2 was pressed (no matter what type). But I suppose legacy consistency applies here.

@cruisinger
Copy link

I also got this issue like you. At the moment, it is only a workaroud to archieve a double tap function, and it's really confusing. Furthermore I would appreciate a more easier way to control other things by tapping twice a sonoff touch.

@davidelang
Copy link
Collaborator

davidelang commented Mar 19, 2017 via email

@arendst
Copy link
Owner

arendst commented Mar 19, 2017

@davidelang if I make that change switching won't work anymore when no MQTT server can be reached. That is what you explained just above correctly. That was also the reason of having the double tap available.

I do not see a relation between a sonoff touch double tapping and a sonoff dual.

Changing it's functionality makes sonoffs useless when there is no MQTT server connection.

I need more convincing...

@davidelang
Copy link
Collaborator

davidelang commented Mar 19, 2017 via email

@joshuagti
Copy link

I'd like to do something similar with the Sonoff Touch.

For example, it is on the bedroom wall controlling the ceiling light. I have two bedside lights each with basic Sonoffs inside. A single press to the Touch would turn ceiling light on but two short presses would send out an MQTT message to my OpenHAB which would then turn on the lights.

From what I am reading, it isn't possible in the current build but it should be possible to implement?

@ionciubotaru
Copy link
Author

Please take a look to the first item in this issue.
You can modify the code and Sonoff Touch will work as you wish.

@cruisinger
Copy link

Hi ion,
do you have a short explanation where to apply your code snippet? I which file/row?
This would be great to get in to it...

Thanks!

@ionciubotaru
Copy link
Author

In sonoff.ino search for the block inside if and replace it with the new one.

@ionciubotaru ionciubotaru reopened this Mar 23, 2017
@davidelang davidelang added the enhancement Type - Enhancement that will be worked on label Apr 23, 2017
@terba
Copy link

terba commented Jun 2, 2017

I would also use double tapping on the Touch to control other things (through MQTT). So it would be good to have this as an option.

@arendst
Copy link
Owner

arendst commented Jun 5, 2017

By trying to get this issue solved I need to know what you want by keeping in mind the original functionality.

| Situation | Buttontopic | Single Press                  | Double Press                   | Press Hold                     | Description                          |
|-----------|-------------|-------------------------------|--------------------------------|--------------------------------|--------------------------------------|
| Current   | 0           | Toggle relay                  | Ditto                          | Reset 1                        | Ditto used for Dual relay 2          |
|           |             |                               |                                |                                |                                      |
|           | = topic     | Send MQTT msg:                | Toggle relay                   | Reset 1                        | Double press backup for MQTT failure |
|           |             | cmnd/<topic>/POWER ON|OFF     |                                |                                |                                      |
|           | <> topic    | Send MQTT msg:                | Toggle relay                   | Reset 1                        | Double press backup for MQTT failure |
|           |             | cmnd/<non-topic>/POWER TOGGLE |                                |                                |                                      |
|-----------|-------------|-------------------------------|--------------------------------|--------------------------------|--------------------------------------|
| Wish 1    | 0           | Toggle relay                  | Ditto                          | Reset 1                        | Ditto used for Dual relay 2          |
|           |             |                               |                                |                                |                                      |
|           | = topic     | Toggle relay                  | Send MQTT msg:                 | Reset 1                        | Single press for MQTT failure        |
|           |             |                               | cmnd/<topic>/POWER ON|OFF      |                                |                                      |
|           | <> topic    | Toggle relay                  | Send MQTT msg:                 | Reset 1                        | Single press for MQTT failure        |
|           |             |                               | cmnd/<non-topic>/POWER TOGGLE  |                                |                                      |
|-----------|-------------|-------------------------------|--------------------------------|--------------------------------|--------------------------------------|
| Wish 2    | 0           | Toggle relay                  | Ditto                          | Reset 1                        | Ditto used for Dual relay 2          |
|           |             |                               |                                |                                |                                      |
|           | = topic     | Toggle relay                  | Send MQTT msg:                 | Reset 1                        | Single press for MQTT failure        |
|           |             |                               | cmnd/<topic>/POWER ON|OFF      |                                |                                      |
|           | <> topic    | Send MQTT msg:                | Send MQTT msg:                 | Send MQTT msg:                 | No local relay control and reset 1   |
|           |             | cmnd/<non-topic>/POWER TOGGLE | cmnd/<non-topic>/POWER2 TOGGLE | cmnd/<non-topic>/POWER3 TOGGLE |                                      |
|-----------|-------------|-------------------------------|--------------------------------|--------------------------------|--------------------------------------|

I could make situation Wish 1 a selectable option if that solves all your problems although I expect an answer where someone wants to send a MQTT message when pressed once and another message when pressed twice bypassing any local relay control...

EDIT: Add Wish 2 as option for Wish 1.

Awaiting your input.

@terba
Copy link

terba commented Jun 5, 2017

For me Wish 1 makes what I would like to do now.

But for a long-term solution I think it would be better to invest into a more generic solution where any combination could be configured. I think it would be easier to maintain and maybe is less LOC. There is also an issue for a Long Press Event (#489), which could be integrated as well (I would use that also). And there is SwitchMode too, which I was not able to use last time I tried. It is and gets even more complicated if you try to implement all wishes with the current model.

I think it would be more versatile and easier to understand if it could be configured somehow like this:

PressAction = <Nothing | RelayToggle <N> | arbitrary MQTT topic + data (with template variable for ON/OFF)>
ReleaseAction = <Nothing | RelayToggle <N> | arbitrary MQTT topic + data (with template variable for ON/OFF)>
DoubleClickAction = <Nothing | RelayToggle <N> | arbitrary MQTT topic + data (with template variable for ON/OFF)>
LongPressAction = <Nothing | RelayToggle <N> | arbitrary MQTT topic + data (with template variable for ON/OFF)>

Where ReleaseAction is if no immediate press follows (ie this is the SingleClickAction but can be used in Press-Release scenarios like a doorbell button).

With these 4 settings all the combinations could be achieved: default operation, MQTT failsafe mode, SwitchMode, SwitchTopic and ButtonTopic things, all the wishes and the multi-relay models too.

For example for Wish 1 I could configure this:

PressAction = Nothing (0)
ReleaseAction = RelayToggle 1 (1)
DoubleClickAction = "/somewhere/something/dothis asdasd"
LongPressAction = Nothing (0)

Default on a Dual:

PressAction = Nothing (0)
ReleaseAction = RelayToggle 1 (1)
DoubleClickAction = RelayToggle 2 (2)
LongPressAction = Nothing (0)

Or a momentary switch mode:

PressAction = RelayToggle 1
ReleaseAction = RelayToggle 1
DoubleClickAction = Nothing (0)
LongPressAction = Nothing (0)

But one can connect other switches to the GPIOs so it can be made even more generic... :)

arendst added a commit that referenced this issue Jun 6, 2017
5.1.6 20170606
* Shrink code
* Removed online configuration of Domoticz In and Domoticz Out MQTT
strings
* Removed commands DomoticzInTopic and DomoticzOutTopic
* Add define KEY_HOLD_TIME to configure button hold threshold before
sending MQTT Hold message
* Add command StateText4 to configure button MQTT Hold text (=
MQTT_CMND_HOLD)
* Add command SetOption11 0|1 to swap pushbutton single and double press
functionality (#200)
* Add command SwitchMode<x> 5 (PUSHBUTTONHOLD) and 6
(PUSHBUTTONHOLD_INV) (#489)
@terba
Copy link

terba commented Jun 12, 2017

@arendst: Yes, I know it is about the "internal" push button not the external ones, but I tried to combine the features of the two to make it even more standardized and easily understandable and programmable way. But never mind, it was just a quick idea, may be worth considering before a major release in the future.

I flashed the new release on all my sonoffs, and configured single, double click and hold on some of them and it works like a charm. Thank you very much!

The only problem I saw is that the default 4 seconds for hold is a lot. I flashed them with 2 seconds initially, but it is also a bit too much. So I was searching for the MQTT command to alter this value, but I didn't found a command for that. Checked in the source, I saw it is hardcoded (defined). Not a big problem, I will reflash them later, but I think this should be configurable.

@arendst
Copy link
Owner

arendst commented Jun 12, 2017

While writing the code I think: How long will it take that someone wants to be able to change the hold timeout online instead of using the default of 4 seconds. I guessed it has always been there as being 4 seconds so a define would have been a great improvement...

Any way, in the next release there will be a command to change its value from 0.1 to 20 seconds ;-)

@TRIROG
Copy link

TRIROG commented Jun 15, 2017

How can i enable this on Touch?
i'm looking for a way to use Touch as a sort of shutters controller. long press would trigger opening or closing of shutters

@terba
Copy link

terba commented Jun 16, 2017

@TRIROG: Use the table above to find the required settings. I assume you are using MQTT. Configure your automation software to listen to "cmnd//POWER" topic with a payload "HOLD". This is sent in case of long press.

@TRIROG
Copy link

TRIROG commented Jun 16, 2017

I figured that much, but i can't seem to find the correct way to do it.
Would you care to tell me which command i should change?

@terba
Copy link

terba commented Jun 16, 2017

@TRIROG: Something like that:

Topic livingroom_ceiling
ButtonTopic 1
SetOption11 1
SetOption1 1

This is the second variation in the last line of the table:

  • Single click: toggles the relay (and sends a status message)
  • Double click: send a command to toggle the relay (and sends a status message after the relay is toggled): cmnd/livingroom_ceiling/POWER ON|OFF
  • Long press: sends a command message to the same topic but with a payload "HOLD": cmnd/livingroom_ceiling/POWER HOLD

arendst added a commit that referenced this issue Jun 16, 2017
5.1.7 20170616
* Prep removal of SetOptions alternatives
* Restore webpage upgrade error messages removed in 5.1.5
* Add hold button functionality to buttons 2 to 4
* Add command SetOption32 1..100 to set Key Hold Time from 0.1 seconds
to 10 seconds (#200)
* Allow slashes in Topic, GroupTopic, ButtonTopic and SwitchTopic (#507)
* Changed webpage form actions from post to get and use relative path
url (#434, #522)
@TRIROG
Copy link

TRIROG commented Jun 17, 2017

Is there any way to give feedback to user on Sonoff Touch that the HOLD event was triggered? Maybe blink a led or something?

@PeggyFree
Copy link

Hello,

Thanks for all your work @arendst ! Can you tell me how you coded the multipress and longpress in domoticz mqtt messages? I would like to suggest simulating a selector switch using levels, just like the xiaomi button works :

10 = single
20 = double
30 = hold
40 = release

This would give a MQTT message like this (this is an example, I need to check tonight the right syntax)

command:switchlight
idx:145
switchcmd:Set Level 40

vicfergar pushed a commit to vicfergar/Sonoff-Tasmota that referenced this issue Jul 17, 2017
3.1.13 20170107
* Fix web console command input when SUB_PREFIX contains '/' (arendst#152)
* Add command response to web command (arendst#200)
* Add option to disable MQTT as define USE_MQTT in user_config.h (arendst#200)
vicfergar pushed a commit to vicfergar/Sonoff-Tasmota that referenced this issue Jul 17, 2017
3.1.14 20170107
* Add support for command TOGGLE (define MQTT_CMND_TOGGLE) when
ButtonTopic is in use and not equal to Topic (arendst#207)
* Fix web console command input when SUB_PREFIX contains '/' (arendst#152)
* Add command response to web command (arendst#200)
* Add option to disable MQTT as define USE_MQTT in user_config.h (arendst#200)
@davidelang
Copy link
Collaborator

davidelang commented Aug 31, 2017 via email

@ionciubotaru
Copy link
Author

SetOption11 Show current button single and double press swap state

@RobertSzabo
Copy link

Hi,

Can the button configuration options (table) work with T1 with more than 1 switches, i.e., configuring both options and topics individually for 2 or 3 switches?

Say, with a double tap (or long press), I would like to have a delayed toggle; is it possible to configure?

Thanks,
/r

@davidelang
Copy link
Collaborator

davidelang commented Nov 13, 2017 via email

@ksya
Copy link

ksya commented Dec 13, 2017

I have a T1 3ch and have it configured like this:

ButtonTopic othertopic
SetOption11 1
SetOption1 1

Now, I get "ON/OFF" when I both press and doublepress the button. If I set SetOption11 to 0, I get for both single and doublepress "TOGGLE". This is not expected. What can be an affecting option?

I am using version 5.10

@Aa26300
Copy link

Aa26300 commented Feb 15, 2018

Hello,

I'm just starting to use the sonoff.
And I find great what you do
I loaded the last version on sonoff basic.
I would like if it is possible to use a sonoff basic from the button to order a second (or more) sonoff basic.
After I want to connect a switch to the first Sonoff, a ligth to the second switch and use the first sonoff to remote the second.
I have done several tests but not conclusive for the moment
I don't know if I was very clear

Thank

@pmarq2008
Copy link

Can this be used on one of the GPIO's set as a button? I have tried it but it doesnt seem to work.

@cooper2751
Copy link

hello could some one answer me what i hope is a simple question. in the table for button multi presses. lets take the last 3 row option 1 and 11 are both set to on, button topic is set to what i require it to be called (button2) but how do you change between the different results, ie if i double press i get cmnd/button2/power = toggle and i would like cmnd/button2/power =on/off .
its the only bit of that table i can get my head around..
many thanks

@mattpurnell
Copy link

What does "reset 1" mean, within the actions table, under "Press Hold"?

@Alestrix
Copy link

Alestrix commented Jun 18, 2018

Is it possible to distinguish between different kinds of button presses of the 433 MHz button of the Sonoff RF? This way I could control a second Sonoff (Basic) from the 433 MHz button.

EDIT: Ok, (kinda) disregard this - HOLD works with the RF button, but double press does not. Would be nice if this could be implemented.

curzon01 pushed a commit to curzon01/Tasmota that referenced this issue Sep 6, 2018
5.1.6 20170606
* Shrink code
* Removed online configuration of Domoticz In and Domoticz Out MQTT
strings
* Removed commands DomoticzInTopic and DomoticzOutTopic
* Add define KEY_HOLD_TIME to configure button hold threshold before
sending MQTT Hold message
* Add command StateText4 to configure button MQTT Hold text (=
MQTT_CMND_HOLD)
* Add command SetOption11 0|1 to swap pushbutton single and double press
functionality (arendst#200)
* Add command SwitchMode<x> 5 (PUSHBUTTONHOLD) and 6
(PUSHBUTTONHOLD_INV) (arendst#489)
curzon01 pushed a commit to curzon01/Tasmota that referenced this issue Sep 6, 2018
5.1.7 20170616
* Prep removal of SetOptions alternatives
* Restore webpage upgrade error messages removed in 5.1.5
* Add hold button functionality to buttons 2 to 4
* Add command SetOption32 1..100 to set Key Hold Time from 0.1 seconds
to 10 seconds (arendst#200)
* Allow slashes in Topic, GroupTopic, ButtonTopic and SwitchTopic (arendst#507)
* Changed webpage form actions from post to get and use relative path
url (arendst#434, arendst#522)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Type - Enhancement that will be worked on
Projects
None yet
Development

No branches or pull requests