Automation Optimization question

Been using HA for about 6 weeks now, and am really moving along nicely thanks to everyone’s help on this community.

Just got around to adding my old 4 button minimote’s. They are working quite well with this simple set of automations, but was wondering if it would be possible to combine into a single automation, or what sort of things other on here might do for something like this. I mean it works right now, just curious if there are better, smarter ways to accomplish this.

#############################################
## 4 Button Controller #1 Buttons Settings ##
#############################################
- alias: 'Minimote Button 1 Pressed'
  trigger:
  - platform: event
    event_type: zwave.scene_activated
    event_data:
      entity_id:  zwave.aeotec_minimote_1
      scene_id: 1
  action:
  - service: homeassistant.toggle
    entity_id: light.bedroom_lamp_one
    
- alias: 'Minimote Button 1 Held'
  trigger:
  - platform: event
    event_type: zwave.scene_activated
    event_data:
      entity_id:  zwave.aeotec_minimote_1
      scene_id: 2
  action:
  - service: homeassistant.toggle
    entity_id: light.bedroom_lamp_one

- alias: 'Minimote Button 2 Pressed'
  trigger:
  - platform: event
    event_type: zwave.scene_activated
    event_data:
      entity_id:  zwave.aeotec_minimote_1
      scene_id: 3
  action:
  - service: homeassistant.toggle
    entity_id: light.bedroom_lamp_two

- alias: 'Minimote Button 2 Held'
  trigger:
  - platform: event
    event_type: zwave.scene_activated
    event_data:
      entity_id:  zwave.aeotec_minimote_1
      scene_id: 4
  action:
  - service: homeassistant.toggle
    entity_id: light.bedroom_lamp_two

- alias: 'Minimote Button 3 Pressed'
  trigger:
  - platform: event
    event_type: zwave.scene_activated
    event_data:
      entity_id:  zwave.aeotec_minimote_1
      scene_id: 5
  action:
  - service: homeassistant.toggle
    entity_id: light.bedroom_ceiling_light

- alias: 'Minimote Button 3 Held'
  trigger:
  - platform: event
    event_type: zwave.scene_activated
    event_data:
      entity_id:  zwave.aeotec_minimote_1
      scene_id: 6
  action:
  - service: homeassistant.toggle
    entity_id: light.bedroom_ceiling_light

- alias: 'Minimote Button 4 Pressed'
  trigger:
  - platform: event
    event_type: zwave.scene_activated
    event_data:
      entity_id:  zwave.aeotec_minimote_1
      scene_id: 7
  action:
  - service: homeassistant.toggle
    entity_id: switch.bedroom_fan

- alias: 'Minimote Button 4 Held'
  trigger:
  - platform: event
    event_type: zwave.scene_activated
    event_data:
      entity_id:  zwave.aeotec_minimote_1
      scene_id: 8
  action:
  - service: homeassistant.toggle
    entity_id: switch.bedroom_fan

You should be able to do this in one automation:

- alias: 'Minimote Button Pressed or Held'
  trigger:
  - platform: event
    event_type: zwave.scene_activated
    event_data:
      entity_id:  zwave.aeotec_minimote_1
  action:
  - service: homeassistant.toggle
    data_template:
      entity_id: >
        {% set map = {1: 'light.bedroom_lamp_one',
                      2: 'light.bedroom_lamp_one',
                      3: 'light.bedroom_lamp_two',
                      4: 'light.bedroom_lamp_two',
                      5: 'light.bedroom_ceiling_light',
                      6: 'light.bedroom_ceiling_light'} %}
        {{ map[trigger.event.data.scene_id|int] }}

I haven’t tried this exactly, but hopefully this is at least a start.

3 Likes
- alias: 'Minimote Button Pressed'
  trigger:
  - platform: event
    event_type: zwave.scene_activated
    event_data:
      entity_id:  zwave.aeotec_minimote_1
  condition:
    condition: template
    value_template: >
      {{ trigger.to_state.data.scene_id in range(1,9) }}
  action:
  - service: homeassistant.toggle
    data_template:
      entity_id: >
        {% set switches = [ 'light.bedroom_lamp_one', 'light.bedroom_lamp_two', 'light.bedroom_ceiling_light','switch.bedroom_fan' ] %}
        {% set i = (trigger.to_state.data.scene_id - 1) // 2 %}
        {{ switches[i] }}

The // symbol divides integers without making it a float. So the result should always be in the list of switches.

EDIT: Didn’t see the other responses, they’ll work for you too. This one is very similar to the other.

1 Like

Super cool, I can see using this in a few of my other automations as well. Let me make sure I fully understand.

So for the trigger, in my automation I had a trigger for each scene ID, with your example the scene ID is not there, so I’m assuming that this means it will trigger for any scene. Then the ‘set map’ will use which ever scene ID was the underlying trigger to then map that scene id to the entity listed.

Super cool, thanks

It will trigger for any scene with the entity id zwave.aeotec_minimote_1

Lol! I saw you responding so I didn’t bother. Then I didn’t see a response so I jumped in. Good idea checking the scene_id and minimizing the data structure size via the divide! :slight_smile:

Yeah, I got a call that side tracked me. Then I hit send and saw yours… lol oh well!

EDIT:

Thanks, I like lists better than dictionary maps because I hate anything verbose… It can be helpful. There are times you look back and think “WTF am I doing here” though.

1 Like

Amazing to get two responses so quickly. So I better understand can you explain a little bit how the template works.

Would I list, each possible trigger
(trigger.to_state.data.scene_id - 1) // (trigger.to_state.data.scene_id - 2) // (trigger.to_state.data.scene_id - 3)// and so on

or is just the / 2 % enough for it to realize there are more options?

Super cool, thanks you so much !

This is a visual representation of whats happening with the math

All this math is doing is taking 1 through 8, turning it into 0 through 3. So the first thing that needs to occur is changing the indexes from 1 through 8 to 0 through 7. So, subtract 1. Then we need to take 0 through 7 and turn it into 0 through 3 because selections (0, 1), (2,3), (4,5), (6,7) are essentially pairs. To do that, use integer divide. That will take any number and remove the remainder. So 7/2 would normally give 3.5, instead // returns 3 with the .5 chopped off.

Now how does 0 through 3 link to our list? Well all lists start with an index of 0. If we make a 4 item list, then item 1 would be index 0, item 2 would be index 1, etc.

1 Like

Awesome, got it. I saw the “-1” as Scene ID 1. :slight_smile:

So this works perfectly for my scenario where I am doing the same thing with a push and a hold.

If I wanted all 8 options, I would list them in the "{% set switches = " section, and just use this

{% set i = (trigger.to_state.data.scene_id ) %}

These examples rock!

Thanks for the visualization and also the reminder of how I can check this stuff out myself in the front end. :slight_smile:

1 Like