How to create an automation that triggers by toggle, when single clicking a button?

My light switches supports 0-100% brightness and I am thinking 0% 33% 66% and 100% for my wireless switches (Zigbee).

In the automation page, I can choose ‘when button is single clicked’,

Do()!

I am just wondering how do I write a program for it to do (0% 33% 66% and 100% and back to 0%) function? Thanks.

Hi there, I’m not sure which switches you are using, and how Home-assistant detects a single click from them, but I will provide an automation that will cycle thru 0% (off), 33%, 66% and 100%. I will be using an Aqara button to simulate your switch, so you will have to make changes to the trigger to fit your switches. So, this is a quick and dirty automation that will get the job done:

id: Toggle Brigthness
alias: Toggle Brigthness
trigger:
  platform: event
  event_type: xiaomi_aqara.click
  event_data:
    entity_id: binary_sensor.switch_main
    click_type: single
condition: []
action:
  - choose:
    - conditions:
        - condition: state
          entity_id: light.livingroom
          state: 'off'
      sequence:
        - service: light.turn_on
          data:
            brightness: 84
          target:
            entity_id: light.livingroom
    - conditions:
        - condition: numeric_state
          entity_id: light.livingroom
          attribute: brightness
          above: '84'
          below: '168'
      sequence:
        - service: light.turn_on
          data:
            brightness: 168
          target:
            entity_id: light.livingroom
    - conditions:
        - condition: numeric_state
          entity_id: light.livingroom
          attribute: brightness
          above: '168'
          below: '255'
      sequence:
        - service: light.turn_on
          data:
            brightness: 255
          target:
            entity_id: light.livingroom
    - conditions:
        - condition: numeric_state
          entity_id: light.livingroom
          attribute: brightness
          state: '255'
      sequence:
        - service: switch.turn_off
          entity_id: light.livingroom
    default: []
mode: single

This automation will start from the bulb being off - 0% to 33% to 66% then to 100%. You might find that you will want to change it so when you first click the button while the light is off it will go to full brightness 100% and the go to 66% and so on, but that’s what so cool about his you have so many options.

when i copy and pasted, replaced with my device ID, it’s showing ‘‘Message malformed: extra keys not allowed @ data[‘actions’][0][‘choose’][3][‘conditions’][0][‘state’]’’

This is the auto generated code for the ‘trigger’ part (which is single click the right button)

alias: New automation
description: “”
triggers:

  • domain: mqtt
    device_id: d14bxxxxxxxxxxxxxxxxxxxxxxxx
    type: action
    subtype: single_right
    trigger: device
    conditions:
    actions:
    mode: single

I just used the copy option form the top right corner of the automation and pasted it into a text document and renamed it to toggle_brightness_automation.yaml, and it did not give me any errors when I checked the config. From the error that you are getting, I believe you are somehow getting extra characters in the automation, ones that you can not see. I would recommend downloading something like Notepad ++ and copying and saving with that, I think that will clear up your issue.

When I copy and paste
it shows

‘’ Toggle Brigthness

Traces

Message malformed: extra keys not allowed @ data[‘actions’][0][‘choose’][3][‘conditions’][0][‘state’]‘’

I cannot save the script

What method are you using for your automation? Are you using the build in automation editor, or are you using the automation.yaml file?

This is my above button showing when ‘The button’ is single clicked.

it’s Automation editor, I am not aware where automation.yaml is.
I do not wish to make changes to the main file, I just want to add another ‘automation’ under the automaton tab.

the code being:

So, it looks like you are using the Automation Editor, I can not show you how to transcribe this automation to the editor, but I can show you a way to add it. Go into your Home-assistant config folder and create a directory call “automations”. Once that is done open up your configuration.yaml which is also in your config folder and add this line somewhere near the top:

automation: !include_dir_list automations/

Now open your newly created automation folder and create a text document and give it a name, whatever you want, and make sure .yaml is at the end (my_automation.yaml). Open this text document and paste the automation into this, and save and close. Now restart Home-assistant, and when Home-assistant comes back online your automation should be active. Sorry I don’t have much experience with the Editor, but when I started this did not exist, I have been writing them by hand for a while now.

Automation with your trigger:

id: Toggle Brigthness
alias: Toggle Brigthness
triggers:
  - trigger: device
    domain: mqtt
    device_id: d14bcd0b3945e7eb725f17d28a77ab07
    type: action
    subtype: single_right
condition: []
action:
  - choose:
    - conditions:
        - condition: state
          entity_id: light.livingroom
          state: 'off'
      sequence:
        - service: light.turn_on
          data:
            brightness: 84
          target:
            entity_id: light.livingroom
    - conditions:
        - condition: numeric_state
          entity_id: light.livingroom
          attribute: brightness
          above: '84'
          below: '168'
      sequence:
        - service: light.turn_on
          data:
            brightness: 168
          target:
            entity_id: light.livingroom
    - conditions:
        - condition: numeric_state
          entity_id: light.livingroom
          attribute: brightness
          above: '168'
          below: '255'
      sequence:
        - service: light.turn_on
          data:
            brightness: 255
          target:
            entity_id: light.livingroom
    - conditions:
        - condition: numeric_state
          entity_id: light.livingroom
          attribute: brightness
          state: '255'
      sequence:
        - service: switch.turn_off
          entity_id: light.livingroom
    default: []
mode: single

I completely understand your logic, I am looking for a solutions just to use the ‘automation editor’ and makes a few changes inside its ‘yaml’ editor, but I will make notes how you do it!

Does anyone know how to transcribe?

Taking a look, this might be the solution, if you remove the first two lines of the automation:

id: Toggle Brigthness
alias: Toggle Brigthness

and substitute this instead,it might work for you the way you are doing it:

description: "Toggle Brightness"

I hope that does it for you.

Using the automation editor, create a new automation then select yaml mode.
Paste the code into the editor then select UI mode and you’ll see it.

You’ll have to change light.livingroom to whatever the entity name is for your lights, but you can do this via the visual editor.


It doesnt… some lines won’t save.

Yes. after copying, I cannot seem to save the file. shows

Looks like I made an indent error, my Home-assistant, didn’t seem to care about it, try this:

id: Toggle Brigthness
alias: Toggle Brigthness
triggers:
  - trigger: device
    domain: mqtt
    device_id: d14bcd0b3945e7eb725f17d28a77ab07
    type: action
    subtype: single_right
condition: []
action:
  - choose:
      - conditions:
          - condition: state
            entity_id: light.livingroom
            state: 'off'
        sequence:
          - service: light.turn_on
            data:
              brightness: 84
            target:
              entity_id: light.livingroom
      - conditions:
          - condition: numeric_state
            entity_id: light.livingroom
            attribute: brightness
            above: '84'
            below: '168'
        sequence:
          - service: light.turn_on
            data:
              brightness: 168
            target:
              entity_id: light.livingroom
      - conditions:
          - condition: numeric_state
            entity_id: light.livingroom
            attribute: brightness
            above: '168'
            below: '255'
        sequence:
          - service: light.turn_on
            data:
              brightness: 255
            target:
              entity_id: light.livingroom
      - conditions:
          - condition: numeric_state
            entity_id: light.livingroom
            attribute: brightness
            state: '255'
        sequence:
          - service: switch.turn_off
            entity_id: light.livingroom
    default: []
mode: single