Trying to condense all kitchen light triggers into single automation

I want to have my kitchen cabinet light strip time/event automations condense into one. I read the docs on using trigger. as a action template. But I am not quite sure how to implement it. Here is what I have so for, but check config is throwing errors. I am sure I have messed up the trigger action. Sorry for being a Noob, lol.

- alias: kitchen lights routine
  initial_state: true
  trigger:
    - platform: sun
      event: sunrise
      offset: "-00:10:00"
    - platform: time
      at: "09:00:00"
    - platform: time
      at: "17:00:00"
    - platform: sun
      event: sunset
    - platform: time
      at: "21:00:00"
  action:
    service: light.turn_on
    data_template:
      entity_id: light.kitchen_cabinet_lightstrip
      brightness: >
        {% if trigger.event == 'sunrise' %}
          255
        {% elif trigger.now == '09:00:00' %}
          255
        {% elif trigger.now == '17:00:00' %}
          255
        {% elif trigger.event == 'sunset' %}
          175
        {% elif triger.now == '21:00:00' %}
          75
        {% else %}
          []
        {% endif %}
      rgb_color: >
        {% if trigger.event == 'sunrise' %}
          [240,172,0]
        {% elif trigger.now == '09:00:00' %}
          [0,240,216]
        {% elif trigger.now == '17:00:00' %}
          [250,130,160]
        {% elif trigger.event == 'sunset' %}
          [150,215,250]
        {% elif trigger.now == '21:00:00' %}
          [85,95,205]
        {% else %}
          []
        {% endif %}
      transistion: 600

Check Config says: Invalid config for [automation]: [alias_state] is an invalid option for [automation]. Check: automation->alias_state. (See /config/configuration.yaml, line 15).

Exactly what the error said: remove the line

alias_state: true

There is no alias_state for an automation.

1 Like
- alias: kitchen lights routine
  alias_state: true      # <---------- ## Remove this. It is not a valid option ##

I’m curious to know what was the inspiration to include alias_state: true because I have never seen that documented anywhere.

Oh gosh, meant to be initial_state: true

Passes test now. Does the rest of the template logic look right?

Me coding when tired. Meant to be initial_state: true

I see.

I rarely ever use that ever since the bug, that sometimes caused automations to be turned off on startup, was fixed and that was many versions ago.

Also : - setting initial_state: true removes the last_triggered attribute (on a restart) so if you have automations that require an automation to have run greater or less than a specific time (ago), you won’t be able to and you will also need special parsing.

Good to know. Looks like, with that silly mistake fixed, the automation was triggered at 9 am but still didn’t successfully call the light change.

That’s because trigger.now is a DateTime object, not a string value. You can’t compare a DateTime object with a string. In other words, you cannot do this:

{% elif trigger.now == '09:00:00' %}

You can do this:

{% elif trigger.now.hour == 9 %}

In addition, I believe your rgb_color template will not work correctly. The output of a template is always a string value. That means even though this looks like a list
[250,128,132]
your template generates it as a string. The rgb_color option doesn’t accept strings.

You would have to put the template inside of the list. Either like this:

rgb_color: [{{"template for red}}", "{{template for green}}", "{{template for blue}}"]

or like this:

rgb_color: 
  - "{{template for red}}"
  - "{{template for green}}"
  - "{{template for blue}}"

Given the complexity of the automation’s trigger, the three templates would be equally complex.

Ok, bummer for simplicity, where would the quotes go for if templates?

"{% If trigger.now.hour = 9 %}
    255
 {% else %}  
   []
 {% endif %}"

No quotes if it is a multi line template.

You may want to research the “chooser” action from the upcoming 0.113 release before going too far with this. It seems that is meant to handle some of what you’re trying to do. I haven’t looked into it to see if it helps with your particular case though.

As an exercise, I created an automation that employs the new (in 0.113) choose option. Although I don’t currently have the means of testing it, I believe it uses the correct syntax required by choose. If anyone thinks it seems rather long and repetitive, you’re not wrong.

Click to reveal the automation
- alias: kitchen lights routine
  initial_state: true
  trigger:
    - platform: sun
      event: sunrise
      offset: "-00:10:00"
    - platform: time
      at: "09:00:00"
    - platform: time
      at: "17:00:00"
    - platform: sun
      event: sunset
    - platform: time
      at: "21:00:00"
    action:
      - choose:
        - conditions:
            - condition: template
              value_template: "{{ trigger.event is defined and trigger.event == 'sunrise' }}"
          sequence:
            service: light.turn_on
            data_template:
              entity_id: light.kitchen_cabinet_lightstrip
              brightness: 255
              rgb_color: [240,172,0]
              transition: 600
        - conditions:
            - condition: template
              value_template: "{{ trigger.now is defined and trigger.now.hour == 9 }}"
          sequence:
            service: light.turn_on
            data_template:
              entity_id: light.kitchen_cabinet_lightstrip
              brightness: 255
              rgb_color: [0,240,216]
              transition: 600
        - conditions:
            - condition: template
              value_template: "{{ trigger.now is defined and trigger.now.hour == 17 }}"
          sequence:
            service: light.turn_on
            data_template:
              entity_id: light.kitchen_cabinet_lightstrip
              brightness: 255
              rgb_color: [250,130,160]
              transition: 600
        - conditions:
            - condition: template
              value_template: "{{ trigger.event is defined and trigger.event == 'sunset' }}"
          sequence:
            service: light.turn_on
            data_template:
              entity_id: light.kitchen_cabinet_lightstrip
              brightness: 175
              rgb_color: [150,215,250]
              transition: 600
        - conditions:
            - condition: template
              value_template: "{{ trigger.now is defined and trigger.now.hour == 21 }}"
          sequence:
            service: light.turn_on
            data_template:
              entity_id: light.kitchen_cabinet_lightstrip
              brightness: 75
              rgb_color: [85,95,205]
              transition: 600

A benefit of performing the exercise is that it made me see a pattern that I can replicate using the current version of Home Assistant. All I need is to make five scripts, one for each of the five triggers and then the automation is reduced to this:

- alias: kitchen lights routine
  initial_state: true
  trigger:
    - platform: sun
      event: sunrise
      offset: "-00:10:00"
    - platform: time
      at: "09:00:00"
    - platform: time
      at: "17:00:00"
    - platform: sun
      event: sunset
    - platform: time
      at: "21:00:00"
  action:
    service_template: >
      {% set name = trigger.event if trigger.event is defined else trigger.now.hour %}
      script.kitchen_lightstrip_{{name}}

Here are the five scripts:

#script:
  kitchen_lightstrip_sunrise:
    sequence:
      service: light.turn_on
      data:
        entity_id: light.kitchen_cabinet_lightstrip
        brightness: 255
        rgb_color: [240,172,0]
        transition: 600

  kitchen_lightstrip_sunset:
    sequence:
      service: light.turn_on
      data:
        entity_id: light.kitchen_cabinet_lightstrip
        brightness: 175
        rgb_color: [150,215,250]
        transition: 600

  kitchen_lightstrip_9:
    sequence:
      service: light.turn_on
      data:
        entity_id: light.kitchen_cabinet_lightstrip
        brightness: 255
        rgb_color: [0,240,216]
        transition: 600

  kitchen_lightstrip_17:
    sequence:
      service: light.turn_on
      data:
        entity_id: light.kitchen_cabinet_lightstrip
        brightness: 255
        rgb_color: [250,130,160]
        transition: 600

  kitchen_lightstrip_21:
    sequence:
      service: light.turn_on
      data:
        entity_id: light.kitchen_cabinet_lightstrip
        brightness: 75
        rgb_color: [85,95,205]
        transition: 600

It fails to meet the original requirement of having “everything in one automation” but, given the automation’s triggering requirements (and need for setting rgb_color), that’s currently impractical. It will become practical when choose is available.

Per a few sugestions, I waited till the 0.113 and it seems well worth it. So far the below code seems to be working as I hoped it would to condense a day long light automation schedule into ONE automation.

- alias: kitchen lightstrip routine
  trigger:
    - platform: sun
      event: sunrise
      offset: "-00:10:00"
    - platform: time
      at: "09:00:00"
    - platform: time
      at: "17:00:00"
    - platform: sun
      event: sunset
    - platform: time
      at: "21:00:00"
  mode: restart
  action:
    - choose:
      - conditions:
          - condition: template
            value_template: >
              {{ trigger.event == sunrise }}
        sequence:
          - service: light.turn_on
            data:
              entity_id: light.kitchen_cabinet_lightstrip
              brightness: 175
              rgb_color:
                - 245
                - 200
                - 0
              transition: 600
      - conditions:
          - condition: template
            value_template: >
              {{ trigger.now.hour == 9 }}
        sequence:
          - service: light.turn_on
            data:
              entity_id: light.kitchen_cabinet_lightstrip
              brightness: 175
              rgb_color:
                - 0
                - 245
                - 225
              transition: 600
      - conditions:
          - condition: template
            value_template: >
              {{ trigger.now.hour == 17 }}
        sequence:
          - service: light.turn_on
            data:
              entity_id: light.kitchen_cabinet_lightstrip
              brightness: 255
              rgb_color:
                - 255
                - 255
                - 255
              transition: 600
      - conditions:
          - condition: template
            value_template: >
              {{ trigger.event == sunset }}
        sequence:
          - service: light.turn_on
            data:
              entity_id: light.kitchen_cabinet_lightstrip
              brightness: 175
              rgb_color:
                - 245
                - 200
                - 0
              transition: 600
      - conditions:
          - condition: template
            value_template: >
              {{ trigger.now.hour == 21 }}
        sequence:
          - service: light.turn_on
            data:
              entity_id: light.kitchen_cabinet_lightstrip
              brightness: 75
              rgb_color:
                - 10
                - 0
                - 245
              transition: 600

How is this significantly different from what I had suggested? :thinking:

1 Like

It’s not. I have always found it helpful when people copy the working code back into their thread after it’s been fixed, that way future rubberneckers can benefit as well :+1:

Fair enough, so what was fixed?

What I noticed is you changed RGB color values (cosmetic choice) and eliminated the is defined test (not sure why; it’s beneficial).

Well first off the big fix is having .113 at all, as prior to that, when I posted my question it wasn’t an option. I also added the mode: restart which is a nice feature in case two of my times clash. A fixed time and sunset as an example.

As for the “test.” Honestly I tried to understand (as an amateur here) why it could help. What’s the circumstance it would help prevent?

Both examples use choose, a feature introduced in 0.113. My question was how did your example, that uses choose, “fix” the example I posted, that also uses choose?

The odds of sunset being exactly 17:00:00 are low. Even if it were to happen, the end-result wouldn’t be much different if the mode is single or restart. It’s a coin toss which one of the two identical triggers would occur first. In one case (single), the first of the two to trigger will determine the light’s final state and in the other (restart) the second of the two will. Nevertheless, the outcome of either scenario is governed by which of the two identical triggers Home Assistant handles first.