Have all lights reference Template for color automatically

I have created the below Script. This works for one light. Without having to add the template to every light in Automations, Scripts, etc., is there a way I can create a Template for color and automatically have every light reference it?

Maybe the way to do this is create a triggered-based Template and listen for any light turning on? If yes, how may that look?

alias: Office Desk Lamp Holiday Colors
sequence:
  - service: light.turn_on
    data:
      brightness_pct: 100
      color_name: >
        {% set holidays = { '09-29': 'green', '07-01': 'blue',
                      '10-31': 'orange', '12-25': 'red',
                      '01-01': 'yellow' } %}
        {% set today = (now().date()|string)[5:] %} {{ holidays[today] if today
        in holidays.keys() else 'white' }}
    target:
      device_id: 49dc7efaec0941a1a9a0470e8424c3f7
mode: single

Reusing templates

No.

  • State Trigger can only listen for changes to entities that you explicit identify.

  • Event Trigger can listen for a state_changed event_type and then filter the events for the ones that involve a light entity. However, that means it will trigger for all state_changed events produced by your system so this approach isn’t normally recommended.

Thank you. Bummed though that I will need to cite it in every Script manually.

With regards to the ‘custom’ folder… I already use a templates.yaml… Can I just use that, or do I need to actually create the custom folder, etc?

I might just be showing my age, but would you really want every light in the house to turn on white, then turn to green every time you turn it on throughout the day… just to have to turn it to back to white? That seems like it would get old real fast. You might also want to consider a time condition, so the members of the household are greeted by bright, colorful lights if they get up for a glass of water in the middle of the night.

No, in order to be globally available, template macros needs to be in a .jinja file within the custom_templates folder.

As with anything in HA, there are many ways to accomplish a goal…

Option 2: Make a template sensor to hold the color value
template:
  - trigger:
      - platform: time
        at: "00:01:00"
      - platform: homeassistant
        event: start
    sensor:
      - name: Holiday Light Color
        state: |
         {% set holidays = { '09-29': 'green', '07-01': 'blue',
         '10-31': 'orange', '12-25': 'red', '01-01': 'yellow' } %}
         {% set today = now().strftime('%m-%d') %} 
         {{ holidays[today] if today in holidays.keys() else 'white' }}

Ok, got it, regarding the template macros.

As for the light color… The color would simply be for the holiday, which was my intension. If you’re referencing the syntax is not correct, I’m all ears. However, you do bring up a valid point… Maybe not all lights be green, that does get a bit annoying :grinning:

What would be the setup for this?
Would I need a sensor for each color?
How would I then check for the specific date?

I’ve seen since other setups in other threads but they look really detailed and confusing.

Click on the option in my post above to show the sensor config.

1 Like

I’m not understanding the syntax of the macro. I’m stuck on what the section to add to automations should look like.

Macro (config/custom_templates/holiday_lights.jinja):

{% macro format_entity(entity_id) %}
{% set holidays = { '10-02': 'green', '07-01': 'blue',
         '10-31': 'orange', '12-25': 'red', '01-01': 'yellow' } %}
         {% set today = now().strftime('%m-%d') %} 
         {{ holidays[today] if today in holidays.keys() }}
{% endmacro %}

Add to Automations/Scripts:

{% from 'holiday_lights.jinja' import format_entity %}
{{ format_entity('sensor.??') }}

Since you aren’t actually using any input arguments, you don’t really need an actual macro.

“Macro” (config/custom_templates/holiday_lights.jinja):

{% set _holidays = 
{ '10-02': 'green', '07-01': 'blue',
'10-31': 'orange', '12-25': 'red', 
'01-01': 'yellow' } %}

{% set _today = now().strftime('%m-%d') %}

{% set holiday_light_color = _holidays[_today] if _today in _holidays.keys() else 'white' %}

Automation

alias: Office Desk Lamp Holiday Colors
sequence:
  - service: light.turn_on
    data:
      brightness_pct: 100
      color_name: >
        {% from 'holiday_lights.jinja' import holiday_light_color %}
        {{ holiday_light_color }}
    target:
      device_id: 49dc7efaec0941a1a9a0470e8424c3f7
mode: single

If I understand correctly, I’m simply creating a template now to use instead? If yes, could this be moved back into my templates.yaml file?

What would this be then, or probably not need at all?

It cannot be moved to templates.yaml as-is. You would need to configure it as a template sensor and adjust you automation’s template accordingly.

Instead of:

    color_name: >
      {% from 'holiday_lights.jinja' import holiday_light_color %}
      {{ holiday_light_color }}

If you used the template sensor shown as Option 2 in my post above, it would be:

    color_name: "{{ states('sensor.holiday_light_color') }}"

color_name: “{{ states(‘sensor.holiday_light_color’) }}”

I’m having trouble adding this to my existing scripts via ‘Manually fire event’. I place this code in the Event data… what Event type would this be, if this is the proper approach.

Are you referencing this:

That is the only reference to Events I can find in this thread…

Post one of the scripts in question so we don’t have to guess about what you are trying to do.

This is the template I have… I don’t need the trigger, I just want to reference this in my scripts.

Edit: I may want to add… I only want to change the light color if holiday, otherwise ignore this template (not else white)…

- sensor:
  - name: Holiday Light Color
    state: |
      {% set holidays = { '09-29': 'green', '07-01': 'blue',
      '10-31': 'orange', '12-25': 'red', '01-01': 'yellow' } %}
      {% set today = now().strftime('%m-%d') %} 
      {{ holidays[today] if today in holidays.keys() else 'white' }}

Add this to the scripts?

color_name: "{{ states('sensor.holiday_light_color') }}"

Here’s one of my scripts…

alias: Living Room - Set Scene
sequence:
  - condition: and
    conditions:
      - condition: numeric_state
        entity_id: zone.home
        above: 0
      - condition: state
        entity_id: binary_sensor.sleep_state
        state: "off"
  - choose:
      - conditions:
          - condition: sun
            after: sunset
        sequence:
          - service: scene.turn_on
            target:
              entity_id: scene.living_room_night
            metadata: {}
      - conditions:
          - condition: sun
            after: sunrise
        sequence:
          - service: scene.turn_on
            target:
              entity_id: scene.living_room_day
            metadata: {}
    default:
      - service: light.turn_on
        data:
          brightness_pct: 50
        target:
          device_id: 61ce75c8c194fc5bf517be3cc7e0a6aa
    alias: Determine Day or Night

Something like the following…

alias: Living Room - Set Scene
sequence:
  - condition: numeric_state
    entity_id: zone.home
    above: 0
  - condition: state
    entity_id: binary_sensor.sleep_state
    state: "off"
  - choose:
      - alias: If it's a holiday
        conditions:
          - not:
              - condition: state
                entity_id: sensor.holiday_light_color
                state: white
        sequence:
          - service: light.turn_on
            data:
              brightness_pct: 100
              color_name: "{{ states('sensor.holiday_light_color') }}"
            target:
              device_id: 61ce75c8c194fc5bf517be3cc7e0a6aa
      - conditions:
          - condition: sun
            after: sunset
        sequence:
          - service: scene.turn_on
            target:
              entity_id: scene.living_room_night
            metadata: {}
      - conditions:
          - condition: sun
            after: sunrise
        sequence:
          - service: scene.turn_on
            target:
              entity_id: scene.living_room_day
            metadata: {}
    default:
      - service: light.turn_on
        data:
          brightness_pct: 50
        target:
          device_id: 61ce75c8c194fc5bf517be3cc7e0a6aa
    alias: Determine Day or Night

If you want it to affect all lights in the living room you can use area_id instead of device_id as the target.

Service Calls: Targeting Areas

Thanks for all the input @Didgeridrew … This works great!