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?
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 allstate_changed events produced by your system so this approach isn’t normally recommended.
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' }}
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
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.