Zigbee2MQTT - Sonoff Wireless Switch (SNZB-01)

This is the blueprint for the Sonoff Zigbee Wireless Switch (SNZB-01):

Supported actions:

  1. Single press
  2. Double press
  3. Long press (4 seconds)
blueprint:
  name: Zigbee2MQTT - Sonoff SNZB-01 Zigbee Wireless Switch
  description: Automate your Sonoff SNZB-01 Zigbee Wireless Switch via Zigbee2MQTT.
  domain: automation
  input:
    switch:
      name: Sonoff SNZB-01 Zigbee Wireless Switch
      description: Sonoff SNZB-01 Zigbee Wireless Switch to use
      selector:
        entity:
          integration: mqtt
          domain: sensor
    single_press:
      name: Single Press
      description: Action to run on single press
      default: []
      selector:
        action: {}
    double_press:
      name: Double Press
      description: Action to run on double press
      default: []
      selector:
        action: {}
    long_press:
      name: Long Press
      description: Action to run on long press (4 seconds)
      default: []
      selector:
        action: {}
  source_url: https://github.com/AramidX/ha-blueprint/blob/dc4b846bc4dfa8725526606b07c051947bfc9687/blueprints/sonoff/sonoff_snzb01_wireless_switch_z2m.yaml
mode: restart
max_exceeded: silent
trigger:
- platform: state
  entity_id: !input "switch"
  attribute: action
action:
- variables:
    command: "{{ trigger.to_state.state }}"
- choose:
  - conditions: 
    - "{{ command == 'single' }}"
    sequence: !input "single_press"
  - conditions: 
    - "{{ command == 'double' }}"
    sequence: !input "double_press"
  - conditions: 
    - "{{ command == 'long' }}"
    sequence: !input "long_press"
7 Likes

Hi, thank you for sharing this, I really appreciate it as I was able to get my Sonoff to work. The only problem is … when I press the Sonoff button and turn on the light, the log reports (every time) this error.

Can you kindly tell me how to solve this problem? Thank you very much, Andrea from Italy

2 Likes

This is due to Zigbee2MQTT. A fix is on the way as discussed here: Add default on all HA templates for attributes by mdegat01 · Pull Request #6988 · Koenkk/zigbee2mqtt · GitHub

Thanks for this, but I can’t seem to get it to work. I’m able to pair my three SNZB-01’s in zigbee2mqtt, but they don’t seem to do anything when pressed (with or without an automation based on this blueprint). I tried watching MQTT and also opening the HA Developer pane and listening for events ("*"), but I don’t see anything from the buttons. Is there anything else I need to do to set them up?

1 Like

Maybe try to pair them again. Do you see them in the Zigbee2MQTT frontend ?

yes, they definitely appear in the interface, and I can rename them. I’ve tried removing and re-pairing them a few times.

I had previously set them up under ZHA, then I transitioned everything to zigbee2mqtt. Could there be some trouble from having them added to one network, then resetting them to add to another? It seems like it should be ok because they do pair successfully. All the other devices seem the have made the switch just fine.

Is there any particular log info that would be helpful? I don’t see any log data in z2m at all when I click the button. is there a way to see if anything comes in over the zigbee signal at all?

Same error for me… when i tap on the button i get

Error: UndefinedError: 'dict object' has no attribute 'to_state'

I have a following problem with blueprint above.

I set an automation for long press with “delay”. It works fine.

Issue I encounter is that button sometimes sends extra action “none”. It cancels my running automation and bypasses triggers from blueprint. Did anyone encounter this problem?

I solved trashing this blueprint to recicle bin and use node-red with zigbee2mqtt monitoring the mqtt topics with payload single, double, long string.
Maybe this blueprint uses some old deprecated methods, no more working.

For anyone still trying to get one of these working in an automation, this seems to be working for me:
Trigger type: MQTT
Topic: zigbee2mqtt/WB1/action (replace WB1 with whatever you named your button)
Payload: single

See screenshot below

Z2M WB1

yes i think it’s the same… but you need to duplicate the trigger and topic string for each automation

This i my Node-Red configurations nodes:

[{"id":"e7f79e0a.203dc","type":"mqtt in","z":"a7b5f465.f2f968","name":"","topic":"zigbee2mqtt/SNZB-01/action","qos":"0","datatype":"auto","broker":"3f412b25.7d88ec","nl":false,"rap":true,"rh":0,"x":190,"y":260,"wires":[["7292c542.0de184"]]},{"id":"7292c542.0de184","type":"switch","z":"a7b5f465.f2f968","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"single","vt":"str"},{"t":"eq","v":"double","vt":"str"},{"t":"eq","v":"long","vt":"str"}],"checkall":"false","repair":false,"outputs":3,"x":410,"y":260,"wires":[["4fa691b5.225a28"],["d1826391.7b5468"],["42ea7350.848e2c"]]},{"id":"4fa691b5.225a28","type":"api-call-service","z":"a7b5f465.f2f968","name":"toggle lumetto","server":"e5fa04da.26da88","version":3,"debugenabled":false,"service_domain":"script","service":"turn_on","entityId":"script.toggle_lumetto_studio","data":"","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":600,"y":240,"wires":[[]]},{"id":"d1826391.7b5468","type":"api-call-service","z":"a7b5f465.f2f968","name":"lumetto night","server":"e5fa04da.26da88","version":3,"debugenabled":false,"service_domain":"script","service":"turn_on","entityId":"script.night_mode_lumetto_studio","data":"","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":590,"y":300,"wires":[[]]},{"id":"42ea7350.848e2c","type":"api-call-service","z":"a7b5f465.f2f968","name":"lumetto full white","server":"e5fa04da.26da88","version":3,"debugenabled":false,"service_domain":"script","service":"turn_on","entityId":"script.full_white_lumetto_studio","data":"","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":610,"y":360,"wires":[[]]},{"id":"78658af9.fd4954","type":"comment","z":"a7b5f465.f2f968","name":"Bridge Zigbeee button sonoff SNZB-01 to HA scripts","info":"Bridge Zigbeee button sonoff SNZB-01 to HA scripts","x":260,"y":200,"wires":[]},{"id":"3f412b25.7d88ec","type":"mqtt-broker","name":"home.linda","broker":"home.linda","port":"1883","clientid":"","usetls":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"sessionExpiry":""},{"id":"e5fa04da.26da88","type":"server","name":"Linda HA","version":1,"legacy":false,"addon":false,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|True|on|home|open","connectionDelay":true,"cacheJson":true}]
3 Likes

@Stringer i love this blueprint. I would like to use it to open and close a cover. But just with a single click.
That would mean a toggle function. I am not keen enough to change the blueprint myself.
Would it be possible for you to adjust this blueprint to add a toggle function?
First press: the cover opens and later a first press, the cover closes.

I know, i could use the single press for open and the double press for close, but that has a lesser WAF.

So I’m finally getting back to my zigbee wireless switches after changing to the SONOFF Zigbee 3.0 USB Dongle Plus and moving from ZHA to zigbee2mqtt, since I was having the disconnection and battery drain issues that others were expecting…

However, this blueprint doesn’t work for me. I’m new enough to HA that I don’t understand a lot of this, but was able to get a bare automation working, so I know things can work, but I don’t see anything hit the automation trace. Any assistance would be helpful. I’m not sure how to troubleshoot.

(as evidenced by the post below I’m super new to HA so I could be entirely off base here)

If you’ve got a way to determine the state of the cover (open vs closed) you should be able to put some conditions in there to open it if it’s closed and vice versa. Alternatively, if you needed to track that, helpers seem like the way to go. You should be able to do that without needing to touch the blueprint. Hopefully, that helps.

Hi, I have installed this blueprint, but, although my device is enabled, I cannot select it in the blueprint device. Is there something I must do with mqtt??

I got this device info:

Zigbee info
IEEE: 00:12:4b:00:24:ce:5e:16
Nwk: 0xc426
Device Type: EndDevice
LQI: 27
RSSI: Unknown
Last Seen: 2022-05-28T20:20:34
Power Source: Battery or Unknown
Quirk: zhaquirks.sonoff.button.SonoffButton

Instead of the button turning on the switch, have it turn on a toggle helper (input_boolean). Then another automation that watches for the input boolean state and changes the switch to match.
Open your Home Assistant instance and show your helper entities.

My suggestion to ‘fix’ the null triggering problem is to add a trigger condition that only allows valid triggers thru to the actions. Z2M sends these null triggers out as part of the way it is designed, so screening them is important. For this one a condition block after the trigger would make your users very happy…

condition:
  condition: template
  value_template: '{{ trigger.to_state.attributes.action in ( ''single'',''double'',''long'') }}'

That will clean up everyone’s log and the only place you see them is if you look at the traces, there will be triggers that happen but fail the condition and die.

Due to requests, I wrote the condition for you…
I hope the author adds this to the actual blueprint, I cannot. However if users manually edit their copy of the blueprint code adding the condition after th trigger, it should only run the blueprint code if one of these things happen, Other event will trigger the blueprint but fai the condition and no nothing. If you stil cannot see traces in the default 5 allotted to fix your stuff, just add to the automation that calls the script a line to store more traces…

condition:
  condition: template
  value_template: '{{ trigger.to_state.attributes.action in ( ''single'',''double'',''triple'',''quadruple'',''hold'',''release'',''shake'',''single_left'',''double_left'',''triple_left'',''hold_left'',''release_left'',''single_right'',''double_right'',''triple_right'',''hold_right'',''release_right'',''single_center'',''double_center'',''triple_center'',''hold_center'',''release_center'',''single_both'',''double_both'',''triple_both'',''hold_both'',''release_both'') }}'

3 Likes

@Sir_Goodenough

Would you please explain how to add this for us newbs? Greatly appreciated

I believe this condition block as is added immediately after the trigger block should work.
I have not tested it with this blueprint, but it works in principal in several of my Z2M blueprints.

The actual problem that few people realize it that if you write a single automation and look for a specific trigger, this works fine without null triggers.

In the case of a blueprint like this, the author screens for 3 triggers, but there are actually 4 valid triggers. Every time it is triggered as a short, double, or long, after a time it triggers again as a null trigger to reset for the next short, double, or long. Null is a valid trigger and happens every time. Therefore you have to account for it or the null trigger gets dumped into the blueprint with nowhere to go and becomes a log error.

What the condition does is allows thru only the 3 valid things the blueprint is looking for, and when the null one shows up, it just fails the condition and goes away quietly. The only place it then shows is as a mostly blank trace.

If there are too many null triggers for you to troubleshoot a problem, you can increase the number of traces for this automation, which in the case of my Z2M cube and other switches I do.

Links about stored_traces—>

1 Like