Change Light Profiles without turning lights on

I am scratching my head a bit with this. I have an automation in place used to set lights to a certain profile, using the input_select for the area as the trigger.
This works fine, except the lights will turn on, even if they are already off. This was not an issue for purely manual operation, but I want to automate changing of the input select option to change the profile depending on the time of day. In this situation, I want lights that are off to remain off, even when the profile is changed.
I am struggling with a condition that will achieve this, given the automation is templated to control 4 light profiles in 4 light groups from the one automation.
Any advice is appreciated.
Here is the current automation:

- id: '201901022123'
  alias: 'Change Light Profile'
  trigger:
    - platform: state
      entity_id: input_select.livingroom_light_profile, input_select.meals_light_profile, input_select.kitchen_light_profile, input_select.bedroom_light_profile
  condition:
    condition: and
    conditions:
      - condition: or
        conditions:
          - condition: state
            entity_id: 'device_tracker.********'
            state: 'home'
          - condition: state
            entity_id: 'device_tracker.********'
            state: 'home'
      - condition: template
        value_template: >
          {% if is_state('input_select.house_mode', 'Movie') %}
            false
          {% else %}
            true
          {% endif %}
      - condition: state
        entity_id: input_boolean.house_automatic
        state: 'on'
  action:
    - service: light.turn_on
      data_template:
        entity_id: >
          {% if trigger.entity_id == 'input_select.livingroom_light_profile' %} 
            {{ "light.living_room_ceiling" }}
          {% elif trigger.entity_id == 'input_select.meals_light_profile' %}
            {{ "light.meals_area_ceiling" }}
          {% elif trigger.entity_id == 'input_select.kitchen_light_profile' %}
            {{ "light.kitchen_ceiling" }}
          {% elif trigger.entity_id == 'input_select.bedroom_light_profile' %}
            {{ "light.bedroom_ceiling" }}
          {% endif %}
        profile: >
          {% if trigger.entity_id == 'input_select.livingroom_light_profile' %} 
            {{ states('input_select.livingroom_light_profile') }}
          {% elif trigger.entity_id == 'input_select.meals_light_profile' %}
            {{ states('input_select.meals_light_profile') }}
          {% elif trigger.entity_id == 'input_select.kitchen_light_profile' %}
            {{ states('input_select.kitchen_light_profile') }} 
          {% elif trigger.entity_id == 'input_select.bedroom_light_profile' %}
            {{ states('input_select.bedroom_light_profile') }}            
          {% endif %}    

Edit: tried to fix formatting of the code.

So, i do not know what a light profile is but I can help you correct the code as to what I think you are trying to achieve. There are a few problems that I see arrising here.

I’m assuming you want the lights to turn on with the current profile activated? I.E. when you press the switch manually. To achieve this, you’ll end up seeing the lights turn on with the wrong profile, then the right profile. A double turn on. If you don’t care about this, ignore this point. If you do care, you need to make another automation that sees when lights are turned on and turns them on again with the current light profile for the light.

light profile automation that will work without turning on the lights.

- id: '201901022123'
  alias: 'Change Light Profile'
  trigger:
    - platform: state
      entity_id: input_select.livingroom_light_profile, input_select.meals_light_profile, input_select.kitchen_light_profile, input_select.bedroom_light_profile
  condition:
    - condition: or
      conditions:
        - condition: state
          entity_id: 'device_tracker.********'
          state: 'home'
        - condition: state
          entity_id: 'device_tracker.********'
          state: 'home'
    - condition: template
      value_template: "{{ not is_state('input_select.house_mode', 'Movie') }}"
    - condition: state
      entity_id: input_boolean.house_automatic
      state: 'on'
    - condition: template
      value_template: >
        {% set object_id = trigger.entity_id.split('.')[1].replace('_light_profile','').replace('_','') + '_ceiling' %}
        {% set entity_id = 'light.'+object_id %}
        {{ is_state(entity_id, 'on') }}
  action:
    - service: light.turn_on
      data_template:
        entity_id: >
          {{ 'light.' + trigger.entity_id.split('.')[-1].replace('_light_profile','').replace('_','') + '_ceiling' }}
        profile: >
          {{ states(trigger.entity_id) }}

If you want the lights to always use the current light profile when turning on, then you’d need the following automation and you can remove the previous one. Keep in mind, lights will never turn on when you change the profile. Only when you turn on the light.

- alias: Apply light profile
  trigger:
    - platform: state
      entity_id: light.living_room_ceiling, light.meals_area_ceiling, light.kitchen_ceiling, light.bedroom_ceiling
      from: off
      to: on
  action:
    service: light.turn_on
    data_template:
      entity_id: "{{ trigger.entity_id }}"
      profile: >
        {% if 'living_room' in trigger.entity_id %}
          {{ states('input_select.livingroom_light_profile') }}
        {% else %}
          {% set entity_id = 'input_select.' + trigger.entity_id.split('.')[-1].replace('_ceiling','_light_profile') %}
          {{ states(entity_id) }}
        {% endif %}

Again the caveat here is that your lights will turn on twice.

1 Like

@petro
Thank you for the input. That certainly is a more elegant script than what I was using.
I was finding the turn on twice happening as it was anyway, but it happens so quickly that it was barely noticable and could be tolerated :).
That appears to add the complexity I was looking for. I will try it when I get back home in a few hours.
It’s amazing how much templating can achieve. I am still learning more about it.

I’ll have to play with this further, but you have given me some ideas.
The template, as you wrote it, seems to work fine for the kitchen ( entity_id: light.kitchen_ceiling ) but it is failing for the other groups, which have an extra _ ( entity_id: light.living_room_ceiling as example)

It’s certainly given me more of an idea though :slight_smile: Thanks.

Edit: I worked out what happened. I had some naming differences. The profile selector for meals area as an example was named input_select.meals_light_profile, but the light group was light.meals_area_ceiling.

I changed to a consistent naming scheme and all works as it should.

Thank you so much Petro.

Well the only reason thats happening is because your naming convention isn’t rock solid.

If you named everything with this format:

*room_name*_light_profile
*room_name*_ceiling

the following automation would work for every new combination you add:

- id: '201901022123'
  alias: 'Change Light Profile'
  trigger:
    - platform: state
      entity_id: input_select.livingroom_light_profile, input_select.meals_light_profile, input_select.kitchen_light_profile, input_select.bedroom_light_profile
  condition:
    - condition: or
      conditions:
        - condition: state
          entity_id: 'device_tracker.********'
          state: 'home'
        - condition: state
          entity_id: 'device_tracker.********'
          state: 'home'
    - condition: template
      value_template: "{{ not is_state('input_select.house_mode', 'Movie') }}"
    - condition: state
      entity_id: input_boolean.house_automatic
      state: 'on'
    - condition: template
      value_template: >
        {% set entity_id = 'light.' + trigger.entity_id.split('.')[-1].replace('_light_profile','_ceiling') %}
        {{ is_state(entity_id, 'on') }}
  action:
    - service: light.turn_on
      data_template:
        entity_id: >
          {{ 'light.' + trigger.entity_id.split('.')[-1].replace('_light_profile','_ceiling') }}
        profile: >
          {{ states(trigger.entity_id) }}

The only reason I had to do fancy foot work was because you have living_room in one spot vrs livingroom in the other.

1 Like

Yes. I appreciate that.
Standardising the naming convention was definitely a better thing long term :slight_smile:
Appreciate your help.