Turn On / Off Lights When Button Pressed

HI All,
I have been using HA for a few months now, when I started out I watched multiple YouTube Videos and got a working solution. I have Several Lights and an Ikea E2002 “4 Way” Switch.
The Automation is a series of “When’s With Trigger ID’s” With “Then Do’s” hanging off the triggers.
I have been looking back over my automation and trying to work out if there is a better way to do it.
Can any one suggest an alternative method or point to a guide to help, my code is as below.

description: "Automation to Turn On / Off Lights in DineingRoom When The Buttons are pressed"
mode: restart
trigger:
  - alias: Button - Door - On
    platform: device
    domain: mqtt
    device_id: LongUUID
    type: action
    subtype: "on"
    id: Turn-On
  - alias: Button - Door - Off
    platform: device
    domain: mqtt
    device_id: LongUUID
    type: action
    subtype: "off"
    id: Turn-Off
  - alias: Button - Door - Up
    platform: device
    domain: mqtt
    device_id: LongUUID
    type: action
    subtype: brightness_move_up
    id: Move-Up
  - alias: Button - Door - Down
    platform: device
    domain: mqtt
    device_id: LongUUID
    type: action
    subtype: brightness_move_down
    id: Move-Down
  - alias: Button - Door - Stop
    platform: device
    domain: mqtt
    device_id: LongUUID
    type: action
    subtype: brightness_stop
    id: Move-Stop
  - platform: device
    domain: mqtt
    device_id: LongUUID
    type: action
    subtype: arrow_left_click
    alias: Button - Door - Left
    id: Door-Left
  - platform: device
    domain: mqtt
    device_id: LongUUID
    type: action
    subtype: arrow_right_click
    alias: Button - Door - Right
    id: Door Right
condition: []
action:
  - alias: Room Up
    choose:
      - conditions:
          - condition: trigger
            id:
              - Move-Up
        sequence:
          - repeat:
              sequence:
                - service: light.turn_on
                  data:
                    brightness_step_pct: 10
                  target:
                    area_id: dineing_room
                - delay:
                    hours: 0
                    minutes: 0
                    seconds: 0
                    milliseconds: 500
              while:
                - condition: trigger
                  id:
                    - Move-Up
  - alias: Room Down
    choose:
      - conditions:
          - condition: trigger
            id:
              - Move-Down
        sequence:
          - repeat:
              sequence:
                - service: light.turn_on
                  data:
                    brightness_step_pct: -10
                  target:
                    area_id: dineing_room
                - delay:
                    hours: 0
                    minutes: 0
                    seconds: 0
                    milliseconds: 500
              while:
                - condition: trigger
                  id:
                    - Move-Down
  - alias: Room - On
    choose:
      - conditions:
          - condition: trigger
            id:
              - Turn-On
        sequence:
          - service: light.turn_on
            data:
              transition: 1
            target:
              area_id: dineing_room
  - alias: Room - Off
    choose:
      - conditions:
          - condition: trigger
            id:
              - Turn-Off
        sequence:
          - service: light.turn_off
            data:
              transition: 1
            target:
              area_id: dineing_room

yeah… lots can be improved.

why do you have it all in one automation? the different blocks aren’t benefitting from each other for the most part.

also you have repeat while trigger id… that looks like it should be an infinite loop since the trigger id doesn’t change. does that actually do what you want it to do?

looks to me like the move-up condition and move-down will loops forever until the brightness is all the way up or all the way down (then actually continue to loop thereafter still). am i misreading?

here’s one way of improving the button-door up/down to turn up/down the dineing_room (sp? … ‘dining’) light by 10% each button press:

description: "dining room brightness"
mode: restart
trigger:
  - alias: Button - Door - Up
    platform: device
    domain: mqtt
    device_id: LongUUID
    type: action
    subtype: brightness_move_up
    id: "10"
  - alias: Button - Door - Down
    platform: device
    domain: mqtt
    device_id: LongUUID
    type: action
    subtype: brightness_move_down
    id: "-10"
condition: []
action:
  - service: light.turn_on
    data:
      brightness_step_pct: >
        {{ trigger.id | int }}
    target:
      area_id: dineing_room

note that i got rid of the loop. if you want to keep the loop, please explain your intent.

i combined the up and down into 1 and i used the trigger id to specify whether it’s going up or down.

here’s an automation that simplifies the turn_on/off:

description: "Automation to Turn On / Off "
mode: restart
trigger:
  - alias: Button - Door - On
    platform: device
    domain: mqtt
    device_id: LongUUID
    type: action
    subtype: "on"
    id: "on"
  - alias: Button - Door - Off
    platform: device
    domain: mqtt
    device_id: LongUUID
    type: action
    subtype: "off"
    id: "off"
condition: []
action:
  - service: light.turn_{{trigger.id}}
    data:
      transition: 1
    target:
      area_id: dineing_room
1 Like

Thank for the reply

ill look through your code and try to understand it…
To answer your questions:
I used one automation as I have mutiple rooms with the same setup, so I can just copy/paste the code for each room quickly.

The repeat, while, id… it was my understanding that “id” acts like a Boolean, if the action is being triggerd its True so the loop will repeat. The thing here is that the actions "brightness_move_down "and up are only triggered when holding down / up (on/off). “brightness_stop” is sent when you release Down or Up

you shouldn’t copy/paste a bunch of code… but as you see above you can reduce the code down to nearly nothing.

if i were you, i would put similar functions together. ie, have 1 function that down on/off for all rooms. similar to how i did {{ trigger.id }} to specify on or off, it’s possible for your to use variables to specify entities or areas. so instead of area_id: dining_room, you could have area_id: {{ current_area }}… and that {{ current_area }} is set depending on which button is pressed.

wrt trigger.id, when it enters that automation, trigger id will be set and it will not change for the life of that run of the automation. so therefore it is always matching or always not matching. so that means in this code block of yours:

      - conditions:
          - condition: trigger
            id:
              - Move-Up
        sequence:
          - repeat:
              sequence:
                - service: light.turn_on
                  data:
                    brightness_step_pct: 10
                  target:
                    area_id: dineing_room
                - delay:
                    hours: 0
                    minutes: 0
                    seconds: 0
                    milliseconds: 500
              while:
                - condition: trigger
                  id:
                    - Move-Up

it will only enter te sequence if trigger.id == “Move-Up”… you’re guaranteed for it tobe “Move-Up”. therefore when it goes into the repeat, since you are guarantted it is “Move-Up”, and because you are guarantteed it will not change, it will loop in that repeat forever…

or at least until the automation is restarted by some other button being pressed (because you have mode:restart).

still, stepping back, the question is… what is your intent here? did you want it to loop forever? are you trying to have that button to a fade all the way to 0 or all the way to 100% and then stop? what are you trying to do with that repeat?

I will often create a basic automation using someone’s blueprint, It usually covers my requirements but if it’s not enough or is too much, I can always modify the YAML manually to add or remove functionality.

There are many blueprints for this device that have been used by many community members and improved over time. You may find one of those as a good starting point for making an automation, as well as being able to manage it in the Home assistant user interface.

Most of these have the functionality to handle the dimming actions of these switches (but may require you to create a number or text helper to temporarily store the level of the dimmer control)

Thanks for referring :mending_heart:

1 Like