How can I set brightness of a lamp to half of current brightness?

Hi, I want to make a script to dim lights first, then turn off.

turn_off_with_step: 
  alias: "Dim lamp and turn off after 5 seconds."
  sequence:
    - service: light.turn_on
      data_template:
        entity_id: "light.{{ light }}"
        brightness_pct: 50  
    - delay: 5
    - service: light.turn_off
      data_template:
        entity_id: "light.{{ light }}"

When I call this script with data of

{"light":"deneme"}

It works good. But sometimes my lamps are at 100, sometimes at 50 brightness. It would be nice if I could find a way to adjust them simply half of the current value. What I am trying to do is something like this:

  data_template:
    entity_id: "light.{{ light }}"
    brightness_pct: {{ states.light.bedroom.attributes.brightness / 2 }}  

How can I use given variable (light name) instead of hard coded bedroom? Something like that:

  data_template:
    entity_id: "light.{{ light }}"
    brightness_pct: {{ states.light.{{ light }}.attributes.brightness / 2 }}
  data_template:
    entity_id: "light.{{ light }}"
    brightness: "{{ state_attr('light.'~light, 'brightness') / 2 }}"
1 Like

You are a hero! Thank you very much!

For anyone interested in this, I had to make a slight change and convert attribute to int.

- service: light.turn_on
  data_template:
    entity_id: "light.{{ light }}"
    brightness: "{{ (state_attr('light.'~light, 'brightness') / 2)|int }}"

Thanks again. :upside_down_face:

That isn’t necessary, for two reasons. First, the attribute is already an int, otherwise you couldn’t divide it by 2. Second, the result of any template is always going to be a string, even if it has to be converted to a string. So adding the int filter does nothing but add extra processing time.

The fact that the brightness value of the service call being a string representation of a number is not a problem because the service code is going to convert it to a int internally. Otherwise you could never use a template (because, again, the output from every template is a string.)

But it says following error when I don’t convert to int:

Invalid service data for light.turn_on: expected int for dictionary value @ data['brightness']. Got '64.5'

yes, brightness needs the |int is what I experienced too:

action:
  service: light.turn_on
  data_template:
    entity_id: light.outside
    brightness: >
      {{state_attr('light.outside','brightness') |int }}

doesn’t work without it.

same with brightness reading from an input_number:

  - service: light.turn_on
    data_template:
      entity_id:
        - light.driveway
        - light.parking_light
      brightness: >
        {{states('input_number.outdoors_brightness')|int }}
      transition: 5

Ah, ok, I didn’t think of that scenario. So it’s not that you’re trying to convert a string to an int, but trying to convert a float to an int before it gets converted to a string representation due to the fact that the template output is always a string.

It comes down to the Python int() function - int('6') is ok, and int(6.5) is ok, but int('6.5') isn’t. You’re ending up in that last case without the int filter because the template takes the float and turns it into a string representation of it (i.e., converts 6.5 to '6.5'.)

@Mariusthvdb, at the very least, I’m pretty sure it’s not needed here. Brightness is an int, so converting an int to an int definitely doesn’t do anything.

This is a different case since an input_number’s state can be a string representation of a float. So to avoid the situation described above, it should be converted to an int before it automatically gets converted to a string.

But I suppose in the end adding a potentially unnecessary |int filter doesn’t hurt. It’s probably just best to be consistent.

I needed it here, since when the light is not on, brightness would be 0. And 0 is not an int, so to keep things simple and not add all kinds of extra rules in the template, adding |int makes it work.

Ah, ok, another scenario I didn’t consider. lol! Thanks for setting me straight. :slight_smile: FWIW, when the light is not on the brightness attribute doesn’t exist, so the state_attr function returns None. The int filter (by default) returns 0 when it can’t convert the input (such as None) to a number.

exactly.
where I mentioned 0, I was referring to my input_number being slided to 0 which is a valid value for the slider, and hence, input for the lights template.

so, in both cases, the |int is very nice to have.