Shelly Device switch template for each click type

Tags: #<Tag:0x00007fc4108c08a8> #<Tag:0x00007fc4108c07b8>

Hi Everyone,

This is my very first contribution to this amazing community so please be indulgent if I’m doing anything wrong :slight_smile:

Shelly Switch controller :

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

I recently bought Shelly1 wifi relay & Shelly Dimmer 2, both allow to control anything you want from HA integration, and it works like a charm.
These component can be controlled from physical switches (such as Sonoff mini, and many others I guess).

I figured out Shelly Devices fire events when the physical switch is triggered and I wanted to take benefit from this feature in my HA configuration so I started to work on this blueprint in order to make this configuration easier.

So here we are, this blueprint allow you to select a Shelly Device (added via HA’s Shelly Integration) and configure any sequence of action that meet your need considering the click types declared in HA’s Shelly Integration doc

I’m pretty new into this stuff and as far as my test went, my Shelly1 only fire events for single click and long click. However I left all the defined click type in the blueprint so make sure your device fire an event for each click type you want to use (via dev tools → events → listen “shelly.click”)

If you are facing any issue, let me know, I’ll try to help out as best as I can.
Cheers !

TROUBLE SHOOTING

Please note that Shelly Devices fire events on switch trigger only if the BUTTON TYPE of the switch connected to the device is set to momentary or detached switch, according to shelly integration doc

How to make sure I can use a click type :
Once your device is configured and added to home assistant using Shelly Integration

Go to Developper Tools → Events, then enter “shelly.click” and start listening.
Now you just need to try the click type on your physical switch and see what event comes up.

Here is the blueprint :

blueprint:
  name: Shelly switch events
  description: Control anything from shelly SW
  domain: automation
  input:
    remote:
      name: Switch
      description: Button to use
      selector:
        device:
          integration: shelly
    channel:
      name: Channel
      description: Which shelly channel should trigger this automation
      default: 1
      selector:
        number:
          min: 0
          max: 20
          mode: box
    button_single:
      name: Short click
      default: []
      selector:
        action: {}
    button_double:
      name: Double click
      default: []
      selector:
        action: {}
    button_triple:
      name: Triple click
      default: []
      selector:
        action: {}
    button_long:
      name: Long click
      default: []
      selector:
        action: {}
    button_single_long:
      name: Button single long
      default: []
      selector:
        action: {}
    button_long_single:
      name: Button long single
      default: []
      selector:
        action: {}
  source_url: https://community.home-assistant.io/t/shelly-device-switch-template-for-each-click-type/296528
mode: restart
max_exceeded: silent
trigger:
- platform: event
  event_type: shelly.click
  event_data:
    device_id: !input 'remote'
    channel: !input 'channel'
action:
- variables:
    event: '{{ trigger.event.data.click_type }}'
- choose:
  - conditions:
    - '{{ event == "single" }}'
    sequence: !input 'button_single'
  - conditions:
    - '{{ event == "double" }}'
    sequence: !input 'button_double'
  - conditions:
    - '{{ event == "triple" }}'
    sequence: !input 'button_triple'
  - conditions:
    - '{{ event == "long" }}'
    sequence: !input 'button_long'
  - conditions:
    - '{{ event == "single_long" }}'
    sequence: !input 'button_single_long'
  - conditions:
    - '{{ event == "long_single" }}'
    sequence: !input 'button_long_single'
3 Likes

No valid blueprint found in the topic. Blueprint syntax blocks need to be marked as YAML or no syntax.

Hi Salopette,

I tried to import it from my HA instance and it worked fine…
I don’t really understand how to mark the block as YAML or whatever… but if someone can explain me how to do so, I’ll modify it asap

