Help with condensing automations

I’m new to Home Assistant, and it’s been quite a challenge getting up and running compared to other home automation platforms. I’ve been able to make some progress on a somewhat advance automation. It works, but one thing that bothers is the growing number of automations in my automation list. Take this for example:

- id: '1'
  alias: Master Bath- Someone Is Sleeping (Turn On)
  trigger:
    platform: state
    entity_id: light.master_bath_vanity_lights
  condition:
    condition: template
    value_template: "{{ trigger.from_state is not none and\n   trigger.from_state.state\
      \ == 'on' and\n   trigger.to_state\
      \ is not none and\n   trigger.to_state.attributes.brightness > 254 and\n   trigger.to_state.state == 'on' }}\n"
  action:
  - service: switch.turn_off
    target:
      entity_id: switch.someone_is_sleeping_virtual_switch
- id: '2'
  alias: Master Bath- Someone Is Sleeping (Turn Off)
  trigger:
    platform: state
    entity_id: light.master_bath_vanity_lights
  condition:
    condition: template
    value_template: "{{ trigger.from_state is not none and\n   trigger.from_state.state\
      \ == 'on' and\n   trigger.from_state.attributes.brightness > 254 and\n   trigger.to_state\
      \ is not none and\n   trigger.to_state.state == 'off' }}\n"
  action:
  - service: switch.turn_on
    target:
      entity_id: switch.someone_is_sleeping_virtual_switch

I’m using the same trigger twice. How can I condense this down to a single automation using an if statement?

Trigger on the light state (one automation is to ‘off’, the other to ‘on’) then use conditions.

You want your triggers to be as specific as possible.

Also, what is with all the new line characters in your templates?

Thanks for the reply. I’m trying to avoid multiple instances of the same trigger, if possible. I’m also trying to learn if statements in HA, but I’m not sure how I would do it using these value templates. May be more aggravation than it’s worth, but HA has been all baby steps anyway. So I’m really not sure at this point, lol.

The new line characters were automatically formatted by the editor. The original code just had each part of the value template on its own line.

What is the goal of the first automation’s condition? Basically it’s looking for a state-change from on to on and a brightness of 255. That happens only if a dimmed light’s brightness is changed to maximum. Is that the goal?

The second automation’s condition is clearer; it’s looking for a state-change from on to off` but only if the light’s brightness was at maximum.

The use case is a light that turns on and off by motion. Normally, this light turns on to (near) full brightness. If either my wife or I are sleeping, I want the light to turn on to 20%. In Hubitat, I could detect Lutron physical button press as a trigger, but that feature is not available through the HA Lutron integration.

So I’m trying to recreate that feature by having the motion sensor turn the light on to 99% brightness. That way I know if it’s at full brightness (255), it’s because the switch was pressed. Same with turning it off.

All of this works as expected, and the virtual switch toggles as necessary. I use that switch in another automation that actually turns on/off the light.

I would just like to consolidate these two automations into one, more for clutter control in the UI more than anything. Right now, there are 5 automations controlling one light. It just seems excessive. I’m pretty sure I can figure out the basic motion sensor ones, but templates confuse me.

So I left out the part you actually asked, lol. The condition is for when my wife is getting ready in the morning. She walks in the bathroom, light comes on to 20%. She can then press the physical light switch on the wall to turn off the “someone is sleeping” switch. Lights go to full brightness. When she’s done, she presses the off switch. “Someone is sleeping” turns back on and the light will turn on at 20% on next motion event.

This is a consolidated version of the two automations. I believe it retains the relevant logic but you should double-check it (EDIT: the revised version employs choose to handle the specific conditions).

- id: 'whatever'
  alias: Master Bath - Someone Is Sleeping
  trigger:
  - platform: state
    entity_id: light.master_bath_vanity_lights
  condition:
  - condition: template
    value_template: "{{ trigger.from_state.state == 'on' }}"
  action:
  - choose:
    - conditions: "{{ trigger.to_state.attributes.brightness > 254 and trigger.to_state.state == 'on' }}"
      sequence:
      - service: switch.turn_off
        target:
          entity_id: switch.someone_is_sleeping_virtual_switch
    - conditions: "{{ trigger.from_state.attributes.brightness > 254 and trigger.to_state.state == 'off' }}"
      sequence:
      - service: switch.turn_on
        target:
          entity_id: switch.someone_is_sleeping_virtual_switch

Thanks. I’ll try this out.

I’m a little short on time to troubleshoot, but it appears the trigger.to_state.attributes.brightness variable isn’t triggering properly. I modified the logic to test the rest of the code and it works. I will see if I can try to figure out what’s going on tomorrow.

Strange because it’s used in a condition, much like it was in your original automation.

I might be doing something wrong. Basically had time to copy and paste. It didn’t work so I set the condition to work off the switch only trigger.to_state.state == 'on' & trigger.to_state.state == 'off' and it worked. I’ll do some more digging tomorrow. It looks like it should work to me too.

Here’s the working code, for anyone else. I might be wrong here, but I think the issue was declaring conditions without actually adding a condition? I’m not for sure why Taras’ code didn’t work. Anyway, I had to explicitly declare each condition’s value template.

- id: 'whatever'
  alias: Master Bath - Someone Is Sleeping
  trigger:
  - platform: state
    entity_id: light.master_bath_vanity_lights
  condition:
  - condition: template
    value_template: "{{ trigger.from_state.state == 'on' }}"
  action:
  - choose:
    - conditions: 
      - condition: template
        value_template: "{{ trigger.to_state.state == 'on' and trigger.to_state.attributes.brightness > 254 }}"
      sequence:
      - service: switch.turn_off
        target:
          entity_id: switch.someone_is_sleeping_virtual_switch
    - conditions: 
      - condition: template
        value_template: "{{ trigger.to_state.state == 'off' and trigger.from_state.attributes.brightness > 254 }}"
      sequence:
      - service: switch.turn_on
        target:
          entity_id: switch.someone_is_sleeping_virtual_switch

A big thanks to Taras for pointing me in the right direction.

1 Like

My example used Template Conditions in Shorthand Notation. It’s functionality equivalent to the traditional, longhand version (provided you are using a recent version of Home Assistant).

It’s the custom of this community to assign the Solution tag to the first post that either introduces or explains the concept that resolves the problem. The real work here was untangling the conditional logic and simplifying it with a choose. Check other solved topics and you’ll see how the tag is used.

Whether shorthand (which I use in all of my automations) or longhand, they are equivalent. Why shorthand notation allegedly failed for you is the subject of a separate investigation; other users should not now assume that longhand notation is somehow the better choice.

I can mark your post as the solution if you’re sure the syntax is correct. I didn’t want to mark non-working code as a solution.

Any ideas on why the shorthand code isn’t working then? Home Assistant version sensor shows 2021.3.4 if that helps.

The shorthand syntax is correct. Here’s an example taken from one of my automations:


  - choose:
    - conditions: "{{ is_state(light, 'off') and trigger.to_state.state == 'on' and states(luminance) | int < 15 }}"
      sequence:

Why shorthand notation is not working for you is the subject of a separate investigation.

I have marked your answer as the solution. I will revisit this at a later time. Thanks for the help!

Just out of curiosity, should the brightness in the code you provided be cast as an integer like the snippet from your automation for luminance? The code works, just not the brightness portion.

Whereas an entity’s state value is always a string, attributes can have a type other than string. In this case, brightness is an integer (so there’s no need to explicitly cast it to an integer).

Thanks. That clears that up. I will go through the documentation again. It’s clear I need to do more reading.

For what it’s worth, Taras’ code above is working now. Not sure if it was the latest update or if I had an unflagged error in the editor. The shorthand definitely helps with code readability. Again, thanks for the help.

1 Like