I have it working well! But how would YOU do it?

Hi all!

Fairly new HA user here. It’s been a great experience so far and between the documentation and forum search I’ve been able to solve every stumbling block I’ve come across.

I was working on some simple tap button automations last night and got to wondering how a more seasoned user would build the automation. I got it working and am happy with the functionality but I’m sure there are techniques/tricks that I’m just not aware of yet.

Take this automation for example. A Hue Tap Switch with four buttons launching 3 lighting scenes (via scripts) and an Off state.

This is what I came up with, but what might be a cleaner/more advanced approach?

- id: '1662424969910'
  alias: Living Room Entry Tap
  description: ''
  trigger:
  - device_id: 652ca016e3b2d80965689318adf91245
    domain: hue
    platform: device
    type: initial_press
    subtype: 1
    unique_id: 42244138-9ec1-4ffa-b0c4-efbd0aafc64e
    id: '1'
  - device_id: 652ca016e3b2d80965689318adf91245
    domain: hue
    platform: device
    type: initial_press
    subtype: 2
    unique_id: 47598d33-2a17-4922-a1ac-2bef674aebbf
    id: '2'
  - device_id: 652ca016e3b2d80965689318adf91245
    domain: hue
    platform: device
    type: initial_press
    subtype: 3
    unique_id: 245c2357-a8ee-4324-8974-c7a4df86c833
    id: '3'
  - device_id: 652ca016e3b2d80965689318adf91245
    domain: hue
    platform: device
    type: initial_press
    subtype: 4
    unique_id: 9d7c9f1d-f283-4adb-8432-7e2190f159fd
    id: '4'
  condition: []
  action:
  - choose:
    - conditions:
      - condition: trigger
        id: '1'
      sequence:
      - service: light.turn_off
        data:
          transition: 4
        target:
          entity_id:
          - light.shapes_d14c
          - light.living_room
    - conditions:
      - condition: trigger
        id: '2'
      sequence:
      - service: script.unified_living_room_bright
        data: {}
    - conditions:
      - condition: trigger
        id: '3'
      sequence:
      - service: script.unified_living_room_dimmed
        data: {}
    - conditions:
      - condition: trigger
        id: '4'
      sequence:
      - service: script.unified_living_room_vibes
        data: {}
    default: []
  mode: single

Would love to hear any tips, tricks and approaches I should look into about building better automations in general!

Cheers and Thanks

I’m not as good as some of hte others here, but your way works. Personally i’d make it more generic and simply add triggers maybe for each and call the associated script. Then I can expand this to more buttons.

No I didn’t test this. Just a quick markup…

alias: Hue Taps
description: ''
trigger:
- device_id: 652ca016e3b2d80965689318adf91245
  domain: hue
  platform: device
  type: initial_press
  subtype: 1
  unique_id: 42244138-9ec1-4ffa-b0c4-efbd0aafc64e
  id: 'unified_living_room_off'
- device_id: 652ca016e3b2d80965689318adf91245
  domain: hue
  platform: device
  type: initial_press
  subtype: 2
  unique_id: 47598d33-2a17-4922-a1ac-2bef674aebbf
  id: 'unified_living_room_bright'
- device_id: 652ca016e3b2d80965689318adf91245
  domain: hue
  platform: device
  type: initial_press
  subtype: 3
  unique_id: 245c2357-a8ee-4324-8974-c7a4df86c833
  id: 'unified_living_room_dimmed'
- device_id: 652ca016e3b2d80965689318adf91245
  domain: hue
  platform: device
  type: initial_press
  subtype: 4
  unique_id: 9d7c9f1d-f283-4adb-8432-7e2190f159fd
  id: 'unified_living_room_vibes'
condition: []
action:
  - service: >-
      script.{{trigger.id}}
    data: {}
mode: single
1 Like

If you make a script called script.unified_living_room_off, the entire choose can be replaced by a single service call employing a template with a dictionary.

