Skip to content

Instantly share code, notes, and snippets.

@dale3h
Created October 24, 2019 21:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dale3h/6c7d64efefe3551bd5efc7910254a112 to your computer and use it in GitHub Desktop.
Save dale3h/6c7d64efefe3551bd5efc7910254a112 to your computer and use it in GitHub Desktop.
[Home Assistant] Light Brightness Scripts
################################################################################
## Scripts to Adjust Light Brightness
################################################################################
################################################
## Example Usage
################################################
# - service: script.increase_brightness
# data:
# entity_id: light.living_room
# - service: script.decrease_brightness
# data:
# entity_id: light.living_room
################################################
## Scripts
################################################
# Increase the brightness of a light. If the light is off, it will go to maximum brightness.
increase_brightness:
alias: Increase Light Brightness
sequence:
- service: light.turn_on
data_template:
entity_id: "{{ entity_id }}"
brightness: >-
{% set brt_min = 5 %}
{% set brt_max = 254 %}
{% set brt_steps = 6 %}
{% set brt_step = ((brt_max - brt_min) / brt_steps)|round(0, 'floor') %}
{% set brightness = state_attr(entity_id, 'brightness')|int(0) %}
{% if brightness %}
{# Increase brightness one step. #}
{{ (brightness + brt_step, brt_max)|min }}
{% else %}
{# Set the brightness to max if light is off. #}
{{ brt_max }}
{% endif %}
# Decrease the brightness of a light. If the light is off, it will go to minimum brightness.
decrease_brightness:
alias: Decrease Light Brightness
sequence:
- service: light.turn_on
data_template:
entity_id: "{{ entity_id }}"
brightness: >-
{% set brt_min = 5 %}
{% set brt_max = 254 %}
{% set brt_steps = 6 %}
{% set brt_step = ((brt_max - brt_min) / brt_steps)|round(0, 'floor') %}
{% set brightness = state_attr(entity_id, 'brightness')|int(0) %}
{% if brightness %}
{# Decrease brightness one step. #}
{{ (brightness - brt_step, brt_min)|max }}
{% else %}
{# Set the brightness to min if light is off. #}
{{ brt_min }}
{% endif %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment