Sian-Lee-SA / Home-Assistant-Switch-Manager

Switch manager is a centralised component to handle button pushes for your wireless switches. This includes anything passed through the event bus. The component relies on switch blueprints which is easily made to allow GUI configuration of your switches and their button pushes. This helps remove clutter from the automations.
Other
269 stars 88 forks source link

How to build automations to handle a hold -> hold (release) cycle? #99

Closed bendikrb closed 1 year ago

bendikrb commented 1 year ago

Hi, first off; thanks for your awesome work putting together this project!

Now, I'm struggling to understand how I should build my scripts in the case where I need a hold (release) action to interrupt the hold action sequence. It's possible I've completely overlooked it in the documentation or in the countless of google searches I've done, but all I've seen is how to write the generic parts of the blueprint YAML for one specific device; I would love to see some more documentation or examples on how to (best) utilize switch manager beyond just getting button triggers.

I have written blueprints for a 3 of my switches which weren't available yet - so I get that part at least.. =)

See below to get an idea on what I'm struggling with 😵‍💫 I've tried different variants of this - but I can't get the wait_for_trigger trigger to actually trigger!

Thanks in advance for any help!

- repeat:
    until: "{{ wait.trigger is not none or repeat.index >= 100 }}"
    sequence:
      - service: system_log.write
        data:
          message: Pretending to turn up brightness ... {{ repeat.index }}
      - wait_for_trigger:
          - platform: event
            event_type: zha_event
            event_data:
              device_id: "{{ data.device_id }}"
              command: stop_with_on_off
        timeout:
          milliseconds: 200

image

Sian-Lee-SA commented 1 year ago

Have you checked my video https://www.youtube.com/watch?v=0tvQTLGtK_I&ab_channel=BitBorn? You can acheive what you're doing by setting a variable and checking if that variable is true or false in your loop.

Most switches repeats the hold action so doing a loop like yours isn't very common but still doable.

Alternatively you can check the button_last_state variable and stop when the action title == hold (released) or even better != hold. https://github.com/Sian-Lee-SA/Home-Assistant-Switch-Manager#button_last_state-variable

bendikrb commented 1 year ago

Oh! No, that one slipped pass me! That's what I get for never bothering to watch videos in docs... I think I'll be good with the variable setting service!

My first idea was infact to use button_last_state or some other variables in data to achieve this, but as far as I could tell it wouldn't help much in that context ..

The switch doesn't repeat the hold action, by the way. Furthermore, the stop_with_on_off command looks the same whether you initiated from the ON button or the OFF button - so to discern that, the button_last_state variable came in handy!

Dumping the config of the switch in question here, for reference. I'll put in a pr someday soon!

name: Namron 4 button switch
service: ZHA
event_type: zha_event
identifier_key: device_id
buttons:
  # Top left (endpoint 1 "ON")
  - x: 118
    y: 115
    width: 165
    height: 279
    actions:
      - title: press
        conditions:
          - key: endpoint_id
            value: 1
          - key: cluster_id
            value: 6
          - key: command
            value: 'on'
      - title: hold
        conditions: >
          {{ data.endpoint_id == 1
          and data.cluster_id == 8
          and data.command == 'move_with_on_off'
          and data.params.move_mode == 0 }}
      - title: hold (released)
        conditions: >
          {{ data.endpoint_id == 1
          and data.cluster_id == 8
          and data.command == 'stop_with_on_off'
          and data.button_last_state[0].title == 'hold' }}
  # Bottom left (endpoint 2 "ON")
  - x: 118
    y: 398
    width: 165
    height: 279
    actions:
      - title: press
        conditions:
          - key: endpoint_id
            value: 2
          - key: cluster_id
            value: 6
          - key: command
            value: 'on'
      - title: hold
        conditions: >
          {{ data.endpoint_id == 2
          and data.cluster_id == 8
          and data.command == 'move_with_on_off'
          and data.params.move_mode == 0 }}
      - title: hold (released)
        conditions: >
          {{ data.endpoint_id == 2
          and data.cluster_id == 8
          and data.command == 'stop_with_on_off'
          and data.button_last_state[1].title == 'hold' }}

  # Top right (endpoint 1 "OFF")
  - x: 516
    y: 115
    width: 165
    height: 279
    actions:
      - title: press
        conditions:
          - key: endpoint_id
            value: 1
          - key: cluster_id
            value: 6
          - key: command
            value: 'off'
      - title: hold
        conditions: >
          {{ data.endpoint_id == 1
          and data.cluster_id == 8
          and data.command == 'move_with_on_off'
          and data.params.move_mode == 1 }}
      - title: hold (released)
        conditions: >
          {{ data.endpoint_id == 1
          and data.cluster_id == 8
          and data.command == 'stop_with_on_off'
          and data.button_last_state[2].title == 'hold' }}
  # Bottom right (endpoint 2 "OFF")
  - x: 516
    y: 398
    width: 165
    height: 279
    actions:
      - title: press
        conditions:
          - key: endpoint_id
            value: 2
          - key: cluster_id
            value: 6
          - key: command
            value: 'off'
      - title: hold
        conditions: >
          {{ data.endpoint_id == 2
          and data.cluster_id == 8
          and data.command == 'move_with_on_off'
          and data.params.move_mode == 1 }}
      - title: hold (released)
        conditions: >
          {{ data.endpoint_id == 2
          and data.cluster_id == 8
          and data.command == 'stop_with_on_off'
          and data.button_last_state[3].title == 'hold' }}
Dunkelschunkel commented 1 year ago

@bendikrb my aquara switch does not repeat the hold action either. I ended up with a variable for each button and setting it to 1 when that button is held down. Then having a loop that executes as long as the variable has the desired value. The loop has 2 actions: change brightness and a delay thereafter. The hold(release) automation has a delay set and than it changes the variable for that button to 0. That gives my great flexibility whith hold actions