Incremental Increase of Light Brightness

So I am very new to Home Assistant and have been working through it all quite well and am now at a stage where I want to do something a bit more clever.

What I want to do is incrementally increase and decrease the brightness of my lights which are there as a device on my dashboard. I can control this via the app which is fine, using the slider, but I am wanting to automate this via an additional input from my LIRC controller on a button press.

I am thinking the way to go about this would be to get the current brightness state of the bulb inside the configuration.yaml file and then to perform a calculation on this, so reduce it by 10 for example, and then pass this through to the turn_on service for that bulb.

Would this be the correct way about going about things? Is there allready something in existance that I just have missed? Is this even possible? If so - how do I go about getting the current value and then performing a calculation on it to send across… variables and the like!

Thanks in advance for any assistance!

1 Like

Hi!

You’ve got the right idea. In your states panel, are you able to see the brightness value as an attribute? For example, one of my lights looks like this…

Entity                 | State  | Attributes
light.living_room_lamp |   on   | {"brightness": 255, "friendly_name": "Living Room Lamp", "rgb_color": [255, 255, 255], "supported_features": 61}

In your automation or script, you’ll want to …

  • Expose the attribute of your light entities’ brightness
  • React to the button press of your LIRC controller
  • Loop through all lights that have state == 'on'
  • Run the light.turn_on service with step1 +/- 10

If you are familiar with Python, I could easily help you with running an AppDaemon app (but this requires a little bit of setup). Otherwise, you will have to rely on the built-in methods within HomeAssistant. What you’re asking for is certainly VERY possible though!

3 Likes

You could do this using a template, something like:

- service: light.turn_on
  entity_id: light.light_name
  data_template:
    brightness: '{{states.light.light_name.attributes.brightness + 10}}'

You’ll need a condition first to ensure the light is on, otherwise states.light.light_name.attributes.brightness will return null and throw and cause the template to throw an error.

edit: added single quotes around the data_template for future reference.

5 Likes

So I am trying the templating method currently as it seemed on the surface the most straight forward to get started, but running into an error I don’t know how to resolve as unfamiliar with this syntax etc.

The error is:
homeassistant.util.yaml: invalid key: "OrderedDict([('states.light.lounge.attributes.brightness + 10', None)])"

and my code is:

# AUTOMATION
automation:
  - alias: Toggle Entrance Light
    trigger:
      platform: event
      event_type: ir_command_received
      event_data:
        button_name: KEY_CHANNELUP
    action:
      service: homeassistant.turn_off
      entity_id: light.entrance

  - alias: Increase Lounge Brightness
    trigger:
      platform: event
      event_type: ir_command_received
      event_data:
        button_name: KEY_VOLUMEUP
    action:
      service: light.turn_on
      entity_id: light.lounge
      data_template:
        brightness: {{states.light.lounge.attributes.brightness + 10}}

The error refers to the last line.

Any assistance greatly appreciated!

put the template in single quotes I think.

So changing to:

  data_template:
    'brightness: {{states.light.lounge.attributes.brightness + 10}}'

Gives this error:

homeassistant.bootstrap: Invalid config for [automation]: expected a dictionary for dictionary value @ data['action'][0]['data_template']. Got None.

But it refers to line 73 which is:
automation:

just around the brakets
‘{{states bla bla bla }}’

1 Like

You absolute star!

Thankyou everybody!

did something change with this im getting an error.

17-03-01 15:13:00 INFO (MainThread) [homeassistant.core] Bus:Handling <Event service_executed[L]: service_call_id=139996349380648-5>

17-03-01 15:13:20 INFO (MainThread) [homeassistant.core] Bus:Handling <Event call_service[L]: service_call_id=139996349380648-6, domain=automation, service_data=entity_id=automation.increase_living_room_lights, service=trigger>
17-03-01 15:13:20 INFO (MainThread) [homeassistant.components.automation] Executing increase living room lights
17-03-01 15:13:20 INFO (MainThread) [homeassistant.core] Bus:Handling <Event logbook_entry[L]: domain=automation, name=increase living room lights, message=has been triggered, entity_id=automation.increase_living_room_lights>
17-03-01 15:13:20 INFO (MainThread) [homeassistant.helpers.script] Script increase living room lights: Executing step call service
17-03-01 15:13:20 ERROR (MainThread) [homeassistant.core] Error doing job: Task exception was never retrieved
Traceback (most recent call last):
File “/usr/local/lib/python3.5/dist-packages/homeassistant/helpers/template.py”, line 99, in async_render
return self._compiled.render(kwargs).strip()
File “/usr/local/lib/python3.5/dist-packages/jinja2/environment.py”, line 1008, in render
return self.environment.handle_exception(exc_info, True)
File “/usr/local/lib/python3.5/dist-packages/jinja2/environment.py”, line 780, in handle_exception
reraise(exc_type, exc_value, tb)
File “/usr/local/lib/python3.5/dist-packages/jinja2/_compat.py”, line 37, in reraise
raise value.with_traceback(tb)
File “”, line 1, in top-level template code
jinja2.exceptions.UndefinedError: ‘mappingproxy object’ has no attribute ‘brightness’

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “/usr/lib/python3.5/asyncio/tasks.py”, line 239, in _step
result = coro.send(None)
File “/usr/local/lib/python3.5/dist-packages/homeassistant/components/automation/init.py”, line 294, in async_trigger
yield from self._async_action(self.entity_id, variables)
File “/usr/local/lib/python3.5/dist-packages/homeassistant/components/automation/init.py”, line 376, in action
yield from script_obj.async_run(variables)
File “/usr/local/lib/python3.5/dist-packages/homeassistant/helpers/script.py”, line 151, in async_run
yield from self._async_call_service(action, variables)
File “/usr/local/lib/python3.5/dist-packages/homeassistant/helpers/script.py”, line 181, in _async_call_service
self.hass, action, True, variables, validate_config=False)
File “/usr/local/lib/python3.5/dist-packages/homeassistant/helpers/service.py”, line 88, in async_call_from_config
config[CONF_SERVICE_DATA_TEMPLATE]))
File “/usr/local/lib/python3.5/dist-packages/homeassistant/helpers/service.py”, line 84, in _data_template_creator
for key, item in value.items()}
File “/usr/local/lib/python3.5/dist-packages/homeassistant/helpers/service.py”, line 84, in
for key, item in value.items()}
File “/usr/local/lib/python3.5/dist-packages/homeassistant/helpers/service.py”, line 86, in _data_template_creator
return value.async_render(variables)
File “/usr/local/lib/python3.5/dist-packages/homeassistant/helpers/template.py”, line 101, in async_render
raise TemplateError(err)
homeassistant.exceptions.TemplateError: UndefinedError: ‘mappingproxy object’ has no attribute ‘brightness’

- alias: 'increase living room lights'
  trigger:
platform: zone
entity_id: device_tracker.benjimatt_benjimatt
zone: zone.work
event: leave
  action:
- service: light.turn_on
  entity_id: light.living_room
  data_template:
    brightness: '{{ states.light.living_room.attributes.brightness + 10 }}'

It seems right but I am still getting an error. I really dont care about the event because im going to change that. I really just need the script.

I had a similar problem that gave the same error and it turned out that the light had to be switch on first. I imagine that this is so that the state can be received. I ended up having another service that turns it on first.

1 Like

Can someone help me implement this into a variable? I received the following error when I tried:

homeassistant found character ‘%’ that cannot start any token

I think it’s something to do with the line below line as it works if I simply put in a number. Just can’t figure what the solution is.

{%- set state = states.light.bedroom.attributes.brightness + 10 -%}

Config code:

- alias: "Bedroom Brightness"
  trigger:
    platform: event
    event_type: cube_action
    event_data:
        entity_id: binary_sensor.cube_xxxxxxxxxxxxxx
        action_type: rotate
  action:
  - service: light.turn_on
    entity_id: light.bedroom
    data_template:
      transition: 1
      brightness: >
              {% set state = (trigger.event.data.action_value|float) -%}
              {%-  if state > 0 -%}
                {%- set state  = states.light.bedroom.attributes.brightness + 10 -%}
              {%-  elif state < 0 -%}
                {%- set state  = states.light.bedroom.attributes.brightness - 10 -%}
              {%- endif %}
              {{ state }}
1 Like

that was it. I need to read a little closer next time lol

Hi there, can some one help me with this automation? I can’t really figure out

- alias: Comodino Andrea Dim+
  trigger:
    platform: event
    event_type: click
    event_data:
        entity_id: binary_sensor.switch_158d00012641b4
        click_type: hold
  action:
    service: light.turn_on
    entity_id: light.comodino_andrea
    data_template:
      brightness: '{{states.light.comodino_andrea.attributes.brightness + 10}}'

Try to change this line to:
brightness: ‘{{states.light.comodino_andrea.attributes.brightness|int + 10}}’

2 Likes

Thanks mate this solved my issue :smiley:
Do you know if there is a way for dim -
I’m trying this way but it doesn’t work… maybe is the service? light.turn_on?

action:
data_template:
  brightness: '{{states.light.comodino_andrea.attributes.brightness|int - 25}}'
entity_id: light.comodino_andrea
service: light.turn_on

Hello @MastroPino, I would try something like this:

  action:
    - service: light.turn_on
      entity_id: light.comodino_andrea
      data_template:
        brightness: '{{states.light.comodino_andrea.attributes.brightness|int - 25}}'

Make sure the indentation is correct. It looks wrong the way you set it up. Check also the logs for error when you reload. The way I have set up + and - dim looks like this:

- alias: Lysere pĂĽ Toalettet +10%
  trigger:
    - platform: event
      event_type: zwave.scene_activated
      event_data:
        entity_id: zwave.bryter_toalett_overetasje
        scene_id: 23
  action:
    - service: light.turn_on
      entity_id: light.dimmer_toalett_overetasje_level
      data_template:
        brightness: >
            {% set suggested = states.light.dimmer_toalett_overetasje_level.attributes.brightness|int + 31 %}
            {% if suggested < 200 %}
            {{ suggested }}
            {% else %}
            200
            {% endif %}
- alias: Mørkere pü Toalettet -10%
  trigger:
    - platform: event
      event_type: zwave.scene_activated
      event_data:
        entity_id: zwave.bryter_toalett_overetasje
        scene_id: 40
  action:
    - service: light.turn_on
      entity_id: light.dimmer_toalett_overetasje_level
      data_template:
        brightness: >
            {% set suggested = states.light.dimmer_toalett_overetasje_level.attributes.brightness|int - 31 %}
            {% if suggested > 25 %}
            {{ suggested }}
            {% else %}
            25
            {% endif %}

The if settings are just to define a minimum and maximum dim. This way it never gets pitch black. The last steps on the dimmer above 200 makes no difference at my house so I define 200 as max.

Good luck :slight_smile:

4 Likes

do you guys have an easy, nice, clean way of increase/decrease stepping without going above 100% or 255 and below 0?

brightness: ‘{{states.light.comodino_andrea.attributes.brightness|int + 10}}’
brightness: >
            {% set suggested = states.light.dimmer_toalett_overetasje_level.attributes.brightness|int - 31 %}
            {% if suggested > 25 %}
            {{ suggested }}
            {% else %}
            25
            {% endif %}

This is doing exactly that. This example is decrease. It first takes the current value from the attribute, detracts 31, checks if the proposed value is above 25, if yes, use the proposed. If no then use 25. The same logic for increase is used in in the example from my previous post. You can change the 25s to 0s for your example. Or 255 for increase (remember to change - to + and > to < :slight_smile:

2 Likes

that is true… overlooked this one :slight_smile:
Thanks

Thanks mate, you save me :slight_smile:

1 Like