Sonoff Double/triple/tap

Hi,
I have just started working with the sonoff a little more in depth. My thought is to use the switch or switches on the sonoff in switchmode 3 or another mode if it makes more sense to increase a counter in HA that i can then trigger an automation based on 2 taps of a momentary switch or 3 taps similar to a switch with the central scene class.

I think i have the idea of how it will work. I will use the Counter as a trigger for the automation. I am just not sure how to get the counter to increment on the MQTT topic. the topic for switch one would be cmnd/sonoff1/POWER1. Went the momentary is tapped it sends cmnd/sonoff1/POWER1 TOGGLE. It sends that once everytime the switch is hit. I think i need to use an MQTT sensor.

The other issue is how to get the automation to not prematurly trigger. for instance if the trigger was on the counter being at 3 but I also have an automation with a trigger at 2 how do i keep the automation from firing if i in fact am going for a triple tap? Is there some way to delay so that if the trigger goes from 2 to 3 in a half second then not to fire that event but fire the triple tap event?

I just got this idea after figuring out that i could use 3 switches on one sonoff to trigger different events. The light bulb went on in my head wondering if i could do the same thing as a $50 Scene capable zwave switch with a $5 Sonoff and a couple of momentary switches.

@DrZzs I learned the additional switch with the sonoff from your videos which pushed towards trying to figure out how to make it work for a 3 way switch. I wonder if you had thought about trying this out?

What firmware are you using ?
I have a long (>1s) and short (<1s) press enabled using some scripting on the ESPEasy firmware.

I am using Tasmota. I have also gotten it wired up so that I can have 3 separate switches sending different Topics. Well the only difference is POWER1, POWER2, POWER3.

Edit:
There is also another part to this that I may have jumped the Gun on. How do I get the MQTT Topic cmnd/sonoff1/POWER1 to register as a switch in HA or a way to use it as a trigger just as single switch? I have not been able to figure this out.

I haven’t used counters or tried multiple presses.
I have used Tasmota to use the other GPIO pins to send MQTT messages to different topics for different buttons.


I had to read that page several times, slowly. Then I just got out a sonoff and tested out how it works.
I’m working on trying to explain it in a video that hopefully I’ll have done in a week or so.

Theo and the Tasmotites have thought of everything!

I to have read that page and I think this time i found were my problem might be. This statement says that Tasmota does not publish the state of the switch.

Important:
The Sonoff-Tasmota firmware does not publish the state of the switch in any way! It only send commands (directly to the corresponding relay or via MQTT to a topic).

Is there a way to have HA “respond” to these command topics to trigger events? I know its probably not going to be as easy as if it had a State topic sent who knows it maybe impossible.

I guess at this point before trying to deal with double and triple taps its probably a little easier to work on how HA can trigger an event from the Command topic.

The command topic I have setup for one switch is cmnd/sonoff1/POWER1. It is set on the sonoff as switchmode1 = 3

Hi how are you getting on with this project ?

I have the Sonoff setup the way it needs to be however I am stuck with the automation. I believe it is an issue with the template. I have another thread for this going but I have not received a reply back yet.

Counter Condition

Once I have it all figured out I plan to do a right up on it.

Let me know if you have any thoughts.

I don’t know about your counter automation proberly a bit over my head. But in case you missed it and it helps, on the tasmota github page there is a section on buttons mulity presses. (Button working same way as momentary switch)
I currently have one 1 press turns standard light on via sonoff relay 2 quick presses sends mqtt msg command/button2/power. (Having trouble picking that msg up at moment but that’s why I has here looking foe anwsers)
If I find any think that may help you ill post a link.

I had not seen that. I will check that out. Looks like it might work better than the way i was going to do it.

Thanks @cooper for sending me in this direction. I know some of this info you will not need but Im going to put it in one post so others can see.

I have successfully gotten this working. The only down fall is it looks as though this only works for the on board momentary switch. However you can still use an external momentary if you solder two wires onto the pads in the picture below.

The way I have it configured is a single tap turns the relay on on the Sonoff, Double tap sends the mqtt topic of cmnd//POWER TOGGLE and if you hold it sends the topic cmnd//POWER HOLD

image

You will then configure the Sonoff like you normally would. @DrZzs has great video of how to get the sonoff up and running with Tasmota and HA.

Once you can access the Web UI of the sonoff and have the basics setup you will need to go to the console of the sonoff and enter the following commands

  • setoption1 1

  • setoption11 1

  • buttontopic
    where is the topic you want to use

  • setoption32
    This can be set from 0.1 to 10 seconds. 5 seems to be a good setting however I do not think it equals 5 seconds.

Once you have that configured you should be able to push the button and see the different topics in the console.

Now to configure Home assistant. I am sure there might be multiple ways to do this but here is how I got it to work. A note, with the double tap and Hold there is not an on and off command from the sonoff. So once you double tap or hold the switch that you have setup for that particular command will stay on until turned on in the HA UI. I fixed this with an Automation that would set it to off after 1 second.

Set up the HA configuration File.

For the Onboard Relay/Single Tap

switch

- platform: mqtt
      name: "Sonoff Relay"
      command_topic: "cmnd/sonoff/POWER"
      state_topic: "stat/sonoff/POWER"
      qos: 1
      payload_on: "ON"
      payload_off: "OFF"
      retain: true

For the Double tap and hold. The sensor will equal the payload of either TOGGLE or HOLD depending on if you double tap or hold the switch.

Sensors

- platform: mqtt
  name: "Sonoff Double Tap Hold"
  state_topic: "cmnd/sonoff-button-tap/POWER"
  qos: 1
  expire_after: "1"

Setting up that Automations

- alias: Turn On with Double Tap
  trigger:
   - platform: state
     entity_id: sensor.sonoff_double_tap_hold
     to: 'TOGGLE'
  condition:
    condition: state
    entity_id: switch.other_light
    state: 'off'
  action:
    service: switch.turn_on
    data:
      entity_id: switch.other_light

 - alias: Turn off with Double Tap
      trigger:
       - platform: state
         entity_id: sensor.sonoff_double_tap_hold
         to: 'TOGGLE'
      condition:
        condition: state
        entity_id: switch.other_light
        state: 'on'
      action:
        service: switch.turn_off
        data:
          entity_id: switch.other_light

You would use the same automation if you want to trigger with Hold just by changing state of the sensor to HOLD instead of TOGGLE. I know that this automation for on and off can be handled with a template but I have not figured templates out yet.

I hope this all makes sense. I can attempt to answer questions about what I have but not sure if I could go more in depth.

@DrZzs Since I had asked if you had ever worked with double taps earlier in this thread I didnt know if you would want to base one of your up coming videos on this. This idea came from attempting to make a cheaper version of the HomeSeer HS-WS100+ that I have purchased that is scene capable with the double and triple taps. Since I got this working on the sonoff these can essentially be done for about $15 using the sonoff and this momentary switch I found on Amazon to replace an existing switch. http://a.co/fEj8MIJ however it can probably be done much cheaper utilizing the touch buttons and momentary buttons you have used which I would like to try too.

hi pmarq2008
nice explanation just wanted to add, when you first set up tasmota with in the ide there is a line of code that sets button 1 to sonoff button, this can be alter then that allows you to assign it to say gpio14. :slight_smile:

While your approach works my gut feel would be that the detection of the switches would be better done locally in the Sonoff to avoid any latency issues, then send a message to HA.

I wanted to create a switch to control two standard lamps in the lounge that had a manual override that would work if HA was down for whatever reason for WAF.

So Sonoff basic, plus I’m using https://github.com/KmanOz/KmanSonoff firmware with a few tweaks.

I also soldered on a header pin to the board and added a momentary button for GPIO14. In the firmware I check for GPIO14 and allow that to toggle the power but do not allow a reset to occur (another reason to use GPIO14 as opposed to just connecting to GPIO0.

Alternatively you could check for double-presses etc and then send a different MQTT message to HA.

also for double press mqtt msg, if you use mqtt sensor with a expire_after: “1” ( 1 sec) you don’t need an automation to reset the toggle.

sensor

  • platform: mqtt
    state_topic: “cmnd/button2/POWER”
    name: test button
    payload_on: “TOGGLE”
    expire_after: “1”

Cooper, Thanks I forgot about that. That will at least make it easier to solder to or use a Header. Personally I dont mind having to solder to the Switch pads but I know it can be more difficult for others.

Changing it from a switch to a Sensor actually makes more sense and is much when it comes to the automations too. I am going to edit my post to have those updates in it.

I hope this helped you with getting the message in HA.

Lol yer it did had been scratching my head over this for weeks. your msg was the missing link. All working know as intended
Thanks

You helped me out too. I have been working on the automation side to make this work for weeks also. Im glad we both were able to help each other. :slight_smile:

Hi, i had same problems as you, and found two type of approach slightly different from yours:

  1. Automation : MQTT message at button press can be used by automations (without the need to create an mqtt sensor):
- alias: 'Button_Pow_r2'
    trigger:
      - platform: mqtt  # modificata funzione tasto usando il comando ButtonTopic https://github.com/arendst/Sonoff-Tasmota/wiki/Control-other-devices
        topic: cmnd/Tasto_PowR2/POWER
        payload: "TOGGLE"
    condition: 
      - condition: state
        entity_id: switch.xiaomi_gateway_alarm
        state: 'off' 
    action:
      - service: switch.turn_on
        entity_id: switch.xiaomi_gateway_alarm

  - alias: 'Button_Pow_r2'
    trigger:
      - platform: mqtt
        topic: cmnd/Tasto_PowR2/POWER
        payload: "TOGGLE"
    condition: 
      - condition: state
        entity_id: switch.xiaomi_gateway_alarm
        state: 'on' 
    action:
      - service: switch.turn_off
        entity_id: switch.xiaomi_gateway_alarm
  1. binary_sensor: instead to read mqtt switch action directly in an automation, an MQTT Binary
    Sensor can be created and used in many ways. Here is the code (verified and working with latest
    hassio release):
  - platform: mqtt
    name: "Tasto_PowR2" # modificata funzione tasto usando il comando ButtonTopic https://github.com/arendst/Sonoff-Tasmota/wiki/Control-other-devices
    state_topic: "cmnd/Tasto_PowR2/POWER"
    payload_on: "TOGGLE"
    expire_after: 3 # Defines the number of seconds after the value expires if it’s not updated. After expiry, the value is cleared, and the availability is set to false
2 Likes