Disclosure: Some of the links on this site are affiliate links. This means that, at zero cost to you, I will earn an affiliate commission if you click through the link and finalize a purchase.

Home Assistant Push Button Dimmer (Simple Template Tutorial)

Wondering how to transform a boring on/off light switch into an awesome dimmer?

This tutorial will teach you how to incrementally increase the brightness of a light each time you push a button.

If you are looking for a simple introduction to the power of templating in Home Assistant then this tutorial is definitely for you!

We will learn how to write a script that includes a template which will incrementally increase the brightness of a chosen light entity in an easy step-by-step format.

Table of Contents

  1. Prerequisite
  2. Configure Home Assistant
    1. Create a script
    2. Setup the service
    3. Add a data template
    4. Define the step size
    5. Store current brightness
    6. Calculate new brightness
    7. Loop brightness
  3. Conclusion

Prerequisite

I will assume you already have Home Assistant up and running as well as at least one light entity configured.

If you are new to Home Assistant or you are not yet familiar with editing YAML files then you should definitely check out my beginners tutorial on YAML first. I would also recommend taking a look at my other tutorials on automation and scripts.

I also touched on the topic of templates in my tutorial about remembering states so you may also be interested in checking it out later.

Configure Home Assistant

In order to get our one button dimmer switch to work we will setup a simple script in our configuration.yaml file. This script will simply turn on the chosen light entity that we want to apply the incremental dimming to.

So how to we turn a basic script into this super-cool one button dimmer? We will use a template to read the brightness value and then add on a value of brightness determined by a step value.

The step value can be whatever you choose, it depends how many times you want to press the button to reach full brightness. The value of brightness is represented by the numbers 0 through to 255 for minimum to maximum brightness respectively. Therefore a step value of 25 will give us 10 steps of brightness.

We will also add an if statement inside of the template that will check to see if we have reached maximum brightness. If maximum brightness has been reached then the brightness will be reset to 0.

Create a script

First we need to create a new script under the script integration in our configuration.yaml file. I will call the script fade_the_lamp but you can give it a different name if you prefer.

fade_the_light:
    alias: Fade the light
    description: 'Incrementally fade the light'
    sequence:

Setup the service

Next we need to add a service to our sequence. We will use the light.turn_on service and specify the entity_id as the entity we wish to apply our incremental brightness to.

I am using a lamp called light.treelamp but you should choose your own entity here.

fade_the_light:
    alias: Fade the light
    description: 'Incrementally fade the light'
    sequence:
      - service: light.turn_on
        entity_id: light.treelamp

Add a data template

We will be adding data_template in order to specify the entity attributes using a template.

As we wish to change the brightness attribute of our entity, we will specify brightness and use the “>” symbol to denote that we will use a template on the following lines.

fade_the_light:
    alias: Fade the light
    description: 'Incrementally fade the light'
    sequence:
      - service: light.turn_on
        entity_id: light.treelamp
        data_template:
          brightness: >

Define the step size

Ok now for the clever bit! Rather than specify a constant value for the brightness, we will calculate the value with a template.

Templating uses the Jinja2 templating engine, therefore we will use the Jinja syntax for a {% statement %} and an {{ expression }} in this example. The actions that we wish to carry out in our template will be written as statements and the values to be returned are written as expressions.

The first thing we need to do is define the variable step_size for the step size that we wish to use. As previously mentioned I will use a step size of 25, giving 10 steps of brightness. However you can modify this if you like.

fade_the_light:
    alias: Fade the light
    description: 'Incrementally fade the light'
    sequence:
      - service: light.turn_on
        entity_id: light.treelamp
        data_template:
          brightness: >
            {% set step_size = 25 %}

Store current brightness

Next we need to save the current value for brightness using the state_attr(‘entity_id’, ‘attribute’) statement. This statement will return the value of the specified attribute for the specified entity.

We will specify our chosen light entity and the brightness value within the state_attr() statement. This will return the current brightness of the light entity.

state_attr('light.treelamp', 'brightness')

We also need to specify the value as an integer, which can be done by adding the pipe symbol and int to the end of the statement.

state_attr('light.treelamp', 'brightness') | int
state_attr('light.treelamp', 'brightness') | int

Then we need to load the returned value into a variable called current_brightness using the set command.

fade_the_light:
    alias: Fade the light
    description: 'Incrementally fade the light'
    sequence:
      - service: light.turn_on
        entity_id: light.treelamp
        data_template:
          brightness: >
            {% set step_size = 25 %}
            {% set current_brightness = (state_attr('light.treelamp', 'brightness') | int) %}

Calculate new brightness

Next we will add the step value to the current brightness and store it in a new variable called new_brightness.

fade_the_light:
    alias: Fade the light
    description: 'Incrementally fade the light'
    sequence:
      - service: light.turn_on
        entity_id: light.treelamp
        data_template:
          brightness: >
            {% set step_size = 25 %}
            {% set current_brightness = (state_attr('light.treelamp', 'brightness') | int) %}
            {% set new_brightness = current_brightness | int + step_size %}

Loop brightness

We need to test the new_brightness value to see if it is greater than 255. If we have exceeded the maximum brightness then we must reset the brightness back to 0.

fade_the_light:
    alias: Fade the light
    description: 'Incrementally fade the light'
    sequence:
      - service: light.turn_on
        entity_id: light.treelamp
        data_template:
          brightness: >
            {% set step_size = 25 %}
            {% set current_brightness = (state_attr('light.treelamp', 'brightness') | int) %}
            {% set new_brightness = current_brightness | int + step_size %}
            {% if new_brightness > 255 %}
              0

Finally we can specify the variable new_brightness as the value we wish to use for our data template attribute brightness. The values inside of the {{ curly brackets }} will be the value used for brightness.

fade_the_light:
    alias: Fade the light
    description: 'Incrementally fade the light'
    sequence:
      - service: light.turn_on
        entity_id: light.treelamp
        data_template:
          brightness: >
            {% set step_size = 25 %}
            {% set current_brightness = (state_attr('light.treelamp', 'brightness') | int) %}
            {% set new_brightness = current_brightness | int + step_size %}
            {% if new_brightness > 255 %}
              0
            {% else %}
              {{ new_brightness }}
            {% endif %}

Conclusion

Each time this script is triggered, the brightness of the light entity associated with it will increment by the step value.

Once it reaches maximum brightness it will reset back to 0 and the light will turn off. Awesome!

You can assign this script to any switch or function in Home Assistant compatible with the script.turn_on service.

Go ahead and give it a try with an entity button on the user interface!

If you enjoyed this tutorial and found it beneficial then I would definitely recommend checking out my tutorial on how to save the state of a light. Also why not check out some of my other awesome Home Assistant tutorials!

Thanks so much for visiting my site! If this article helped you achieve your goal and you want to say thanks, you can now support my work by buying me a coffee. I promise I won't spend it on beer instead... 😏

2 thoughts on “Home Assistant Push Button Dimmer (Simple Template Tutorial)”

  1. good work, thank you! 🙂

    Did you ever though about adding physical button to dimmer component in ESPhome? I did something similar years ago for MySensors, but now would like to switch to ESPhome.

    I did it based on Anduril UI (one button control for flashlights):
    – click = toggle on/off
    – long press = dimm or brighten the light
    – double click = set to 100%

    1. Hello. Have you found a solution to the problem? I’m currently solving this problem for lighting in the bathroom. According to the scenario, at night the brightness is 30%, during the day the brightness is 70%. This problem is solved by an automation algorithm. Activation is carried out by a sensor in the door and a presence sensor. But sometimes you need to increase (or decrease) the brightness with the press of a button.

Leave a Comment

Your email address will not be published. Required fields are marked *


Scroll to Top