Brightness based on time of day

hi,
I have an IKEA Tradfri with motion sensor. I am looking for some help. I would like to set the brightness level of the lamp based on the time of day. For example:

  • 7AM - 7PM: 255
  • 7PM - 11PM: 127
  • 11PM - 7AM: 31

So, whenever the motion sensor triggers during the day the light turns on and has the brightness according to the listed values.

My problem is that it seems I can not set the brightness of the light without turning it on. So the best thing I can think of is to create a time based trigger that runs a simple 3 step script:

  • turn on light with right brightness
  • wait 1 sec (just to be sure the light turned on)
  • turn off light

But, I feel this is a stupid workaround and hope someone has some another idea.

Thanks!

In the automation that turns the light on, you could use a template with an if block to set the brightness in the action based on the value of now(). I’d give a more specific example but I’m having a hard time finding out what format now() returns.

If somehow the light is turned on outside of Hass (ZWave has features allowing device-to-device control, it’s possible zigbee does as well), you could set a trigger, which changes the brightness of the light when it turns on. To wit:

automation foo:
  trigger:
    platform: state
    entity_id: light.foo
    from: 'off'
    to: 'on'
  action:
    service: light.turn_on
    entity_id: light.foo
    data_template:
      brightness: {% if block here %}
1 Like

The thing is that the Tradfri will turn on the light and not an automation in HA (like you said device-to-device control).
Thanks for the suggestion to adjust the brightness when the light is indeed turned on.

I am curious though whether the limitation to set brightness without turning on the light is an imitation of the HA Light Platform (feature request!) or that z-wave/zigbee/e.a light platforms simply do not support this and hence mo point of building it in HA. Anyone know?

You could do a simple condition if the state of the light is on. And another automation that if you turn it on between the times it goes to the brightness you’d like it as.

The problem with this is you’d have a lot of automations for each of your Lights.

So another way but more complex would be to write some jinja code to loop through all of your Lights and any that are on, set to the desired brightness. You could do 2 triggers, one for time of day and the other if the light state from off to on.

With trigger.entity_id I would probably be able to create a single automation, so that wouldn’t be so bad, but I would have liked to create a time based triggers to set the brightness without turning on the lights.
Thanks for your response.

It’s not possible to set brightness when lights are off. Basically lights are off means brightness = 0

Hi,

i tried this approach since I have the same demand.

I did

- id: Licht-6
  alias: Ganglicht abends dimmen
  trigger:
    platform: status
    entity_id: group.gang
    from: 'off'
    to: 'on'
  action:
    service: light.turn_on
    entity_id: group.gang
    data_template:
      brightness: {% if now().hour > 20 %}50{% elif now().hour < 8 %}50{% else %}200{% endif %}

If I check this code in the home assistant template editor, I get the correct result depending of time of day

Example result:

data_template:
      brightness: 200

but if I check the config using HA, I get the following error:

Error loading /home/homeassistant/.homeassistant/configuration.yaml: while scanning for the next token found character ‘%’ that cannot start any token in “/home/homeassistant/.homeassistant/automations.yaml”, line 70, column 20

which corresponds to the first “%” in my data template. What am I doing wrong? Am I overseeing the obvious?

Any hint would be highly appreciated.

Cheers,
Huettenwirt

should be with quotes

 brightness: '{% if now().hour > 20 %}50{% elif now().hour < 8 %}50{% else %}200{% endif %}'

I feared it was that simple :wink:

there are so many examples out there without the quotes… I was under the impression that the values should be without single quotes. But it seems it doesn’t hurt to use them most of the time…

Thanks a lot for pointing this out!

Best,
Huettenwirt

If it’s on a different line like this, it doesn’t need the quotes. Single lines should require it.

brightness: >
  {% if now().hour > 20 %}50
  {% elif now().hour < 8 %}50
  {% else %}200
  {% endif %}
4 Likes
data_template:
      brightness: >
      {% if now().hour > 20 %}100
      {% elif now().hour < 5 %}50
      {% elif now().hour < 8 %}100
      {% else %}200
      {% endif %}

throws the same error:

Error loading /home/homeassistant/.homeassistant/configuration.yaml: while scanning for the next token found character ‘%’ that cannot start any token in “/home/homeassistant/.homeassistant/automations.yaml”, line 71, column 8

So the only way to get it to work for me is keeping it in one line and using the single quotes…

Strange…

2 Likes

It’s your spacing. You need to indent like my example above. It can’t line up with brightness.

Argh…:roll_eyes:

Should have seen it myself…

Thx a ton for pointing me on this…

Cheers,
Huettenwirt

The easiest way to fix this would be to create 3 scenes for :
7AM - 7PM: 255
7PM - 11PM: 127
11PM - 7AM: 31
and then in the automation create new automation calling the scens at specific time of the day.