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

Handling complex click events on Shelly1, Shelly2.5, Shelly dimmer and Shelly dimmer2 with fw 1.8.x and above #438

Open
wants to merge 10 commits into
base: master
Choose a base branch
from

Conversation

g-kiss
Copy link

@g-kiss g-kiss commented Sep 12, 2020

Overwiev

I created a new event to handle complex events on fw 1.8.x and above. I tested it on Shelly1, Shelly2.5, Shelly dimmer and Shelly dimmer2. With a new "momentary_button" yaml parameter defines which button is connected to Shelly.
If the clicking speed is slow or fast or very fast this event works very well (its accurate is 100%).

These events allows to:

  • control more devices from switch of the Shelly1 or Shelly2.5
  • control more devices from switch of the dimmer
  • control a dimmer from switch of the Shelly1 or Shelly 2.5

See the examples below.

This PR needs the (StyraHem/pyShelly#42) PR too.

1. Momentary button

1.1 If a momentary button is attached to Shelly, then the momentary_button parameter in yaml must be set to true.
click_events contains a complex event, like this:

{
    "event_type": "shelly_click",
    "data": {
        "entity_id": "binary_sensor.lampa_haloszoba_mennyezet_switch",
        "click_events": "SSL"
    },
    "origin": "LOCAL",
    "time_fired": "2020-09-10T09:16:34.063499+00:00",
    "context": {
        "id": "52343a3df34611eab4b431423721197c",
        "parent_id": null,
        "user_id": null
    }
}

This event will be send after last release of button.

1.2 In case of long push, it also sends another event, when the long push starting (LSTART) or stopping (LSTOP), like this:

{
    "event_type": "shelly_click",
    "data": {
        "entity_id": "binary_sensor.lampa_haloszoba_mennyezet_switch",
        "click_events": "SSLSTART"
    },
    "origin": "LOCAL",
    "time_fired": "2020-09-10T09:24:55.476679+00:00",
    "context": {
        "id": "7d11ce25f34711eab2829b2412e9ff51",
        "parent_id": null,
        "user_id": null
    }
}

"LSTART" and "LSTOP" can be used when the long push is the last action. With "LSTART" event can start executing the action before releasing the long push.

1.3 The time diagram of events
kép

2. Toggle button

If a toggle button is attached to Shelly, then the momentary_button parameter in yaml must be false (this is the default) and in shelly settings (web interfacae) the long push time must be set to 10 ms. In this case the click_count contains number of clicks, like this:

{
    "event_type": "shelly_click",
    "data": {
        "entity_id": "binary_sensor.lampa_barni_agy_melletti_switch",
        "click_count": 3
    },
    "origin": "LOCAL",
    "time_fired": "2020-09-10T09:39:51.135123+00:00",
    "context": {
        "id": "92ec6fd3f34911ea9ed6f77f27f2ff32",
        "parent_id": null,
        "user_id": null
    }
}

3. Examples

3.1 Shelly1 with momentary, detached button:

  • Toggle ceiling lamp (this is the main function of the button):
  trigger:
    platform: event
    event_type: shelly_click
    event_data:
      entity_id: binary_sensor.lampa_haloszoba_mennyezet_switch
      click_events: "S"
  action:
    - service: light.toggle
      entity_id: light.lampa_haloszoba_mennyezet    
  • Toggle night lamp:
  trigger:
    platform: event
    event_type: shelly_click
    event_data:
      entity_id: binary_sensor.lampa_haloszoba_mennyezet_switch
      click_events: "SS"
  action:
    - service: light.toggle
      entity_id: light.lampa_haloszoba_agy_melletti_1
  • Turn off all lights of room:
  trigger:
    platform: event
    event_type: shelly_click
    event_data:
      entity_id: binary_sensor.lampa_haloszoba_mennyezet_switch
      click_events: "LSTART"
  action:
    - service: light.turn_off
      entity_id: light.lampa_haloszoba_mennyezet    
    - service: light.turn_off
      entity_id: light.lampa_haloszoba_agy_melletti_1    
    - service: light.turn_off
      entity_id: light.lampa_haloszoba_agy_melletti_2    
  • Close the shutter:
  trigger:
    platform: event
    event_type: shelly_click
    event_data:
      entity_id: binary_sensor.lampa_haloszoba_mennyezet_switch
      click_events: "SLSTART"
  action:
    - service: cover.close_cover
      entity_id: cover.haloszoba_redony
  • Open the shutter:
  trigger:
    platform: event
    event_type: shelly_click
    event_data:
      entity_id: binary_sensor.lampa_haloszoba_mennyezet_switch
      click_events: "SSLSTART"
  action:
    - service: cover.open_cover
      entity_id: cover.haloszoba_redony

3.2 Shelly dimmmer with momentary, detached button:

  • Toggle lamp:
  trigger:
    platform: event
    event_type: shelly_click
    event_data:
      entity_id: binary_sensor.lampa_lepcso_switch
      click_events: "S"
  action:
    - service: light.toggle
      entity_id: light.lampa_lepcso
  • Dim up and down:
  trigger:
    platform: event
    event_type: shelly_click
    event_data:
      entity_id: binary_sensor.lampa_lepcso_switch
  condition:
    condition: template
    value_template: "{{ trigger.event.data['click_events'] | replace('L', '') == 'START' }}"
  action:
    - service: light.turn_on
      entity_id: light.lampa_lepcso
      data:
        effect: dim_up_down
  • Dim stop:
  trigger:
    platform: event
    event_type: shelly_click
    event_data:
      entity_id: binary_sensor.lampa_lepcso_switch
  condition:
    condition: template
    value_template: "{{ trigger.event.data['click_events'] | replace('L', '') == 'STOP' }}"
  action:
    - service: light.turn_on
      entity_id: light.lampa_lepcso
      data:
        effect: dim_stop
  • Toggle another lamp with the dimmer switch:
  trigger:
    platform: event
    event_type: shelly_click
    event_data:
      entity_id: binary_sensor.lampa_lepcso_switch
      click_events: "SS"
  action:
    - service: light.toggle
      entity_id: light.lampa_eloszoba

3.3 Control Shelly dimmmer with a switch of Shelly1

  • Toggle lamp of Shelly1 (this is the main function of the button):
  trigger:
    platform: event
    event_type: shelly_click
    event_data:
      entity_id: binary_sensor.lampa_etkezo_spot_switch
      click_events: "S"
  action:
    - service: light.toggle
      entity_id: light.lampa_etkezo_spot
  • Toggle the dimmer's lamp:
  trigger:
    platform: event
    event_type: shelly_click
    event_data:
      entity_id: binary_sensor.lampa_etkezo_spot_switch
      click_events: "SS"
  action:
    - service: light.toggle
      entity_id: light.lampa_lepcso
  • Dim up and down:
  trigger:
    platform: event
    event_type: shelly_click
    event_data:
      entity_id: binary_sensor.lampa_etkezo_spot_switch
  condition:
    condition: template
    value_template: "{{ trigger.event.data['click_events'] | replace('L', '') == 'START' }}"
  action:
    - service: light.turn_on
      entity_id: light.lampa_lepcso
      data:
        effect: dim_up_down
  • Dim stop:
  trigger:
    platform: event
    event_type: shelly_click
    event_data:
      entity_id: binary_sensor.lampa_etkezo_spot_switch
  condition:
    condition: template
    value_template: "{{ trigger.event.data['click_events'] | replace('L', '') == 'STOP' }}"
  action:
    - service: light.turn_on
      entity_id: light.lampa_lepcso
      data:
        effect: dim_stop

@gerard33
Copy link
Contributor

Tried this on a Shelly dimmer (first model, firmware 1.8) with a momentary switch, but I get no shelly_click events in HA when pressing either of the 2 buttons. Anything I can add in the code to make this work?

@thecem
Copy link

thecem commented Sep 18, 2020

@gerard33 Yes, RTFM ;-) -> Sensors in https://github.com/StyraHem/ShellyForHASS#sensor-global-and-per-device

