#Perform an action based on time between two triggers

1 messages · Page 1 of 1 (latest)

signal idol
#

I'm looking for feedback on how I could accomplish the following:

Given that I have a remote device that produces a "On Pressed" "On Released" event when a button is pressed, and subsequently released, but not one for when it is held.

I would like to know in an automation the time between the pressed, and the released triggers, so that I can perform an action based on them.

The use case is I have a lutron Caseta pico remote, and I want to use the center button to set, and activate a specific brightness value for a light.

I've worked out that i'll need a helper to save the favorited value, but i'm not sure what the best way is to say "if pressed is held for 5s do path A, if pressed is released in <1s do path B"

hybrid shadow
#

I would use a wait_for_trigger in the action for the "on released" event with a timeout of the longer time period and then use the wait.remaining variable to derive how long it was pressed for

#

and the automation trigger would be the "on pressed" event

signal idol
#

that's for scripts tho...

#
alias: Garage Dimmer Favorite Save
description: ""
triggers:
  - device_id: 1f1f1101cf814af9c5fc97f08aa07c27
    domain: lutron_caseta
    type: press
    subtype: stop
    trigger: device
  - device_id: 1f1f1101cf814af9c5fc97f08aa07c27
    domain: lutron_caseta
    type: release
    subtype: stop
    trigger: device
    enabled: false
conditions: []
actions:
  - action: input_number.set_value
    target:
      entity_id: input_number.garage_dimmer_favorite
    data:
      value: "{{state_attr('light.shelly_dimmer_1_light','brightness')}}"
mode: single
#

so, the action to set the value is working

#

so I'd set a timeout for 5s and have it continue to save?

hybrid shadow
#

that's for scripts tho...
The action in an automation is a script

hybrid shadow
#

no, I would do what I suggested above

signal idol
#

Sorry, I am having a bit of a dumb moment here, can you explain a little more?

hybrid shadow
#
  • trigger on "on pressed"
  • use a wait_for_trigger in the action looking for the "on released" event with a timeout using the longest time period you want to match
  • use an if/then or choose after that based on the wait variable (see link above) to see how much time was left on the timer
#

if you use a timeout of 5 seconds, then the remaining time would be 0 (or just check for whether it completed) for a 5s hold, if it's 4s, then it was held for 1s

hybrid shadow
#

I just threw this together:

- id: button_press
  alias: Button Press
  triggers:
    - trigger: event
      event_type: button_pressed
  action:
    - wait_for_trigger:
        trigger: event
        event_type: button_released
      timeout: 5
      continue_on_timeout: true
    - action: persistent_notification.create
      data:
        message: "{{ 'Button pressed for ' ~ (5 - wait.remaining) ~ 's' }}"
#

tested with this script:

press_button:
  sequence:
    - event: button_pressed
    - delay: 2
    - event: button_released
    - delay: 5
    - event: button_pressed
    - delay: 5
    - event: button_released
#

A more complete example:

- id: button_press
  alias: Button Press
  triggers:
    - trigger: event
      event_type: button_pressed
  action:
    - variables:
        timeout: 5
    - wait_for_trigger:
        trigger: event
        event_type: button_released
      timeout:
        seconds: "{{ timeout }}"
      continue_on_timeout: true
    - variables:
        time_pressed: "{{ (timeout - wait.remaining)|round }}"
    - if:
        - "{{ time_pressed <= 2 }}"
      then:
        - action: persistent_notification.create
          data:
            message: Something when button is pressed for 2s or less
      else:
        - action: persistent_notification.create
          data:
            message: Something when button is pressed for longer than 2s
#

hope that helps...

signal idol
#

That's fantastic, and very much helps

#

thank you

signal idol
#
alias: Garage Dimmer Favorite Save
description: ""
triggers:
  - device_id: 1f1f1101cf814af9c5fc97f08aa07c27
    domain: lutron_caseta
    type: press
    subtype: stop
    trigger: device
    id: favorite_down
  - device_id: 1f1f1101cf814af9c5fc97f08aa07c27
    domain: lutron_caseta
    type: release
    subtype: stop
    trigger: device
    id: favorite_up
conditions: []
actions:
  - variables:
      timeout: 5
      min_time: 1
  - wait_for_trigger:
      - device_id: 1f1f1101cf814af9c5fc97f08aa07c27
        domain: lutron_caseta
        type: release
        subtype: stop
        trigger: device
    timeout:
      seconds: "{{ timeout }}"
    continue_on_timeout: true
  - variables:
      time_pressed: "{{ (timeout - wait.remaining)|round }}"
  - if:
      - condition: template
        value_template: "{{ time_pressed <= min_time }}"
    then:
      - action: light.turn_on
        metadata: {}
        data:
          brightness: "{{ 'states(input_number.garage_dimmer_favorite)' }}"
        target:
          entity_id: light.shelly_dimmer_1_light
    else:
      - action: input_number.set_value
        target:
          entity_id: input_number.garage_dimmer_favorite
        data:
          value: "{{state_attr('light.shelly_dimmer_1_light','brightness')}}"
mode: single
#

it works, but it's not setting the brightness like i need it to

#

nvm, got it working now!