MQTT - Shelly Button1

I have made an MQTT blueprint for the Shelly Button1. For some reason the Shelly integration for me did not work with the Shelly Button1, so I had to resort to MQTT. It’s annoying to set up MQTT automations for these buttons (especially if you have more), so hopefully this blueprint will ease the process quite a lot.

The blueprint requires an ID, which can be read from the Button’s web interface under Settings → Device Info → Device ID (eg.: E8DB48B6CADB). This is necessary to subscribe to the correct MQTT topic (Shelly Button1 API documentation).

The Button1 supports single, double and triple presses, and long presses, all of which can be automated with this blueprint.

Remember: You’ll need to have your own MQTT broker set up, and both Home Assistant (MQTT Integration) and the Shelly Button1 needs to use it (Shelly Button1 can be set up to use MQTT from its web interface).

To access the Shelly Button1’s web interface, it needs to be on USB charging.

Open your Home Assistant instance and show the blueprint import dialog with a specific blueprint pre-filled.

blueprint:
  name: MQTT - Shelly Button1
  description: Trigger actions with a Shelly Button1 from MQTT. Remember to set your Shelly Button1 to MQTT mode, and point it (and Home Assistant) to your MQTT broker.
  domain: automation

  input:
    shelly_button_id:
        name: "Shelly Button1 Device ID"
        description: "The Device ID of the Shelly Button1. Can be found on the web interface of the Shelly button under 'Settings -> Device Info'. (Eg.: XXXXXXXXXXXX)"
        selector:
          text:

    single_press:
      name: Single Press
      description: The button is pressed 1 time.
      default: []
      selector:
        action:
    double_press:
      name: Double Press
      description: The button is pressed 2 times.
      default: []
      selector:
        action:
    triple_press:
      name: Triple Press
      description: The button is pressed 3 times.
      default: []
      selector:
        action:
    long_press:
      name: Long Press
      description: The button is long pressed (held down).
      default: []
      selector:
        action:

trigger_variables:
  button_id: !input shelly_button_id

## Example MQTT topic full path: shellies/shellybutton1-E8DB84A9BDBB/input_event/0
## Topic subscription using MQTT trigger limited templates: https://www.home-assistant.io/docs/automation/trigger/#mqtt-trigger 
trigger:
  - platform: mqtt
    topic: "{{'shellies/shellybutton1-' ~ button_id ~ '/input_event/0'}}"

## Alternatively, you can just ask for the full topic in the input, but then you'll need to input the MQTT topic path, instead of just the device ID:
# trigger:
#   - platform: mqtt
#     topic: !input shelly_button_id
#     # topic: !input input_mqtt_topic

condition: []

action:

  choose:
    - conditions:
        - condition: template
          value_template: '{{ trigger.payload_json.event == "S" }}'
      sequence: !input single_press
    - conditions:
        - condition: template
          value_template: '{{ trigger.payload_json.event == "SS" }}'
      sequence: !input double_press
    - conditions:
        - condition: template
          value_template: '{{ trigger.payload_json.event == "SSS" }}'
      sequence: !input triple_press
    - conditions:
        - condition: template
          value_template: '{{ trigger.payload_json.event == "L" }}'
      sequence: !input long_press
3 Likes

Thanks. I can control shelly button 1 now.

Thanx, I like this very much! I came to some difficulties though. I’m using it for switching some lights. One of the actions includes switching a light on, a 5 minuten wait, then switching it off again. While this is “waiting” it is not possible to trigger other actions, as the waiting period blocks the automation.
I helped myself by using two of these. Then it works like a charm

Glad to hear it’s useful for others too :slight_smile:

However, I might have a small tip for the blocking wait actions. I was also struggling with it, until I found this piece of the documentation: https://www.home-assistant.io/integrations/script/#waiting-for-script-to-complete
Basically it says if you call a script with script.turn_on instead of script.myscript directly, then the script becomes asynchronous and won’t block further execution of the parent script, while in the latter, it will become synchronous, and the wait command will block the entire flow.

I moved the “light turn on → wait 5 minutes → light turn off” workflow into its own separate script, and then on button press just call the script.turn_on command (and target the script), and below that call I can do whatever else I want, without waiting 5 minutes to pass. Then I didn’t need to have two of this blueprint triggered.

Hope I could help! :slight_smile:

1 Like

Thanks! I totally forgot about scripts. Now it is working as intended… seems like I should buy more buttons…

I also want to use a shelly button but can’t get it working.
I imported the blueprint.
I took the ID out of shelly and entered it. Then started an automation.
Just a single press and then switch a lamp.
When I save the automation I don’t get any errors.
When I press, nothing happens.
If I listen in mqtt with ‘shellies/#’ and press then I get the following :

Bericht 39 ontvangen op shellies/shellybutton1-98CDAC2DADF1/input_event/0 om 21:13:
{ "event": "S", "event_cnt": 77 }
QoS: 0 - Retain: false

Bericht 38 ontvangen op shellies/shellybutton1-98CDAC2DADF1/sensor/act_reasons om 21:13:
[ "ext_power" ]
QoS: 0 - Retain: false

Bericht 37 ontvangen op shellies/shellybutton1-98CDAC2DADF1/sensor/error om 21:13:
0
QoS: 0 - Retain: false

Bericht 36 ontvangen op shellies/shellybutton1-98CDAC2DADF1/sensor/charger om 21:13:
true
QoS: 0 - Retain: false

Bericht 35 ontvangen op shellies/shellybutton1-98CDAC2DADF1/sensor/battery om 21:13:
100
QoS: 0 - Retain: false

Where would I have missed something in the build-up?

I know this is difficult for you but maybe with the right questions I can get there.

If your having problems setting up shelly buttons have a look at this: Setting up Shelly buttons with HA Docker It was written for a docker instance but a lot of the stuff (firewall rules, how to catch the shelly.click events etc) is likely relevant to those running home assistant in some other set up.

Thank you,
I’ll study this.

I don’t work with Docker. Do I also need to set up that firewall (6)?
If it does, the command does not work.
So I have continued to do so, but listening does not yield anything.

The firewall will depend on what Linux version your running it on. Different firewalls have different commands, and you’ll need to find the command for your operating system.

Hi
I solved it by following the video of ‘BeardedTinker’
Adding Shelly Humidity and Temperature sensor to Home Assistant - YouTube
Not only the temperature gauge works but also my button.
I wanted to give you feedback
I also want to thank BeardedTinker for the interesting videos

@Vogete - Thank you! I’ve tried many things to try to get a Shelly Button 1 working with MQTT with no success until I found your Blueprint. It worked perfectly and flawlessly.

Late to the party, but maybe this might help some new newbies like me in the future.

Instead of using this blueprint, you can just use a simple trigger:

platform: mqtt
topic: shellies/shellybutton1-xxxxxxxxxxxx/input_event/0
payload: SS
value_template: "{{ value_json.event }}"

where xxxxxxxxxxxx is your Shelly button ID, and payload can be either S, SS, SSS or L.

1 Like

Thank for this blueprint. Ive trying to get my button to work for over a week and this did the trick.

I have a problem implementing MQTT entities with shelly button: when the broker restarts, the trigger is fired. I suppose shelly tried to mitigate this adding an event counter, wich is difficult to manage.

This happen because the event does not refrect the button status, and it does not reset when the button is released.
Than, when the ha or broker restarts, the payload is interpreted as a new event.