Or add

shelly:
  sensors:
    - all #show all sensors
    #or
    - switch #show only switch actions

I searched also a lot ;-), happy to help...

@gerard33
Copy link
Contributor

@thecem
I meant if I can add anything in the .py code as addition to this PR as it doesn't work for Shelly dimmers 😉

My config contains the part you are mentioning

shelly:
  sensors:
    - switch

The shellyforhass.click event is showing data for my Shelly dimmer, but always as a single press.
And this PR should make double press etc. possible again.

@thecem
Copy link

thecem commented Sep 18, 2020

Oh this would be nice!

Do you receive only the shellyforhass.click or also the shelly_switch_click, in HA 0.115.0 I´m not able to search with wildcard like *shelly*.
So I only receive for shelly 2.5 on double click a message for cnt with shelly_switch_click and the one shellyforhass.click

@g-kiss
Copy link
Author

g-kiss commented Sep 18, 2020

I tested it with HA 115.0, Shelly Dimmer and momentary_button: true in yaml. It works. I get these events:

{
    "event_type": "shelly_click",
    "data": {
        "entity_id": "binary_sensor.lampa_furdoszoba_mennyezet_switch",
        "click_count": 0,
        "click_events": "S"
    },
    "origin": "LOCAL",
    "time_fired": "2020-09-18T08:51:53.561663+00:00",
    "context": {
        "id": "330f9255f98c11eab1ac3320c994fda4",
        "parent_id": null,
        "user_id": null
    }
}
{
    "event_type": "shelly_click",
    "data": {
        "entity_id": "binary_sensor.lampa_furdoszoba_mennyezet_switch",
        "click_count": 0,
        "click_events": "L"
    },
    "origin": "LOCAL",
    "time_fired": "2020-09-18T08:51:52.304406+00:00",
    "context": {
        "id": "324fbb07f98c11ea8f43cb6a68433bdb",
        "parent_id": null,
        "user_id": null
    }
}

@thecem
Copy link

thecem commented Sep 18, 2020

Undocumented feature: momentary_button: true ?

I get also the info without this config in HA 115 but I have exactly to search for shelly_switch_click and shellyforhass.click, but not a shelly_click, maybe there should be a detailed explanation...

@g-kiss
Copy link
Author

g-kiss commented Sep 18, 2020

@gerard33, @thecem
This PR needs the (StyraHem/pyShelly#42) PR too.

@gerard33
Copy link
Contributor

Ah that makes sense 👍

@gerard33
Copy link
Contributor

@g-kiss
I have added your pyShelly PR and tested it with my Shelly dimmer on fw1.8 and I get the click_count.
I have set the following in configuration_schema.py as I have only momentary switches with the Shelly devices ->
vol.Optional(CONF_MOMENTARY_BUTTON, default=True): cv.boolean so would have expected the click_events.

But for me this is working fine like this as well. Will do some more testing, but it seems really reliable and get's up to 5-clicks 👍

Below an example for a triple click.

{
    "event_type": "shelly_click",
    "data": {
        "entity_id": "binary_sensor.shelly_kantoor_2_switch",
        "click_count": 6,
        "click_events": ""
    },
    "origin": "LOCAL",
    "time_fired": "2020-09-18T20:29:23.558574+00:00",
    "context": {
        "id": "a39a5039f9ed11ea8286f382ca21a762",
        "parent_id": null,
        "user_id": null
    }
}

@g-kiss
Copy link
Author

g-kiss commented Sep 18, 2020

@gerard33
My yaml is like this:

  devices:
    - id: "xxxxxxxx"
      name: Fürdőszoba mennyezet
      entity_id: lampa_furdoszoba_mennyezet
      light_switch: true
      momentary_button: true

and gives click_events:

{
    "event_type": "shelly_click",
    "data": {
        "entity_id": "binary_sensor.lampa_furdoszoba_mennyezet_switch",
        "click_count": 0,
        "click_events": "LS"
    },
    "origin": "LOCAL",
    "time_fired": "2020-09-18T20:56:48.610026+00:00",
    "context": {
        "id": "782140e9f9f111ea98aa79d2c3fe8fd7",
        "parent_id": null,
        "user_id": null
    }
}

@thecem
Copy link

thecem commented Sep 19, 2020

Is it possible to set the variable global in yaml?
I have only momentary switches.

@gerard33
Copy link
Contributor

@g-kiss
I have added momentary_button: true for the device I am testing with and then it shows S etc. indeed.

I didn't have that device_id in the devices: list and apparently it needs to be in or otherwise it wont get the momentary_button value, even not if it's set to default = true (changed that locally).

@g-kiss
Copy link
Author

g-kiss commented Sep 29, 2020

@gerard33
The button type (momentary or toggle) is the property of device. It must be assigned to device, because for example I have about 10 Shellys with momentary button and about 15 Shellys with toggle button.

@danstr1
Copy link

danstr1 commented Nov 8, 2020

Hi, there is any estimation date when it will be merged to the master?

Thanks in advance

@g-kiss g-kiss changed the title Handling complex click events on Shelly1 and Shelly2.5 with fw 1.8.x Handling complex click events on Shelly1 and Shelly2.5 with fw 1.8.x and above Dec 4, 2020
@g-kiss g-kiss changed the title Handling complex click events on Shelly1 and Shelly2.5 with fw 1.8.x and above Handling complex click events on Shelly1, Shelly2.5, Shelly dimmer and Shelly dimmer2 with fw 1.8.x and above Dec 14, 2020
@jacobhallgren
Copy link

@gerard33
My yaml is like this:

  devices:
    - id: "xxxxxxxx"
      name: Fürdőszoba mennyezet
      entity_id: lampa_furdoszoba_mennyezet
      light_switch: true
      momentary_button: true

and gives click_events:

{
    "event_type": "shelly_click",
    "data": {
        "entity_id": "binary_sensor.lampa_furdoszoba_mennyezet_switch",
        "click_count": 0,
        "click_events": "LS"
    },
    "origin": "LOCAL",
    "time_fired": "2020-09-18T20:56:48.610026+00:00",
    "context": {
        "id": "782140e9f9f111ea98aa79d2c3fe8fd7",
        "parent_id": null,
        "user_id": null
    }
}

Where can I look for the events when they are fired? In some log file?

@g-kiss
Copy link
Author

g-kiss commented Dec 17, 2020

@jacobhallgren
You can see it on "developer tools" / "events" page of HA web interface.

@jacobhallgren
Copy link

@hakana Please add this to master

@libussa
Copy link

libussa commented Dec 22, 2020

are single, long and double clicks already supported? Seeing this now https://github.com/StyraHem/ShellyForHASS#events

@g-kiss
Copy link
Author

g-kiss commented Mar 2, 2021

@hakana Do you have any plan with this PR?

@hakana
Copy link
Contributor

hakana commented Mar 3, 2021

I have tried this but it is not stable when testing. So I need to investigate more around this.

@jacobhallgren
Copy link

I have tried this but it is not stable when testing. So I need to investigate more around this.

Works great for me, use it every day for 2 months. No problems.

@gerard33
Copy link
Contributor

gerard33 commented Mar 6, 2021

For what it's worth, it's working fine for me for several months as well. Double and triple clicks for all Shelly devices easily available now.

@wauswaus
Copy link

Hi @hakana, did you had a chance of looking into this a bit more? I would love to use this.

@g-kiss, are you planning on adding this functionality to the Shelly Home Assistant native integration as well? I can imagine more people would really like to use it.

@g-kiss
Copy link
Author

g-kiss commented Aug 25, 2021

Hi @wauswaus, I will thinking about it

@vipial1
Copy link

vipial1 commented Nov 17, 2021

Would be awesome to have this on master!

@vipial1
Copy link

vipial1 commented Nov 17, 2021

@g-kiss I would like to be part of the testing...how can I install your change?

@wauswaus
Copy link

Hi @wauswaus, I will thinking about it

What's the outcome? I'm running the native integration nowadays . Would be nice to have your nice work in there as well

@g-kiss
Copy link
Author

g-kiss commented Dec 22, 2021

Hi @wauswaus, see this PR: home-assistant/core#58436

@gerard33
Copy link
Contributor

gerard33 commented Feb 22, 2022

@g-kiss Have migrated to the native HA integration since a couple of weeks and added your code and that works like a charm! Thanks for sharing it 👍🏻
Hopefully it will be added to the native HA integration or library soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants