Need template gurus to provide some guidance :)

Hello all,

I have created an automation to use when a button is pressed on my switch. If the light (one light in a group of Hue lights in that Hue room) is On, turn it off, if the light is off, turn it on.
So far so good, nice and easy.

- alias: H1 Office
  description: 'automation to handle the switch in Office'
  use_blueprint:
    path: Jonybat/aqara_ws-k07e_event_entity.yaml
    input:
      event: event.h1_office_action
      helper_last_controller_event: input_text.h1_office_last_pressed
      helper_last_event_time: input_text.h1_office_last_pressed
      single_up:
        - action: automation.trigger
          metadata: {}
          target:
            entity_id: automation.office_lights
          data:
            skip_condition: true
- alias: Office Lights
  description: 'automation to handle turning the lights on/off when H1 Office up button is pressed'
  triggers: []
  conditions: []
  actions:
    - choose:
        - conditions:
            - condition: device
              type: is_off
              device_id: 3514bb7197b7b4a194ae514d0d1c20a2
              entity_id: 5318e3acfbe31fd53170f6b9b4684541
              domain: light
          sequence:
            - action: light.turn_on
              metadata: {}
              target:
                entity_id: light.office
              data: {}
            - action: switch.turn_on
              metadata: {}
              target:
                entity_id: switch.h1_office_led_indicator
              data: {}
        - conditions:
            - condition: device
              type: is_on
              device_id: 3514bb7197b7b4a194ae514d0d1c20a2
              entity_id: 5318e3acfbe31fd53170f6b9b4684541
              domain: light
          sequence:
            - action: light.turn_off
              metadata: {}
              target:
                entity_id: light.office
              data: {}
            - action: switch.turn_off
              metadata: {}
              target:
                entity_id: switch.h1_office_led_indicator
              data: {}
  mode: single

I have 20+ switches at home for which I would need to create the exact same thing. So I’m thinking I should be able to use a template or a blueprint (not clear yet on the difference) where the inputs would be

  • the switch to setup
  • the light to test
  • the helper to use

And it could create those 2 automations (one based on the blueprint, the other to be used when it triggers) for me directly.

I was going to write a python script to put those inputs in a csv and generate the yaml brute force but I’m pretty sure there is a more elegant way !

All help welcome

EDIT: realize this is clearly a blueprint which I need

First, as a general practice, either place the action in the blueprinted automation or use a script not a second automation. Automations without triggers have a history of causing issues.

As to your specific question…

You will likely need to forego the first blueprint so that you can trigger off multiple event entities in the same automation.

1 Like

Good point thank you. It’s also much simpler.

This is the modified version:

- id: '1776176222888'
  alias: H1 Switch Office
  description: ''
  use_blueprint:
    path: Jonybat/aqara_ws-k07e_event_entity.yaml
    input:
      event: event.h1_office_action
      helper_last_controller_event: input_text.h1_office_last_pressed
      helper_last_event_time: input_text.h1_office_last_pressed
      single_up:
      - choose:
        - conditions:
          - condition: device
            type: is_on
            device_id: 3514bb7197b7b4a194ae514d0d1c20a2
            entity_id: 5318e3acfbe31fd53170f6b9b4684541
            domain: light
          sequence:
          - action: light.turn_off
            metadata: {}
            target:
              entity_id: light.office
            data: {}
          - action: switch.turn_off
            metadata: {}
            target:
              entity_id: switch.h1_office_led_indicator
            data: {}
        - conditions:
          - condition: device
            type: is_off
            device_id: 3514bb7197b7b4a194ae514d0d1c20a2
            entity_id: 5318e3acfbe31fd53170f6b9b4684541
            domain: light
          sequence:
          - action: light.turn_on
            metadata: {}
            target:
              entity_id: light.office
            data: {}
          - action: switch.turn_on
            metadata: {}
            target:
              entity_id: switch.h1_office_led_indicator
            data: {}

I take it the buttons are not also hardware switches that power on/off the light? What about using light.toggle?

I had one automation that would trigger on a group and then look at the group entities to see what was changed.

Are you asking how to not have so many separate automations? I also have a bunch of scene controllers where I trigger like this:

  - trigger: state
    not_from: unavailable
    not_to: unavailable
    entity_id:
      - event.living_room_cans_scene_001
      - event.living_room_cans_scene_002
      - event.wall_splash_scene_001
      - event.cabinet_lights_scene_001
      - event.family_room_cans_scene_001
      - event.family_room_cans_scene_002
      - event.recirc_pump_remote_switch_scene_001
      - event.recirc_pump_remote_switch_scene_002
      - event.primary_bath_fan_zen71_scene_001
      - event.primary_bath_fan_zen71_scene_002
      - event.2nd_bath_fan_scene_001
      - event.guest_fan_switch_scene_001
      - event.guest_fan_switch_scene_002