Don`t work to import

I think your definition is also missing the channel. A Shelly 2.5 can trigger the event from two different channels. A Shelly i3 even has 3 channels.

Can you tell me more about the error logs or anything ?
You are the second one telling me he struggles to import it. I’d like to help but I can’t figure out what’s wrong. It works fine when importing it from my HA instance.

You’re right

I’m just starting using Shelly Devices and I have a very simple usage at this time.

In the current version, since the trigger only check for the nature of the event “shelly.click” it should work whatever channel you use. the point is you cannot make different action regarding the channel triggered.

To be honest I think this feature would deserve another blueprint dedicated to those specific device to avoid confusion (Shelly1 user might not understand why they have actions form for channel they don’t have) unless there is a way to make a dynamic blueprint that create actions for the desired number of channel.

EDIT : after reflection, we could add a channel input into this blueprint, then you’d have to create an automation based on this blueprint for each channel.
The point is I don’t have device with multiple channels I would prefer to test this before making an update on the blueprint

Hi Kenneth,

Thanks for your reply. It drove me nut. Believe it or not, the only way I found to fix this is simply remove everything below the code in my first message :roll_eyes:

Can’t figure out why, seems there is an issue in the way HA parse topics to find blueprint, like it doesn’t stop at the end of the code blocks and keep interpreting anything that come below…

Anyway, it should work now.
Let me know ! :slight_smile:

Updated considering your comment
Hope it will be useful to someone.

1 Like

Hi @nok, tried your blueprint and got the following error message:

Any idea what I am doing wrong? Imported it and selected my shelly and set it up via the UI:

Shelly button type is set to detached and I can see him fire via developer tools.

Thanks in advance!

Hi @marothe ,
Well first, I must admit I don’t really get the way you’re using the blueprint, I don’t have this interface on my HA instance when dealing with Blueprint/automation.

Still, concerning your issue, it seems the event that trigger the automation comes with empty “click_type” data. Since you’re triggering the automation manually, it’s just normal functioning.
The automation try to parse a non-existing event (manual trigger) and result in failing the condition.

Could you please screenshot an event log in your Dev Tools->listening when you trigger the physical switch. It could help.
Also which Shelly Device are you trying to control here ?

Hi @nok, Thank you for your quick reply and for my late reply, had been tinkering with other stuff and did not do anything HA-related in the last time :slight_smile:.

First of all: this is the new “automation debug view” basically, it came with on of the last updates. You can click “view trace”, which is the middle button in the automations overview right now, it will lead you to it.

Now to the problem:
Thank you again for your great insight, you were totally right. I always thought manually triggering an automation would be the same as phsycically clicking the button - guess I was wrong. I got it working now. But if somebody else finds this problem, I also had to change the shelly to “detached” in its button type settings.

But it is working now! So thank you. But maybe an off-topic question:

  • How are you turning the light off again? I now got the automation working by using the light switch, the automation runs a script and the lights turns on. So far, so good and your blueprint does what it should do :-). But now my question: Is there a way to turn off the light with the same light switch? Probably not, right? Because just clicking the light switch turns it on. So I just created an automation to turn off the light after no motion for 15 minutes or something like that. Please find also the shelly.click event below…Not sure if that helps.

Thanks in advance!

@marothe use light.toggle instead of light.turn_on and you can probably use the same automation you have created.

hi @JohNan thanks - that did the trick! I can now just toggle it by using my light switch. I guess there is only one problem left and I don’t really know why.

If the light is off and I hit the light switch, the light turns on. But, if I want to turn it off again, I have to hit the light switch twice. So it has to go back into the same place where it came from. So you always have to turn the switch “on and off”, otherwise the light won’t turn on or off. This is kinda un-intuitive. Any idea why that is? Is this what I can see when I start listening to the “shelly.click” event? There Is one action from “shelly.click” and one from “LOCAL” which just gives out null.

Is there any way to change that?

@marothe Use both switch on and switch off as triggers and toggle as action. No matter in what state the light is in, a flip of the switch in either direction will toggle it.

@JohNan Ah, once again - that makes total sense. But Is this managable with the template or do I need to do that manually, like in YAML.

I can’t just for the life of it get it to run.

Doing it like that, does not really do anything.

Sorry for all the questions and needed help, this is my first shelly and also my first time using blueprints and the UI…

EDIT: sorry, I used (obviously, but apparently not for me at first sight…) the wrong input. But, using the correct one (the input boolean changes from on to off when I physically turn the switch) it still only works if I hit the switch twice.

Kinda lost here. Long click → binary_sensor goes to either “on” or “off” (can see that in the entity view) → toggle light. But: only if I go “on” → “off” → “on” or “off” → “on” → “off”.

What am I missing here?!

Final edit 6/3/21: Just did it “manually” via yaml, works like a charm. Guess blueprints are not for me.

Hi @marothe,
can you share your yaml solution?
I’m dealing with the same problem, having to it hit the switch twice to make it work.
thanks,
Carlos

Hi @cjpb, in the end, I abandoned this blueprint, it was just more easy to do it with the tools the Shelly integration offers:

  - id: badezimmer_oben_lights_on_with_shelly
    alias: badezimmer_oben lights on with shelly
    trigger:
      - platform: state
        entity_id: binary_sensor.badezimmer_oben_switch_input_phsyical
        from: 'off'
        to: 'on'
      - platform: state
        entity_id: binary_sensor.badezimmer_oben_switch_input_phsyical
        from: 'on'
        to: 'off'
    condition:
      - condition: state
        entity_id: light.badezimmer_oben
        state: 'off'
    action:
      - service: script.badezimmer_oben_scene_on
  - id: badezimmer_oben_lights_off_with_shelly
    alias: badezimmer_oben lights off with shelly
    trigger:
      - platform: state
        entity_id: binary_sensor.badezimmer_oben_switch_input_phsyical
        from: 'off'
        to: 'on'
      - platform: state
        entity_id: binary_sensor.badezimmer_oben_switch_input_phsyical
        from: 'on'
        to: 'off'
    condition:
      - condition: state
        entity_id: light.badezimmer_oben
        state: 'on'
    action:
      - service: light.toggle
        entity_id: light.badezimmer_oben

I just used the simple code above. If you integrated your shelly with HA, you should get a binary_sensor for the physical switch of the shelly (please ignore the typo, just saw that…) and I just used that to check the state of it and then I toggle the light. Works like a charm…

I got two of these inputs, image , but only one of them is active, not sure why there are 2 channels.

Hope this helps!