Trouble with light group on/variable brightness based on time of day automation

Hi All, I’m running home assistant core, and have been having a bit of trouble with an automation to turn a light group on/off with a specified brightness. Basically, I have an automation that turns on a light group in my kitchen when motion is detected, and another that turns it off after a minute of no motion. The basic automation to turn on and off works just fine, the problem is when I try to add logic to turn the light on with a brightness_pct setting added in.

When I add this automation, the automation will no longer work properly, I’ve seen it trigger once after restarting home assistant, and now its not reacting at all to motion… I was hoping someone may be able to help me troubleshoot where I’m going wrong. I’m assuming there may be something wrong with my if/else block, here is the relevant block of code as it appears in my automations.yaml:

- id: '1565162664975'
  alias: Kitchen Motion Lights On When Movement Detected
  trigger:
  - entity_id: binary_sensor.samjin_motion_fcf00601_ias_zone
    platform: state
    to: 'on'
  - entity_id: binary_sensor.samjin_motion_e5ad0701_ias_zone
    platform: state
    to: 'on'
  condition: []
  action:
  - data:
      entity_id: light.kitchen_lights
      brightness_pct: >
        {% if now().hour > 22 %} 1
        {% elif now().hour < 7 %} 1
        {% else  %} 100 {% endif %}
    service: homeassistant.turn_on
- id: '1565163530303'
  alias: Kitchen Lights Off After A Minute Of No Motion
  trigger:
  - entity_id: binary_sensor.samjin_motion_fcf00601_ias_zone
    for: 00:00:30
    platform: state
    to: 'off'
  - entity_id: binary_sensor.samjin_motion_e5ad0701_ias_zone
    for: 00:00:30
    platform: state
    to: 'off'
  condition:
  - condition: state
    entity_id: binary_sensor.samjin_motion_fcf00601_ias_zone
    for: 00:00:30
    state: 'off'
  - condition: state
    entity_id: binary_sensor.samjin_motion_e5ad0701_ias_zone
    for: 00:00:30
    state: 'off'
  action:
  - data:
      entity_id: light.kitchen_lights
    service: homeassistant.turn_off

Note the brighness_pct code under the “Kitchen Motion Lights On When Movement Detected” alias.

There are a few other things I am confused about as well that maybe someone can clear up:

  1. I am using the service: homeassistant.turn_on /off because I think that a light group cannot be triggered by a service: light.turn_on/off call, is this true?
  2. I am using data: under the action: section because I get an error when I try to use “data_template” (which I saw being used in a bunch of examples online)… I get this error in the UI automation editor when I put in data_template:
UI editor is not supported for this config:
Key "data_template" is not expected or not supported by the visual editor. You can still edit your config in YAML.

Am I actually able to use data_template, and I should just ignore the error? Regardless, the automation still doesn’t work if I use data_template, so I’m still dead in the water.

Thanks for any help you can provide!

The entities in this yaml are not groups. If it was a group, the entity id would have started with group and not light. The light.kitchen_lights is a light entity and you should use light.turn_on and light.turn_off

Thanks for the reply, here is what my light: section looks like in my configuration.yaml:

light:
  - platform: group
    name: Dining Room Lights
    entities:
      - light.sengled_e11_n1ea_5e970100_level_light_color_on_off #Center Bulb
      - light.sengled_e11_n1ea_d6410300_level_light_color_on_off #Front Bulb
      - light.sengled_e11_n1ea_200a0300_level_light_color_on_off #Back Bulb
      - light.dining_room_wall_light
        #  - service: light.candle_flicker
  - platform: group
    name: Kitchen Lights
    entities:
      - light.ledvance_br30_rgbw_58500600_level_light_color_on_off #1st Recessed Bulb
      - light.ledvance_br30_rgbw_95650600_level_light_color_on_off #2nd Recessed Bulb
      - light.ledvance_br30_rgbw_e4400600_level_light_color_on_off #3rd Recessed Bulb
      - light.ledvance_br30_rgbw_3c050e00_level_light_color_on_off #4th Recessed Bulb

I thought that this was how light groups were implemented, but it is possible I am completely off base, does this look like a proper implementation to you?

I also just tried changing the service to light.turn_on

- id: '1565162664975'
  alias: Kitchen Motion Lights On When Movement Detected
  trigger:
  - entity_id: binary_sensor.samjin_motion_fcf00601_ias_zone
    platform: state
    to: 'on'
  - entity_id: binary_sensor.samjin_motion_e5ad0701_ias_zone
    platform: state
    to: 'on'
  condition: []
  action:
  - data_template:
      entity_id: light.kitchen_lights
      brightness_pct: >
        {% if now().hour > 22 %} 1
        {% elif now().hour < 7 %} 1
        {% else  %} 100 {% endif %}
    service: light.turn_on