And then map those to running variious things.

A few comments:

        - action: switch.turn_on
          metadata: {}
          target:
            entity_id: switch.h1_office_led_indicator

I have a separate automation for my indicator lights because I want them to reflect state, not when I pushed a button.

My little rant here is I find it odd that the developers decided to generate code like this. If you know the ID of the entity why not use the entity_id (the abstract name for that)?

            - condition: device
              type: is_off
              device_id: 3514bb7197b7b4a194ae514d0d1c20a2
              entity_id: 5318e3acfbe31fd53170f6b9b4684541
              domain: light

Thanks for taking the time !

The buttons are also in fact hardware switches but I have decoupled them and locked them to make sure the lights are always reachable.

I want to be able to also use the hue app to potentially turn the lights on or off so toggle would not really work in that case since I also want to keep the led light on the switch itself in sync. The way I have setup the automation, the led will reflect state the next time the switch is pressed but your comment made me realize there might be a better way indeed.

How would you recommend setting it up instead ? Are you linking the on/off state of groups of lights to the led on the switch ? Would be keen to see an example if you have one handy.

Agreed on device and entity id not using the easily readable version but I can modify that when I build the blueprint I guess ?

Perhaps something like this could work for you?

description: ""
mode: single
triggers:
  - trigger: event
    event_type: event.h1_office_action
    id: light.office
  - trigger: event
    event_type: event.h2_office_action
    id: light.office_2
  - trigger: event
    event_type: event.h1_kitchen_action
    id: light.kitchen
conditions: []
actions:
  - action: "switch.turn_{{ 'on' if states(trigger.id) == 'off' else 'off' }}"
    metadata: {}
    target:
      entity_id: "{{ trigger.event.event_type | replace('action', 'led_indicator') }}"
    data: {}
  - action: "switch.turn_{{ 'on' if states(trigger.id) == 'off' else 'off' }}"
    metadata: {}
    target:
      entity_id: "{{ trigger.id }}"
    data: {}

Could you explain a bit more about that first sentense?

And for that second point, that’s why you have the indicators trigger on what the state of the light is, not when you push the button.

Is your goal to be able to press any of the light switches and turn on all the lights in the group?

Or is it just to have indicator lights show up. (My light indicator lights show up when the light is off so I can find the button in the dark. When the overhead light is on I kind of know it’s on so the indicator isn’t really useful in that case. :wink: )

so @busman I went your way and it’s a lot simpler indeed.

My only issue is that to check if a group of lights is on or off, I have to choose a single one in the group so some cases might arise when the led on the switch is not in sync with the group if the specific light I have chosen to represent that group is turned on/off independently from the group (in the hue app for example).

Think I can live with that but curious how you handle that case (short of doing all the possible combinations of on/off lights in a group and deciding case by case if you want led on or off…) ?

Could you explain a bit more about that first sentense?

I am using the Aqara H2 switches which you can use in both relay control or decoupled.

(My light indicator lights show up when the light is off so I can find the button in the dark. When the overhead light is on I kind of know it’s on so the indicator isn’t really useful in that case. :wink: )

I agree with you but I was told by the boss (aka my wife) that she prefers it the other way. Not difficult to change if we realize it makes more sense the other way around :slight_smile:

So second revision following the great advice of both @busman and @Didgeridrew

The toggle automation for any press on the button

- id: '1776177248679'
  alias: Office Lights Toggle
  description: ''
  use_blueprint:
    path: Jonybat/aqara_ws-k07e_event_entity.yaml
    input:
      event: event.h1_office_action
      helper_last_controller_event: input_text.h1_office_last_pressed
      helper_last_event_time: input_text.h1_office_last_pressed
      single_up:
      - action: light.toggle
        metadata: {}
        target:
          entity_id: light.office
        data: {}

And the LED sync using one of the light bulbs of the group

- id: '1776177409606'
  alias: Sync led h1 office
  description: ''
  triggers:
  - type: changed_states
    device_id: 3514bb7197b7b4a194ae514d0d1c20a2
    entity_id: 5318e3acfbe31fd53170f6b9b4684541
    domain: light
    trigger: device
  conditions: []
  actions:
  - choose:
    - conditions:
      - condition: device
        type: is_off
        device_id: 3514bb7197b7b4a194ae514d0d1c20a2
        entity_id: 5318e3acfbe31fd53170f6b9b4684541
        domain: light
      sequence:
      - action: switch.turn_off
        metadata: {}
        target:
          entity_id: switch.h1_office_led_indicator
        data: {}
    default:
    - action: switch.turn_on
      metadata: {}
      target:
        entity_id: switch.h1_office_led_indicator
      data: {}
  mode: single

Now how to create a blueprint which would output those 2 automations based on the right inputs ?

