Automation: Run script on 2nd button click

HI Everyone!

I 'm trying to configure an automation to run different actions on different button clicks.

  • I have a script that works great if triggered from the UI;
  • I have a Shelly Button 1 device.

After configuring the following automation:

alias: AUTOMATION_SCRIPT_OFFICE_WORK_LIGHTS_TOGGLE
description: Run Script to toggle office lights on button 2x press
trigger:
  - platform: numeric_state
    entity_id: binary_sensor.shelly_for_ha_shbtn_1_483fda6fe510_switch
    attribute: click_count
    above: '1'
    below: '3'
    value_template: '2'
condition: []
action:
  - service: script.script_office_work_lights
    data: {}
mode: queued
max: 3

The script only runs the first time. No matter what I do, how long I wait, it will not work again, unless I edit the automation and save it.

Doesn’t make any sense to me… Appreciate your help.

Thank you in advance.

What is the mode of ‘script.script_office_work_lights’? If it’s default (mode: single), you can’t call it again until it is completed. All future attempts will be ignored (and logged). If you expect 3 instances to happen, make it mode: parallel

Also, what does that script do? Does it take a long time for it to run?

Hey @jocnnor thanks for replying.

The script runs in 1 second, and both the script and automation are set to Queue.

If I run the script by clicking the execute button in HA it runs every time. Basically it toggles my office lights on and off… no big deal I think.

I just dont understand why it only runs once through the automation!!

alias: SCRIPT_OFFICE_WORK_LIGHTS_TOGGLE
sequence:
  - choose:
      - conditions:
          - condition: device
            type: is_off
            device_id: 3c4b7f17cf7cf264264a4396018e6d35
            entity_id: light.shelly_for_ha_shblb_1_3cc353
            domain: light
          - condition: and
            conditions:
              - condition: device
                type: is_on
                device_id: 6d03d20d217f8e7f68b8fc756f6f9bf1
                entity_id: switch.shelly_for_ha_shsw_1_98f4abf28273
                domain: switch
        sequence:
          - type: turn_off
            device_id: 3c4b7f17cf7cf264264a4396018e6d35
            entity_id: light.shelly_for_ha_shblb_1_3cc353
            domain: light
          - type: turn_off
            device_id: 6d03d20d217f8e7f68b8fc756f6f9bf1
            entity_id: switch.shelly_for_ha_shsw_1_98f4abf28273
            domain: switch
      - conditions:
          - condition: device
            type: is_on
            device_id: 3c4b7f17cf7cf264264a4396018e6d35
            entity_id: light.shelly_for_ha_shblb_1_3cc353
            domain: light
          - condition: and
            conditions:
              - condition: device
                type: is_off
                device_id: 6d03d20d217f8e7f68b8fc756f6f9bf1
                entity_id: switch.shelly_for_ha_shsw_1_98f4abf28273
                domain: switch
        sequence:
          - type: turn_off
            device_id: 3c4b7f17cf7cf264264a4396018e6d35
            entity_id: light.shelly_for_ha_shblb_1_3cc353
            domain: light
          - type: turn_off
            device_id: 6d03d20d217f8e7f68b8fc756f6f9bf1
            entity_id: switch.shelly_for_ha_shsw_1_98f4abf28273
            domain: switch
      - conditions:
          - condition: device
            type: is_on
            device_id: 3c4b7f17cf7cf264264a4396018e6d35
            entity_id: light.shelly_for_ha_shblb_1_3cc353
            domain: light
          - condition: and
            conditions:
              - condition: device
                type: is_on
                device_id: 6d03d20d217f8e7f68b8fc756f6f9bf1
                entity_id: switch.shelly_for_ha_shsw_1_98f4abf28273
                domain: switch
        sequence:
          - type: turn_off
            device_id: 3c4b7f17cf7cf264264a4396018e6d35
            entity_id: light.shelly_for_ha_shblb_1_3cc353
            domain: light
          - type: turn_off
            device_id: 6d03d20d217f8e7f68b8fc756f6f9bf1
            entity_id: switch.shelly_for_ha_shsw_1_98f4abf28273
            domain: switch
      - conditions:
          - condition: device
            type: is_off
            device_id: 3c4b7f17cf7cf264264a4396018e6d35
            entity_id: light.shelly_for_ha_shblb_1_3cc353
            domain: light
          - condition: and
            conditions:
              - condition: device
                type: is_off
                device_id: 6d03d20d217f8e7f68b8fc756f6f9bf1
                entity_id: switch.shelly_for_ha_shsw_1_98f4abf28273
                domain: switch
        sequence:
          - type: turn_on
            device_id: 3c4b7f17cf7cf264264a4396018e6d35
            entity_id: light.shelly_for_ha_shblb_1_3cc353
            domain: light
          - type: turn_on
            device_id: 6d03d20d217f8e7f68b8fc756f6f9bf1
            entity_id: switch.shelly_for_ha_shsw_1_98f4abf28273
            domain: switch
    default: []
mode: queued
max: 3

Could it be the button ? is this the correct way to get the clicks from it ?

Thanks

Well, there are 2 ways to run a script.

  1. Use the script service directly
    - service: script.my_script
      data:
        parameter_1: "Ho ho ho"
  1. Turn on the script
    - service: script.turn_on
      entity_id: script.my_script

There is a subtle difference between the 2 (other than script.turn_on not allowing you to pass data). The 2nd one runs the script and returns immediately. The 1st one will wait for the entire script to complete before continuing.

If you have some rogue light or something that wont reply, this poor script is just going to wait forever to finish. It might even halt on errors for all I know. Either way, the 2nd one doesn’t care. It just says “good luck” and goes to the next step.

So, change your action to this:

action:
  - service: script.turn_on
    entity_id: script.script_office_work_lights

And it might work better for you.

1 Like

I don’t see the need for a numeric_state trigger here.
Your value_template will always be 2, so don’t know what that’s doing in there.

Try a normal state trigger:

trigger:
 - platform: state
   entity_id: binary_sensor.shelly_for_ha_shbtn_1_483fda6fe510_switch
   attribute: click_count
   to: 2

I don’t know how the shelly integration works, try double-clicking the button and see if the click_count is always 2 afterwards

Did as you suggested… same behavior, runs on the first doubleclick, but afterwards, never again!

No no, @fedot has the right answer here.

Your numeric_state trigger will only happen once. After that, it will never not be 2. Thus, will never cross the bounds.

numeric_state triggers will only fire if the previous state was outside of the bounds. Thus, the first double click it probably goes from 0 to 2. Now it’s stuck at 2 forever and will never fire again.

Hey @fedot

I tried has you suggested, made no difference.

One thing I noticed, the click count never resets when I look at the entity switch details… the click type varies, depending if it is a single, double or triple click.

Shelly type Shelly Button 1
Shelly ID 483FDA6FE510
Ip address 192.168.3.17
Protocols CoAP-discovery, poll, CoAP-msg
Uptime 47 h
Cloud status connected
Click type double
Click count 93

Shouldn’t this value be reseted automatically ?

Either that or the shelly integration counts the total clicks.
In any case it’d be best to set a simple action, i.e. toggling a light and check if the trigger or the action is causing trouble.

The attribute shouldn’t matter. I assume the state turns off since it’s a binary_sensor?

trigger:
  platform: state
  entity_id: binary_sensor.shelly_for_ha_shbtn_1_483fda6fe510_switch
  to: 'on'
condition:
  - "{{ is_state_attr(trigger.entity_id, 'click_count', 2) }}"

I assume if you single click it, the attribute changes to 1?

cant make it work: Message malformed: required key not provided @ data[‘trigger’][0][‘platform’]

Is there a place I can see the sintax ?

Looking at the latest reply it seems like the integration is counting the total amount of clicks :crazy_face:
Well in that case…
Let’s hope the click type changes state whenever clicked :confused:

trigger:
- platform: state
  entity_id: binary_sensor.shelly_for_ha_shbtn_1_483fda6fe510_switch
  attribute: click_type
  to: double

ok… so it works if I enter a single click between double clicks… if I only do double clicks, it will only work once…

weird hun ?

That makes sense in a way since it will wait for you to finish clicking before updating.
Maybe there’s an easier way, which integration are you using for your binary sensor?
Shelly, Tasmota, MQTT?

In any case, there might be an event fired on a double-click, you could listen to * events in the developer tools while double-clicking…

If there’s nothing, the next step would be to check if the click count updates before you’ve finished clicking:

(before) 93 --> (double-click) --> (now) 95
We could catch that :thinking:

(before) 93 --> (first click of double click) --> 94 --> (second click of double click) --> (now) 95
while this behavior would be bad :frowning_face:

I am using Shelly integration. I don’t know how to use mqtt … yet ! :slight_smile:

I’ll try to explore the double click event.

oh and the click numbers always increment by 1… doesn’t matter if I do a single double triple or long, it is always 1.

Thank you for the effort :wink:

Well, at least it increments. For now, you can use this:

alias: AUTOMATION_SCRIPT_OFFICE_WORK_LIGHTS_TOGGLE
description: Run Script to toggle office lights on button 2x press
trigger:
  - platform: state
    entity_id: binary_sensor.shelly_for_ha_shbtn_1_483fda6fe510_switch
    to: on
condition: []
action:
  - wait_for_trigger:
      - platform: state
        entity_id: binary_sensor.shelly_for_ha_shbtn_1_483fda6fe510_switch
        to: on
    timeout: '00:00:03' # wait this long until the 2nd button press
    continue_on_timeout: false
  - service: script.script_office_work_lights
mode: queued
max: 3

ok this one works like this… double single double etc…

But looking at the events, this one looks promising:

Event 33 fired 23:09:
{
“event_type”: “shellyforhass.click”,
“data”: {
“entity_id”: “binary_sensor.shelly_for_ha_shbtn_1_483fda6fe510_switch”,
“click_type”: “double”
},
“origin”: “LOCAL”,
“time_fired”: “2020-12-02T23:09:41.577749+00:00”,
“context”: {
“id”: “57753584a81aa1438de986d49a5e192e”,
“parent_id”: null,
“user_id”: null
}
}

I’ll try to explore this one.

Nice. This might do it then?

trigger:
  platform: event
  event_type: shellyforhass.click
  event_data:
    entity_id: binary_sensor.shelly_for_ha_shbtn_1_483fda6fe510_switch
    click_type: double

Bingo!!!

Thank you @fedot … really appreciated your help!
@jocnnor thank you as well!!!

trigger:
  - platform: event
    event_type: shellyforhass.click
    event_data:
      entity_id: binary_sensor.shelly_for_ha_shbtn_1_483fda6fe510_switch
      click_type: double
condition: []
action:
  - service: script.turn_on
    entity_id: script.script_office_work_lights
mode: queued
max: 3

Finally :sweat_smile:
Guess reading their docs might be worth it, didn’t know you use this integration: https://github.com/StyraHem/ShellyForHASS#events-1
Thanks to @jocnnor for me as well, learned a few things as well :smile: