Dim lights to 50% of their brightness?

I am trying to automate my lights dimming when I turn on the TV…
So I put this automation together:

- id: '1234567890123'
  alias: TV Dim Living Room Lights
  - entity_id: binary_sensor.livingroomtv
    from: 'off'
    platform: state
    to: 'on'
  - condition: state
    entity_id: light.living_room
    state: 'on'
  - data_template:
      brightness: '{{ state_attr(''light.living_room'', ''brightness'') / 2 }}'
      entity_id: light.living_room
    service: light.turn_on

But the brightness is not being set… any ideas?

1 Like

Don’t delete this one … I flagged the other post …

My internet bombed out and I thought it didn’t post successfully so I posted again before realising!

your quotes are not correct:

  {{ (state_attr('light.living_room', 'brightness')/2)|int }}

would do it. Probably need the |int because otherwise you’re feeding a string for brightness…

1 Like

Tried this, this works:

brightness: "{{ (state_attr('light.living_room', 'brightness') / 2) | int }}"

EDIT: @Mariusthvdb beat me to it

1 Like

Thanks guys

Hi all, thank you all for this thread, but I want to be able to turn on my TV and my dimmer go to 50% or 25% brightness of 100%.
The way it’s been setup (as per the threads above) is that I get 50% of my last state of the dimmer. eg. light last off at 40%, it turns on at 20%.
My current input_number.yaml is:

  name: Brightness
  initial: 0
  min: 0
  max: 255
  step: 1
  icon: mdi:brightness-5

and automations.yaml:

- id: '1234567890123'
  alias: Brightness to 50%
  description: TV on turns brightness to 50%
  - platform: state
    entity_id: media_player.lounge
    from: 'off'
    to: 'on'
  - condition: state
    entity_id: light.sonoff_lounge_dimmer
    state: 'off'
  - data:
      brightness: '{{ (state_attr(''light.sonoff_lounge_dimmer'', ''brightness'')/2)|int }}'
      entity_id: light.sonoff_lounge_dimmer
    service: light.turn_on

sorry this is my first post so not sure how to fix the coding to look like everyone else…

Welcome to the forum!

To get your code formatted correctly see item 11 here:

Thank you Dave

Not sure what you have the input number for as it’s not doing anything in that automation.

That automation will divide the current brightness by 2 when the TV turns on, but only if the light is off… Your condition seems wrong to me. If the light is off the brightness will be zero. Plus, don’t you just want to reduce the brightness of a light which is on?

Thanks for the reply Dave.
To be honest, I got the input number details from another post I read around 3 weeks ago to get a slider card on my lovelace to adjust the brightness. I didn’t like the large lightbulb card with the gauge to adjust the brightness.
But what you mention about reducing and increasing brightness is exactly what I’m looking for. I just haven’t found any other posts that explain how to do it on the sonoff D1.
I think I’m overthinking it and in doing so, I can’t wrap my mind around how to

What I’m really trying to get to, is to be able to add a shortcut on my phone, whereby I can select it and it goes to a specific brightness. Whether the light is on or off.

I’m not sure if this should be an automation or a script that I need to create. Say one for 75%, another for 50% and then 25%.

Your guidance is much appreciated.

Thats fair enough, however I’m guessing you have other code somewhere to take care of that since the input_number is not mentioned in your above automation, so has no effect on it.

If you remove the condition in the above automation, it should work for you.

How about a row (horizontal stack) of button cards calling the light.turn_on service? You can then include the desired brightness in the service call of each button, 25%, 50%, 75%, 100%

Hi Dave

I finally played around enough and created scripts for each of the % that I wanted (20,40,60,80 and 100).
I’ve subsequently created shortcuts on my phone whereby I can activate each iteration by voice.

Thank you very much for your assistance!

No problem, but you should have been able to use voice commands to request a brightness anyway without the need for scripts.

eg: I can say “hey google, set the lounge downlights to 46%”. (note that I can use any % and this does not require any coding at all, simply exposing the light entity to Google Assistant)

Unfortunately I’ve got an iPhone. Siri only understands what I’ve programmed as a shortcut into it.
It would be great if I knew how or what kind of script, would allow me to adjust brightness based on what I say.
So in the script where I have my action, I have set brightness: 50%… if it it said “variable” instead or something to that effect that would be great but don’t know how to do that.