Several action on press

Hello,

i need help for a automation

Issue:

a binary button on a ESP

on press turn light on with weak light
on press again degrace light
on press aigsin turn off light

Here my code weak on and light off works but i can’t switch to the brighter light

  - alias: Heike Lampe schwach ein
    trigger:
      platform: state
      entity_id: binary_sensor.esp_heike_bett_taster_schwarz
      to: 'on' 
    condition:
      - condition: state
        entity_id: light.bf33
        state: 'off'  
    action:
      - service: scene.turn_on
        entity_id: scene.heike_weiss_schwach_an
      - service: input_boolean.turn_on
        data:
           entity_id: input_boolean.heike_bett_weiss_schwach


 - alias: Heike Lampe stark ein
   trigger:
     platform: state
     entity_id: binary_sensor.esp_heike_bett_taster_schwarz
     to: 'on' 
   condition:
     - condition: state
       entity_id: input_boolean.heike_bett_weiss_schwach
       state: 'on'  
   action:
     - service: scene.turn_off
       entity_id: scene.ingo_weiss_schwach_an
     - service: input_boolean.turn_off
       data:
         entity_id: input_boolean.heike_bett_weiss_schwach
    - service: scene.turn_on
       entity_id: scene.ingo_weiss_an 
    - service: input_boolean.turn_on
       data:
          entity_id: input_boolean.heike_bett_weiss_hell
        
  - alias: Heike Lampe aus
    trigger:
      platform: state
      entity_id: binary_sensor.esp_heike_bett_taster_schwarz
      to: 'on' 
    condition:
      - condition: state
        entity_id: input_boolean.heike_bett_weiss_schwach 
        state: 'on'  
    action:
      - service: light.turn_off
        entity_id: light.bf33  
      - service: input_boolean.turn_off
        data:
          entity_id: input_boolean.heike_bett_weiss_schwach  

is there also a way for automation to press long button ?
(similar the on_click: min_length: 50ms max_length: 350ms function in ESPphome)

Thanks for your Help

Greetings from Germany
Ingo

visit my german language Home Assitant Blog

Just add a wait_for_trigger after each section with a timeout. It will wait for up to ‘timeout’ for the trigger to happen. If it doesn’t happen within timeout, it will stop the whole automation.

  - alias: Heike Lampe schwach ein
    trigger:
      platform: state
      entity_id: binary_sensor.esp_heike_bett_taster_schwarz
      to: 'on' 
    condition:
      - condition: state
        entity_id: light.bf33
        state: 'off'  
    action:
      - service: scene.turn_on
        entity_id: scene.heike_weiss_schwach_an
      - service: input_boolean.turn_on
        data:
           entity_id: input_boolean.heike_bett_weiss_schwach
      - wait_for_trigger:
           platform: state
           entity_id: binary_sensor.esp_heike_bett_taster_schwarz
           to: 'on' 
        timeout: "00:00:03"
        continue_on_timeout: false
      - service: "< degrace light >"
      - wait_for_trigger:
           platform: state
           entity_id: binary_sensor.esp_heike_bett_taster_schwarz
           to: 'on' 
        timeout: "00:00:03"      

Only issue is it will restart after each press.

Edit - oh, I misread. I thought you wanted to emulate double or triple click. This doesn’t make sense if you didn’t want that lol.

I do something similar with a ceiling fan.

Every button press goes to the next speed. You can easily modify it to set a light brightness based on the current state/brightness attribute of the light:

  - alias: Turn MBR Fan On With Scene Switch
    trigger:
      - platform: event
        event_type: zwave.scene_activated
        event_data:
          entity_id: zwave.mbr_scene_switch
          node_id: 35
          scene_id: 1
          scene_data: 0
    action:
      - service: fan.set_speed
        data_template:
          entity_id: fan.master_bedroom_fan
          speed: >-
            {% if states('fan.master_bedroom_fan') == 'off' %}
              low
            {% elif states('fan.master_bedroom_fan') == 'on' and state_attr('fan.master_bedroom_fan', 'speed') == 'low' %}
              medium
            {% elif states('fan.master_bedroom_fan') == 'on' and state_attr('fan.master_bedroom_fan', 'speed') == 'medium' %}
              high
            {% elif states('fan.master_bedroom_fan') == 'on' and state_attr('fan.master_bedroom_fan', 'speed') == 'high' %}
              'off'
            {% endif %}

Jim.

thank for your example.
Is there any way to measure the time the button switch is on state “on” ?

So that i can create a different automation for short press and long press ?

You could just have 2 automations with the trigger look for ‘on’ for ‘X’ seconds. But, there is a chance both will fire if you weren’t careful enough.

Could do it in a single automation…trigger on the on state, then wait for the off state.

 - alias: Heike Lampe schwach ein
    variables: 
      short_press_s: 0.9
      on_time: "{{ as_timestamp(now()) }}"
    trigger:
      platform: state
      entity_id: binary_sensor.esp_heike_bett_taster_schwarz
      to: 'on' 
    action:
      - wait_for_trigger:
           platform: state
           entity_id: binary_sensor.esp_heike_bett_taster_schwarz
           to: 'off'
      - variables:
          # Calculate how many seconds the button was pressed
          press_time_s:  "{{ as_timestamp(now()) - (on_time | float) }}"
      - choose:
        conditions:
          condition: template
          value_template: "{{ press_time_s | float < short_press_s | float}}"
        sequence:
          - service: "<< do something on short press >>"
      - choose:
        conditions:
          condition: template
          value_template: "{{ press_time_s | float >= short_press_s | float }}"
        sequence:
         - service: "<< do something on long press >>"

Considerations:

I’m not sure how precise the timing will be on an automation firing. You have to consider the latency of releasing the button, state updating in home assistant, and HA firing this automation. And, this latency could be variable depending on many different factors…

The real solution would be to implement the long press in the ESP button itself and have it send events rather than be a binary_switch.