- id: '1565163530303'
  alias: Kitchen Lights Off After A Minute Of No Motion
  trigger:
  - entity_id: binary_sensor.samjin_motion_fcf00601_ias_zone
    for: 00:00:30
    platform: state
    to: 'off'
  - entity_id: binary_sensor.samjin_motion_e5ad0701_ias_zone
    for: 00:00:30
    platform: state
    to: 'off'
  condition:
  - condition: state
    entity_id: binary_sensor.samjin_motion_fcf00601_ias_zone
    for: 00:00:30
    state: 'off'
  - condition: state
    entity_id: binary_sensor.samjin_motion_e5ad0701_ias_zone
    for: 00:00:30
    state: 'off'
  action:
  - data:
      entity_id: light.kitchen_lights
    service: light.turn_off

But no dice, the lights are still unresponsive. The issue seems to have something to do with me adding brightness_pct

Please call the service from the developer console and see if it is working.

I called the light.kitchen_lights service from the services tab of the developer console, and the lights turn on successfully. I tried adding the brightness_pct section as part of the Service Data, and get an error:

Failed to call service light/turn_on. expected float for dictionary value @ data[‘brightness_pct’]

looks like its having trouble with my if/else statement.

You cant use templates in developer tools services.

Try this in your automation:

  action:
  - service: light.turn_on
    entity_id: light.kitchen_lights
    data:
      brightness_pct: >
        {% if now().hour > 22 %} 1
        {% elif now().hour < 7 %} 1
        {% else  %} 100 {% endif %}
    
1 Like

Thanks for that, do you know if it is ok to just put the brightness setting under the data: section for my example here?

Try this in automation data

entity_id: light.kitchen_lights
brightness_pct: >-
  {% if now().hour > 22 %} 1  {% elif now().hour < 7 %} 1 {% else  %} 100 {%
  endif %}

Yes, it is fine.

1 Like

I modified my code to now have:

  action:
  - data:
      entity_id: light.kitchen_lights
      brightness_pct: >-
        {% if now().hour > 22 %} 1  {% elif now().hour < 7 %} 1 {% else  %} 100 {%endif %}
    service: light.turn_on

Unfortunately, it is still not working, although I did notice some behavior I had not noticed before. After one minute, and the light.turn_off service is called, the lights briefly flare on before turning off… Not sure why that is happening.

Try moving the entity id out of the data block as in my example.

sorry about that, I now tried this:

  action:
  - service: light.turn_on
    entity_id: light.kitchen_lights
    data:
      brightness_pct: >-
        {% if now().hour > 22 %} 1  {% elif now().hour < 7 %} 1 {% else  %} 100 {%endif %}

Still no luck though :confused: I am still seeing the behavior where the lights will flicker briefly when the light.turn_off service is called.

What time is it where you are?

Your template will turn the light on with a 1% brightness from 10pm to 7am and 100% all other times.

My proposal is to resolve that scripting part into a choose sequence. You can then use a time-based condition after: 22:00:00 and before: 07:00:00. Using full brightness would be the default then.

I figured out the issue!

Turns out we were looking down the wrong path for the solution, the issue was actually with the bulbs themselves. I am using Sylvania smart+ (LEDVANCE) 73739 color BR30 bulbs from 2017, which apparently had a bug related to brightness settings.

I figured out this was the case from another forum post from July 2020 that @tom_l was actually a part of:

I am using the ZHA integration to control my bulbs, so I then looked into updating the firmware on my bulbs, and found that it was possible from this reddit post (also mentioned in the ZHA integration page, but the reddit post describes how to force an update):

https://www.reddit.com/r/homeassistant/comments/fak430/how_to_update_your_ikea_or_ledevance_firmware/?ref=share&ref_source=link

I did end up utilizing @m0wlheld’s recommendation to utilize a choose sequence, so in case anyone is curious how that turned out, here is my final implementation; the choose sequence has a nice implementation in the automations UI:

- id: '1565162664975'
  alias: Kitchen Motion Lights On When Movement Detected
  trigger:
  - entity_id: binary_sensor.samjin_motion_fcf00601_ias_zone
    platform: state
    to: 'on'
  - entity_id: binary_sensor.samjin_motion_e5ad0701_ias_zone
    platform: state
    to: 'on'
  condition: []
  action:
    - choose:
        - conditions:
            - condition: time
              after: "22:00:00"
              before: "07:00:00"
          sequence:
            - service: light.turn_on
              entity_id: light.kitchen_lights
              data:
                brightness_pct: 1
      default:
        - service: light.turn_on
          entity_id: light.kitchen_lights
          data:
            brightness_pct: 100

I seriously probably attempted to get past this issue at least 10 times the last 1.5 years, and just ended up giving up every attempt till now. Lesson learned, always check for firmware updates if things that should be behaving are not. Thanks everyone for the help!