action:
  - service: "script.unified_living_room_{{ {1: 'off', 2: 'bright', 3: 'dimmed', 4: 'vibes').get(trigger.id | int(0), 'off') }}"

You can dispense with the dictionary entirely if you put the desired script’s name in the trigger’s id. So instead of naming it 3 call it dimmed. The template is reduced to:

action:
  - service: "script.unified_living_room_{{ trigger.id }}"

You can also replace the four Device Triggers with a single Event Trigger. Instead of using trigger.id you would reference the subtype. Let me know if that interests you.

1 Like

Very cool, thank you! Yes making it generic/repeatable/templatable is definitely a goal of mine.

Oh wow this is amazing! I’ll definitely look more into templates.

I’ll have to dig more into events as well that looks like an excellent option.
I found the event listener and see what you’re referring to:

event_type: hue_event
data:
  id: front_door_tap_button
  device_id: 652ca016e3b2d80965689318adf91245
  unique_id: 47598d33-2a17-4922-a1ac-2bef674aebbf
  type: initial_press
  subtype: 2
origin: LOCAL
time_fired: "2022-09-06T20:59:04.418633+00:00"
context:
  id: 01GCA9JWV2R49YKPZAVPJCB274
  parent_id: null
  user_id: null

I’m sure this is a basic question but what would be the best way for me to reference that subtype in the automation?

Thanks!

Here’s the general idea:

- id: '1662424969910'
  alias: Living Room Entry Tap
  description: ''
  trigger:
    - platform: event
      event_type: hue_event
      event_data:
        id: 'front_door_tap_button'
        type: 'initial_press'
  action:
    - service: "script.unified_living_room_{{ {1: 'off', 2: 'bright', 3: 'dimmed', 4: 'vibes').get(trigger.event.data.subtype | int(0), 'off') }}"

If you remove the line containing id from the Event Trigger, then it no longer restricts itself to just one specific Hue Dimmer Switch but from all of them (assuming you have more than one).


EDIT

The int(0) filter isn’t necessary if trigger.event.data.subtype is already an integer value.

EDIT 2

Use more legible id instead of long-winded device_id.

1 Like

you could go a step further too:

- id: '1662424969910'
  alias: Hue Tap
  description: ''
  trigger:
    - platform: event
      event_type: hue_event
      event_data:
        type: 'initial_press'
  action:
    - service: "script.652ca016e3b2d80965689318adf91245_{{ {1: 'off', 2: 'bright', 3: 'dimmed', 4: 'vibes').get(trigger.event.data.subtype | int(0), 'off') }}"

Now you just need to name hte scripts with the device names and what you want them to do. You can keep the ‘1/2/3/4’ more generic and name your scripts like this as well:

script.652ca016e3b2d80965689318adf91245_1
script.652ca016e3b2d80965689318adf91245_2
etc…

Or one script with a parameter:
script.652ca016e3b2d80965689318adf91245
parameter {{ button }}

1 Like

Very slick! :+1:

Thank you Rob and Taras I’m excited to try out these approaches.

Although streamlining automations can become a fun brain exercise, the deeper question is why have you chosen to create separate scripts for each button-press?

For example, will script.unified_living_room_bright be called by anything other than pressing button #2 on one specific Hue Dimmer Switch?

If it won’t then there are better ways of designing this thing.

That is a good question…

As it stands there are two things that could call that script right now: the physical tap switch and a button exposed in Homekit.

In that room there are multiple different sets of lights from multiple lighting vendors each with their own Bright or Dimmed scenes etc. The idea of the Unified script is to have one place where I can tweak what is triggered anytime you want the room to be in the Bright state. I had initially tried to make a scene that contained scenes within it but quickly learned nested scenes are not supported hence the switch to scripts.

Even typing this out I can see how I might be adding extra effort that may not be needed and I could probably build all of this into one automation with multiple triggers…

Would love to hear your thoughts and any suggestions. I appreciate your time and insights, truly golden.

Cheers

1 Like

K.I.S.S. method

1 Like

Cheers y’all. Thanks for your replies, questions, and great ideas!

1 Like