Turn off Switch after x time with Aqara Button

Hi,

I have one of those Aqara Mini Buttons and I want to do the following:

Single press: Turn on Switch for 90 seconds. If I press it again, the time resets to 90 again.
Double press: Turn on Switch for 10 Minutes.

The things I have found are very old or about lights. They also does not include all the recently added features.

Thanks in advance.

Do you have any sample automations? You should be able to make use of the automation modes to do this by specifying something like

- id: 'your_id'
  description: Blah blah blah
  trigger:
  - entity_id: your_mini_button
    platform: state
    to: 'on'
  action:
  - service: switch.turn_on
    data:
      entity_id: your_switch
  - delay:
      seconds: 90
  - service: switch.turn_off
    data:
      entity_id: your_switch
  mode: restart 

My syntax might be a little off, I stole this from one of my motion sensor automations. Setting mode:restart will make sure that every time the button is pressed, the delay restarts. You could probably even template this to handle single- and double-pressing in the same automation, but I can’t think of the exact syntax off the top of my head.

HTH

I don’t know what kind of events your Aqara Mini Button generates so the Event Trigger in the following example is merely a suggestion (you’ll have to adjust it to suit your needs).

- alias: example 1
  mode: restart
  trigger:
  - platform: event
    event_type: button_press
    event_data:
      event_type: single
      entity_id: whatever
  - platform: event
    event_type: button_press
    event_data:
      event_type: double
      entity_id: whatever
  action:
  - service: switch.turn_on
    entity_id: switch.my_switch
  - delay: "{{ '00:01:30' if trigger.event.data.event_type == 'single' else '00:10:00'}}"
  - service: switch.turn_off
    entity_id: switch.my_switch
3 Likes

Thanks, both of you :slight_smile:

I tried it with that:

- id: 'Schalter'
  alias: Schalter
  mode: restart
  trigger:
  - platform: event
    event_type: deconz_event
    event_data:
      id: schalter
      event: 1002
  - platform: event
    event_type: deconz_event
    event_data:
      id: schalter
      event: 1004
  action:
  - service: switch.turn_on
    entity_id: switch.stecker_1
  - delay: "{{ '00:00:10' if trigger.event.data.event_type == '1002' else '00:00:20'}}"
  - service: switch.turn_off
    entity_id: switch.stecker_1

The restart works, but always after 20 seconds. On single press it does not turn off after 10 seconds.

Remove the quotes delimiting the value 1002. With the quotes it’s handled as a string value, without them it’s an integer value.

  - delay: "{{ '00:00:10' if trigger.event.data.event_type == 1002 else '00:00:20'}}"

Full automation:

- id: 'Schalter'
  alias: Schalter
  mode: restart
  trigger:
  - platform: event
    event_type: deconz_event
    event_data:
      id: schalter
      event: 1002
  - platform: event
    event_type: deconz_event
    event_data:
      id: schalter
      event: 1004
  action:
  - service: switch.turn_on
    entity_id: switch.stecker_1
  - delay: "{{ '00:00:10' if trigger.event.data.event == 1002 else '00:00:20'}}"
  - service: switch.turn_off
    entity_id: switch.stecker_1

Still not turning off after 10 seconds.

I think I see the problem. Your Event Trigger is using data from a Deconz event, specifically id and event. However, in the template you are using the example I provided which was not based on a Deconz event. It uses event_type when it should be using event.

Try this:

  - delay: "{{ '00:00:10' if trigger.event.data.event == 1002 else '00:00:20'}}"

I think that will work. If it doesn’t, put the quotes back for '1002' (but I don’t think you’ll need to do that).

Works as expected now, thanks alot :slight_smile:

1 Like