Button pressed -> increase brightness 10 %

Hi,
Looking for a script/automation that will help me with this. maybe I need to look at templates? anyone done anything similar?

When I button is pressed i would like my hue light to increase the brightness by 10% and the reverse if Another button is pressed?

So Always append 10% to previous value or remove 10% depending on with button is pressed.

The action would be something like this,

   action:         
     - service: light.turn_on
       data_template:
         entity_id: light.zw_uplight_dimmer_level
         brightness: >-
           {%if trigger.event.data.action_value | float > 0 %}
           {{  states.light.zw_uplight_dimmer_level.attributes.brightness | int + 50 }}
           {% else %}
           {{  states.light.zw_uplight_dimmer_level.attributes.brightness | int - 50 }}
           {% endif %}

I’ve done exactly this with my Lifx lights and a script.

Increase brightness:

livinglightup:
  alias: Living Light Up
  sequence:
    service: light.turn_on
    data_template:
      entity_id: light.living
      transition: '1'
      brightness: >
       {%- if states.light.living.attributes.brightness <= 25 %}
         51
       {% elif states.light.living.attributes.brightness <= 51 %}
         77
       {% elif states.light.living.attributes.brightness <= 77 %}
         102
       {% elif states.light.living.attributes.brightness <= 102 %}
         128
       {% elif states.light.living.attributes.brightness <= 128 %}
         153
       {% elif states.light.living.attributes.brightness <= 153 %}
         179
       {% elif states.light.living.attributes.brightness <= 179 %}
         205
       {% elif states.light.living.attributes.brightness <= 205 %}
         230
       {% elif states.light.living.attributes.brightness <= 230 %}
         255
       {% endif %}

Decrease brightness:

livinglightdown:
  alias: Living Light Down
  sequence:
    service: light.turn_on
    data_template:
      entity_id: light.living
      transition: '1'
      brightness: >
       {%- if states.light.living.attributes.brightness <= 51 %}
         25
       {% elif states.light.living.attributes.brightness <= 77 %}
         51
       {% elif states.light.living.attributes.brightness <= 102 %}
         77
       {% elif states.light.living.attributes.brightness <= 128 %}
         102
       {% elif states.light.living.attributes.brightness <= 153 %}
         128
       {% elif states.light.living.attributes.brightness <= 179 %}
         153
       {% elif states.light.living.attributes.brightness <= 205 %}
         179
       {% elif states.light.living.attributes.brightness <= 230 %}
         205
       {% elif states.light.living.attributes.brightness <= 255 %}
         230
       {% endif %}

The only thing I haven’t incorporated is to go from 0% (off) to 10% and vice versa.

Hope this helps

2 Likes

Thanks Keithh666 and Icaman004,
I question Icamann004, can’t you just use on line for this like keithh666 example or I’m missing something here?

I haven’t tried that method so can’t say unfortunately. I created this script to use as a brightness button in HADashboard.

For 10% using icaman004’s automation. Works at any brightness. Same principle as Keithh666’s but used in a script without requiring a trigger value.

 livinglightdown:
      alias: Living Light Down
      sequence:
        service: light.turn_on
        data_template:
          entity_id: light.living
          transition: '1'
          brightness: >
           {% set n = states.light.living.attributes.brightness + 25 %}
           {% if n > 255 %}
             255
           {% else %}
             {{ n }}
           {% endif %}
2 Likes

I’ve been trying to use this one and it is working at any brightness, but not when the light is completely switched off. I tried to add an additional if-statement to first check for the state of the light, but cannot get it done. Anyone that can add that to the script?

Thanks for posting this! Just used it to write a script for my Ikea Tradfri remote and a group of lights.

Here’s what it ended up looking like for Decreasing brightness

Edit: I kept reading and testing and this looks better.

'1234567890':
  alias: Kitchen Brightness Increase Button
  sequence:
    service: light.turn_on
    data_template:
      entity_id: light.kitchen_lights
      transition: '1'
      brightness: >
       {% if not state_attr('light.kitchen_lights', 'brightness') == None %}
       {% set n = state_attr('light.kitchen_lights', 'brightness') + 25 %} 
       {% if n > 255 %}
         255
       {% else %}
         {{ n }}
       {% endif %}
       {% else %} 
       25 
       {% endif %}

'1234567891':
  alias: Kitchen Brightness Decrease Button
  sequence:
    service: light.turn_on
    data_template:
      entity_id: light.kitchen_lights
      transition: '1'
      brightness: >
       {% if not state_attr('light.kitchen_lights', 'brightness') == None %}
       {% set n = state_attr('light.kitchen_lights', 'brightness') - 25 %} 
       {% if n > 255 %}
         255
       {% else %}
         {{ n }}
       {% endif %}
       {% else %} 
       0 
       {% endif %}
1 Like

Hi, I am trying to achieve this as well with my yeelight, but it just won’t work.

This is my yaml code at the “action” part:

data_template:
  brightness: >
    {% set n = states.light.living_room.attributes.brightness + 25 %} 
    {% if n > 255 %}
      255
    {% else %}
      {{ n }}
    {% endif %}
  entity_id: light.living_room
  transition: '1'
service: yeelight.set_color_temp_scene

Can you tell me what I am doing wrong?
Thanks,
Steeveno

1 Like

You are adding 25 to a string.
Try : -

{% set n = state_attr('light.living_room', 'brightness') | int + 25 %}

brightness isn’t a string, it’s an int. attributes can be any type and the |int is not needed.

@steeveno service: yeelight.set_color_temp_scene is not the same as light.turn_on.

Your issue is that transition is not supported by yeelight.set_color_temp_scene. The docs say that only entity_id, kelvin, and brightness are allowed.

1 Like

Thanks for the correction but as there should be no problem with casting an int to an int and I couldn’t test (I was on my phone) I thought it a fair bet.
Always willing to be corrected
:+1:

I would just use light.turn_on with the newish brightness_step_pct: used for brightness. :slight_smile:

@petro I assume that this can still be used, because presumably home assistant is actually converting this to a brightness instead of brightness step before passing it to the component?

The brightness template can still be used anywhere that accepts brightness but I’m assuming his issues are from the ‘extra key: transition’.

I don’t mean the brightness template, I mean the brightness_pct option.

Yeah that should work for brightness_pct but the math will be different:

  brightness_pct: >
    {% set n = states.light.living_room.attributes.brightness + 10 %} 
    {% if n > 100 %}
      100
    {% else %}
      {{ n }}
    {% endif %}

I know this post is quite old, but I wanted to do the same thing here and found that my UI in the services developer tool offered brigness_step which takes a -/+ integer of the amount to change by.
(Running Home Assistant Container 2021.8.7)

I now have an automation that when I turn a Aqara cube right, it increases my light by 50 (~1/5th of 255) and whe turned left, decreases by 50.
Acts like a dimmer but without being fixed to a wall or anything - currently being used on the bedside table for the bedside lights which are both zibgee bulbs using Zigbee2MQTT.

The service code which is eventually used by the automation GUI is as follows:

  - service: light.turn_on
    target:
      entity_id: light.bedside_lamp
    data:
      brightness_step: +50

and obviously a -50 step for decrease

3 Likes