Change only the lights that are currently on. Template Sensor maybe?

Okay so this may be a bit confusing so I’m going to try and be clear.
Example code will be inserted at the end of the post.

Here’s my use case.

I’ve manually made automations for circadian lights. I’m in the process of cleaning things up using templates and template sensors.

I have 5 entities that when they turn on, the brightness and color_temp are brought in based on template sensors that change every half hour or so throughout the day. I think I have this part working (haven’t done the real world testing yet, but it’s looking good in the template editor). See code below Fig. A

I have another automation for the overnight times because the template sensor has an RGB value from 23:01 to 05:00 for rgb_color instead of color_temp (in case you’re wondering about the time condition)

This works great (or should) if the light is turning from off to on, but doesn’t help when the light is already on when it goes from, for example: 16:59 to 17:00. I would like it to transition to the new value.

In my old version manual automations, each entity and time frame had it’s own automation (19 automations per entity…don’t judge…I’m trying to make things a bit sexier now).
Fig. B below

So you can see that when the light turns on and the time condition is met, it turns on to the specified brightness/temp
If it’s already on and the time trigger is met, then the light will transition from the old brightness/temp to the new one.

This was a TON of code, but worked surprisingly well.
So I’m looking for a template or automation or script or combination to recreate that time trigger. What I have so far is the last bit at the end of this post… Fig. C

As I type this, I’m thinking that maybe a template sensor may be possible. One that has the entities that are currently in the on state and could be brought into the entity_id: of the action. Is that possible?

Thanks in advance for any help with this.

#####################Example Code###################

This code should work for when any of the entities turn on

Fig. A
- id: circadian_day
  alias: Circadian Day On Trigger
  initial_state: true
  trigger:
  - platform: state
    entity_id: light.lamp
    from: 'off'
    to: 'on'
  - platform: state
    entity_id: light.desk_lamp
    from: 'off'
    to: 'on'
  - platform: state
    entity_id: light.sengled_cabinet_strip
    from: 'off'
    to: 'on'
  - platform: state
    entity_id: light.dining_bulb
    from: 'off'
    to: 'on'
  - platform: state
    entity_id: light.back_hall_lights
    from: 'off'
    to: 'on'
  condition:
  - condition: time
    before: '23:01:00'
    after: '04:59:00'
  - condition: state
    entity_id: input_boolean.circadian_on
    state: 'on'
  action:
  - service: light.turn_on
    data_template:
      entity_id: "{{ trigger.entity_id }}"
      brightness: "{{ states('sensor.cl_brightness') }}"
      color_temp: "{{ states('sensor.cl_color_temp') }}"
      transition: '5'

##################Example of old circadian automations############

Fig. B
- id: cl6a_desk_lamp
  alias: 6am desk_lamp
  initial_state: true
  trigger:
  - platform: state
    entity_id: light.desk_lamp
    from: 'off'
    to: 'on'
  - platform: time
    at: '06:01:00'
  condition:
  - condition: time
    after: '06:01:00'
    before: '06:30:00'
  - condition: state
    entity_id: light.desk_lamp
    state: 'on'
  - condition: state
    entity_id: input_boolean.circadian_on
    state: 'on'
  action:
  - service: light.turn_on
    data_template:
      entity_id: light.desk_lamp
      brightness_pct: 25
      color_temp: 450
      transition: '5'

################Beginning of the time trigger automation################

Here’s what I have so far, but not even sure if it’s worth anything

Fig. C
- id: circadian_time_transition
  alias: Circadian Time Transition
  initial_state: true
  trigger:
  - platform: time
    at: '05:00:00'
  - platform: time
    at: '06:00:00'
  - platform: time
    at: '06:30:00'
  - platform: time
    at: '07:00:00'
  - platform: time
    at: '07:30:00'
  - platform: time
    at: '08:00:00'
  - platform: time
    at: '09:00:00'
  - platform: time
    at: '16:30:00'
  - platform: time
    at: '17:00:00'
  - platform: time
    at: '17:30:00'
  - platform: time
    at: '18:00:00'
  - platform: time
    at: '18:30:00'
  - platform: time
    at: '19:00:00'
  - platform: time
    at: '19:30:00'
  - platform: time
    at: '20:00:00'
  - platform: time
    at: '20:30:00'
  - platform: time
    at: '21:00:00'
  - platform: time
    at: '22:00:00'
  - platform: time
    at: '22:30:00'
  condition:
  - condition: state
    entity_id: input_boolean.circadian_on
    state: 'on'
  action:
  - service: light.turn_on
    data_template:
      entity_id: #########need only the specified entities that are currently on. 
##########################Template sensor maybe?
      brightness: "{{ states('sensor.cl_brightness') }}"
      color_temp: "{{ states('sensor.cl_color_temp') }}"
      transition: '15'

I may be going about this the wrong way and am open to new ideas too. I’ve tried the Flux component and the Circadian Lighting Custom component. I’m not interested in using them.
Thanks!

  condition:
  - condition: state
    entity_id: input_boolean.circadian_on
    state: 'on'
  - condition: template
    value_template: >
      {% set lights = ['light.dining_bulb', ...] %}
      {{ expand(lights)|selectattr('state','eq','on')|list|length > 0 }}
  action:
  - service: light.turn_on
    data_template:
      entity_id: >
        {% set lights = ['light.dining_bulb', ...] %}
        {{ expand(lights)|selectattr('state','eq','on')
           |map(attribute='entity_id')|join(',') }}
      brightness: "{{ states('sensor.cl_brightness') }}"
      color_temp: "{{ states('sensor.cl_color_temp') }}"
      transition: 15

Instead of listing all the lights in each of the two templates you could also create a group that contains the lights you want this to operate on and then use that instead:

{{ expand('group.XXX')|selectattr... }}

Of if you want to operate on ALL lights, then:

{{ states.light|selectattr... }}
1 Like

I never would have figured those out on my own…haha.

I was worried about creating a group specifically because if let’s say the dining room light is on and the lamp isn’t, then I thought setting the group to x brightness and x temp would actually turn on the lamp and any other lights in the group that weren’t already on. I am specifically trying to avoid this.
However, I am willing to try it out as it would make this much cleaner and would make it easier to potentially add specific areas/rooms for circadian lighting independent of other areas/rooms. That’s a long way down the road for now, but nice to lay the groundwork.

Implementing tonight and testing tomorrow…

Another quick question: Having all those triggers…while I know will work…seems messy. Is there a slicker way to express that? Since it’s not every hour or even every 30 min, I don’t know of a sexy way to do it. Am I missing something or is this pretty much the way?

Edit: could I express the different times as a list?

- platform: time
  at: 05:00:00, 06:00:00, ...

But that’s not what this would do. It uses the group to decide which lights to check. It would still only use the ones that are on (of those, as opposed to of all the lights.) That’s what |selectattr('state','eq','on') does.

Given the inconsistency/irregularity of the times, that’s probably the best way. But, if you wanted, if you configured sensor.time, then you could do something like:

trigger:
- platform: template
  value_template: >
    {% set times = ["05:00", "06:00", "06:30", ...] %}
    {{ states('sensor.time') in times }}

I love it!
I have the group configured now and will hopefully have a chance to test this out tomorrow. I’m assuming this will probably work so I’ll mark this as the solution after I test it out.
I really appreciate your help.

1 Like

So far so good! After a few days of testing and living normal life, the lights are working as I had hoped. Thank you so much for your help!!

1 Like

Auto.neub, could you share the final code used? I want to create a template group based on the on state of a light group. This so that I can adjust the brightness of this selected group only (and not turn on the light that are off). Thanks!