I don’t decouple my switches. I want them to work as normal switches if HA is down. I use the scene features like double-tap to do, well, scenes.

There are lots of ways of doing things, but in my automation with multiple triggers I then have this “configuration”:

actions:
  - variables:
      debug: false
      buttons:
        guest__001_KeyPressed:
          - light.toggle
          - light.family_table_light
        guest__002_KeyPressed:
          - light.toggle
          - light.family_table_light
        guest__001_KeyPressed2x:
          - light.toggle
          - light.family_room_cans
        guest__002_KeyPressed2x:
          - light.toggle
          - light.family_room_cans
        # Maybe better examples:
        craft__002_KeyPressed:
          - switch.turn_off
          - switch.craft_fan_power
        living_001_KeyPressed2x:
          - light.turn_on
          - light.living_room_cans
          - brightness_pct: 100
        living_001_KeyPressed3x:
          - scene.turn_on
          - scene.living_room_cozy
        living_002_KeyPressed2x_see_below:
          - scene.turn_on
          - scene.living_room_all_off
        wall_s_001_KeyPressed2x:
          - light.turn_on
          - light.wall_splash
          - brightness_pct: 100

which maps the events to what to do by looking at the trigger data and a bit of text replacement – just like @Hellis81 showed.

Thanks for sharing. I am not sure where I would put this though. You have a single automation handling all the switches in your house ? Triggered by pressing any button in your house and then you go to the right action based on the snippet you shared ?

Re: your point on not decoupling → How do you handle 2 way switches ? If someone turns off the relay and you want to turn the lights on by using the second switch which controls the same group of lights ?

Kind of, yes. But it’s just for the “special” case of when I use the scene feature of the switch (e.g. double or triple tap, hold-down, etc.). Initially I had an automation for everything and it just got to be too many to handle. As you noted you have to start repeating the same thing over and over.

In that snippet I was just showing how I use variables to define actions to run – more than just always on/off of a light.

You mean like if you have a z-wave or bluetooth battery operated button that you want to also run the lights? That’s why I use light.toggle, or am I not understanding your question?

BTW, I’ve reduced the number of small automations over time, and now do most things in packages as then I can group related things together (e.g. HVAC, irrigation, security) that have template sensors, automations and scripts. I think I use only one Blueprint.

Kind of, yes. But it’s just for the “special” case of when I use the scene feature of the switch (e.g. double or triple tap, hold-down, etc.). Initially I had an automation for everything and it just got to be too many to handle. As you noted you have to start repeating the same thing over and over.

Yes OK got you. So for the button which controls the relay you just use whatever was the last scene which was set on the lights the relay controls I guess.

In that snippet I was just showing how I use variables to define actions to run – more than just always on/off of a light.

My plan (which you’ve thrown a spanner in :wink: ) was to setup all the top buttons to do a simple on/off and then use the bottom buttons to do fancy cycling through scenes, change intensity by holding down etc. Now I’m not sure !

You mean like if you have a z-wave or bluetooth battery operated button that you want to also run the lights? That’s why I use light.toggle, or am I not understanding your question?

If you don’t decouple your switches and someone turns the relay off, your second z-wave switch becomes useless basically because it can’t reach the lights it’s supposed to turn on ? Maybe I can add a choice and if the relay is off, turn it on first before triggering the scene.

My setup is a bit weird in that, in my kitchen for example I have several switches which physically (through the relay) control only one portion of the lights I want to turn on/off. So i kind of have to decouple them otherwise I would have to go to 3 switches to turn all the lights in the kitchen on or off. Does that make sense ?

Hum. Isn’t the relay controllable via Home Assistant?

I use Zooz wall switches that can be wired as 3-way (or 4+ way) so my lights work just like they did before HA was added to the mix. I left it that way in a long hall where it’s important that it works if HA is down.

Other palces where I just wanted to add an additional switches I use a remove z-wave button and have it toggle the same light, scene, or group.

An automation won’t do anything without a trigger.
If this is the path you want, look into scripts, which are basically automations without triggers, but these have fields that you can pass data to them as a bonus.

1 Like

I use Zooz wall switches that can be wired as 3-way (or 4+ way) so my lights work just like they did before HA was added to the mix. I left it that way in a long hall where it’s important that it works if HA is down.

Yes I could have done that but I didn’t even plug them in as 2 ways, thinking I would just do one properly wired, controlling the relay, and the other side of the room a “pure” smart switch.

It’s only one area of the house though so I might have to live with the possibility that, when the server is down, some lights in this specific area won’t work anymore.

Thanks again for the insights, I have a clearer idea of what I want to do now and I won’t need any automation where the switch controls a set of lights which are all on the same relay.

I will just have now to figure out how to cycle through my Hue scenes for each room but that’s a topic for another thread :slight_smile: