Can you create a "function" in HA to simpliy Automation.yaml?

I have a Xiaomi Zigbee button in each of my rooms (sometimes several per) and these buttons control the Lifx lights in that room.
All the buttons around the house work the same, makes things easy for all members of the house and guests.
1 press - Toggle Light
2 press - Toggle between 50% and 100% brightness
Press & Hold - turn on lights to 13% brightness

This works fantastic, but, if i want to make a change to any of the automaitions e.g. change the brightness levels when you hold the button, I have to do it to ALL the automations i have written, as consistency is key between the rooms, this is a very lengthy process that is prone to human error.

I have a little bit of experience in Arduino and C++, so i was wondering if it’s possible to create a “function” when i can pass the “switch event” (1 press, 2 press, Hold), the “switch name” and the “light names” into the function to process and execute the desired command.

This way there is effectively only one automation code (the function) which is easy to maintain, change, and easy to scale up when I buy more lights/buttons.

For example (very rough pseudocode)
Event yaml:

- alias: Lights Living Room
  trigger:
    platform: event
    event_type: xiaomi_aqara.click
    event_data:
      entity_id: binary_sensor.switch_0001
  action:
  - alias: ''
    data:
    service: Xiaomi_Switch(click.type, binary_sensor.switch_0001, light.living_room_lights) 

Automation Function:

Xiaomi_Switch(switch.event, switch.name, light.name){ 

	if( switch.event == "1 press"){
		Toggle light.name }

	if( switch.event == "2 press"){
	 service: light.turn_on
        data_template:
          entity_id: light.name
          brightness: "{% if states.light.name.attributes.brightness  <\
            \ 254 %}\n 254\n{% elif states.light.name.attributes.brightness\
            \  > 250 %}\n 130\n{% endif %}\n" }

	if( switch.event == "Press & Hold"){
	   turn on light.name
       Brightness = 13 }
} 

If i have to create a function for each event type, i can live with it.

So… is what im trying to achieve possible?

Thank you!

I’m pretty sure with node red I could get this done with a js function node, but I’m useless with the yamls. Do you use node red?

I’ve played with it for a total of 5 minutes.

You could create a script and pass the parameters to the script or if you are a bit familiar with Python, I can highly suggest AppDaemon.

You can do this with templating, and a “map”:

trigger:
  platform: state
  to: 'on'
  entity_id:
    - binary_sensor.switch_0001
    - binary_sensor.switch_0002
    - ...
action:
- service: light.turn_on
  data_template:
    entity_id: >-
      {% set map = {
        "binary_sensor.switch_0001": "light.living_room_lights",
        "binary_sensor.switch_0002": "light.family_room_lights",
        "binary_sensor.switch_0003": "..."
      }%}
      {{ map.get(trigger.to_state.entity_id, "light.default") }}

(All this stolen shamelessly from @ludeeus)

The second part of .get() is a default, if the first part is not found in the “map”

2 Likes

That map function looks pretty much like it’s the way to go!

currently i have the brightness function look at the current brightness from the illuminated light, and change the brigheness value dependant that brightness:

      brightness: "{% if states.light.bedroom_lights.attributes.brightness  < 13 %}\n\
        \ 127\n{% elif states.light.bedroom_lights.attributes.brightness  >= 13 %}\n\
        \ 12\n{% endif %}\n"

how would i add the map into the brightness: function.
like below?

      brightness: "{% if states.map.attributes.brightness  < 13 %}\n\
        \ 127\n{% elif states.map.attributes.brightness  > 13 %}\n\
        \ 12\n{% endif %}\n"

p.s. i know it’s been a few months since i visited this thread, but new born + COVID19 really threw a spanner into the works :smiley: