Template Help for date and times

I know there is a better way to perform this automation based on the templates I have seen, but I have had problems putting it together. So I thought I would show how it is working now with a lot of triggers.

alias: Living Room - Recess Lights On/Off/Brightness
description: >-
  Turn on Media Can Lights on and off based on TV state and adjust the
  brightness
trigger:
  - platform: time
    id: 25% @ Home
    at: "08:00:00"
    alias: 25% @ Home
  - platform: time
    at: "22:00:00"
    id: time off early scheduled
    alias: time off early scheduled
  - platform: time
    at: "23:00:00"
    id: time off late
    alias: time off late
  - platform: time
    at: "17:00:00"
    id: 50% @ Work
    alias: 50% @ Work
  - platform: time
    at: "11:00:00"
    id: 50% home
    alias: 50% home
  - platform: time
    at: "13:00:00"
    id: 75% home
    alias: 75% home
  - platform: time
    at: "19:00:00"
    id: 25% @ Work
    alias: 25% @ Work
  - platform: state
    entity_id:
      - media_player.livingroomstereo
    to: "off"
    id: Stereo turned off
  - platform: time
    at: "23:59:59"
    alias: Off Midnight
    id: Off Midnight
condition:
  - condition: state
    entity_id: input_boolean.automations_kill_switch
    state: "off"
  - condition: state
    entity_id: person.brad
    state: home
action:
  - choose:
      - conditions:
          - condition: trigger
            id: 25% @ Home
        sequence:
          - service: light.turn_on
            data:
              brightness_pct: 25
            target:
              entity_id:
                - light.wled_lr_left_3
                - light.wled_lr_right_2
                - light.wled_lrtop
          - service: select.select_option
            data:
              option: Default
            target:
              entity_id:
                - select.wled_lr_left_preset_3
                - select.wled_lr_right_preset_2
                - select.wled_lrtop_preset
      - conditions:
          - condition: trigger
            id: 50% @ Work
        sequence:
          - service: light.turn_on
            data:
              brightness_pct: 50
            target:
              entity_id:
                - light.wled_lr_left_3
                - light.wled_lr_right_2
                - light.wled_lrtop
      - conditions:
          - condition: trigger
            id: time off early scheduled
        sequence:
          - service: light.turn_off
            data: {}
            target:
              entity_id:
                - light.wled_lr_left_3
                - light.wled_lr_right_2
                - light.wled_lrtop
      - conditions:
          - condition: trigger
            id: time off late
        sequence:
          - service: light.turn_off
            data: {}
            target:
              entity_id:
                - light.wled_lr_left_3
                - light.wled_lr_right_2
                - light.wled_lrtop
      - conditions:
          - condition: trigger
            id: 50% home
        sequence:
          - service: light.turn_on
            data:
              brightness_pct: 50
            target:
              entity_id:
                - light.wled_lr_left_3
                - light.wled_lr_right_2
                - light.wled_lrtop
      - conditions:
          - condition: trigger
            id: 25% @ Work
        sequence:
          - service: light.turn_on
            data:
              brightness_pct: 25
            target:
              entity_id:
                - light.wled_lr_left_3
                - light.wled_lr_right_2
                - light.wled_lrtop
          - service: select.select_option
            data:
              option: Default
            target:
              entity_id:
                - select.wled_lr_left_preset_3
                - select.wled_lr_right_preset_2
                - select.wled_lrtop_preset
      - conditions:
          - condition: trigger
            id: 75% home
        sequence:
          - service: light.turn_on
            data:
              brightness_pct: 75
            target:
              entity_id:
                - light.wled_lr_left_3
                - light.wled_lr_right_2
                - light.wled_lrtop
      - conditions:
          - condition: trigger
            id: Stereo turned off
        sequence:
          - service: light.turn_off
            data: {}
            target:
              entity_id:
                - light.wled_lr_left_3
                - light.wled_lr_right_2
                - light.wled_lrtop
      - conditions:
          - condition: trigger
            id: Stereo turned off
        sequence:
          - service: light.turn_off
            data: {}
            target:
              entity_id:
                - light.wled_lr_left_3
                - light.wled_lr_right_2
                - light.wled_lrtop
      - conditions:
          - condition: or
            conditions:
              - condition: trigger
                id: time off late
              - condition: trigger
                id: Off Midnight
        sequence:
          - service: light.turn_off
            data: {}
            target:
              entity_id:
                - light.wled_lr_left_3
                - light.wled_lr_right_2
                - light.wled_lrtop
mode: single

There are numerous ways to compact an automation using templates and combining triggers and actions.

Here’s one way to reorganize your automation using the methods above…

trigger:
  - platform: time
    at: "13:00:00"
    id: '75'
  - platform: time
    at: 
      - "11:00:00"
      - "17:00:00"
    id: '50'
  - platform: time
    at: 
      - "08:00:00"
      - "19:00:00"
    id: '25'
  - platform: state
    entity_id:
      - media_player.livingroomstereo
    to: "off"
    id: 'off'
  - alias: Timed Off
    platform: time
    at: 
      - "22:00:00" 
      - "23:00:00" 
      - "23:59:59"
    id: 'off'
condition:
  - condition: state
    entity_id: input_boolean.automations_kill_switch
    state: "off"
  - condition: state
    entity_id: person.brad
    state: home
action:
  - variables:
      lights: "{{ [ 'light.wled_lr_left_3', 'light.wled_lr_right_2', 'light.wled_lrtop'] }}"
  - choose:
      - conditions:
          - condition: trigger
            id: 'off'
        sequence:
            - service: light.turn_off
              data: {}
              target:
                entity_id:  "{{ lights }}"
    default:
      - service: light.turn_on
        data:
          brightness_pct: "{{ trigger.id | int }}" 
        target:
          entity_id:  "{{ lights }}"
      - condition: trigger
        id: '25'
      - service: select.select_option
        data:
          option: Default
        target:
          entity_id:
            - select.wled_lr_left_preset_3
            - select.wled_lr_right_preset_2
            - select.wled_lrtop_preset        

There are other ways to tackle the same automation using more sophisticated templating.

Action block without Choose
...
action:
  - service: light.turn_{{ 'on' if trigger.id != 'off' else 'off' }}
    data: '{{ {"brightness_pct": trigger.id | int } if trigger.id != "off" else {} }}'
    target:
      entity_id:
        - light.wled_lr_left_3
        - light.wled_lr_right_2
        - light.wled_lrtop
  - condition: trigger
    id: '25'
  - service: select.select_option
    data:
      option: Default
    target:
      entity_id:
        - select.wled_lr_left_preset_3
        - select.wled_lr_right_preset_2
        - select.wled_lrtop_preset        

From what I understand, there usually isn’t going to be a huge difference in performance with changes like these, so use whichever one is most understandable to you. If you’re not adept at doing so, complex templates can make an automation just as unreadable as having a lot of repeating components. There’s a balance to be struck between the two.

1 Like

Thanks! I knew there was a better way. Let me see if I can get that in. Appreciate the help!