The Goal:
I want to create an automation that controls the brightness of light according to a schedule, like a circadian rhythm. Specifically I want a group of light to start dimming gradually after 9 pm, by 4% every 10 minutes until it reacheas 28% at 12am. If the light is still on after that, it would not further dim. If the light was off and then turned on after 9 pm, say at 10 pm, it would automatically starts at 76% brightness ( six 10 minutes blocks, each dim by 4%).
What I have so far:
I have no programming skill whatsoever, so this is a steep learning curve. I have managed to write a template that takes current time and figure out the time difference between now and 9pm, and change that number from seconds to number of 10 minute blocks. When I try a new automation, the service called âlight:turn onâ allows custom action such as setting a brightness. Except it is a number you put in and unchangeable. I want to run a script that uses the template I created to generate the correct brightness level.
The problem:
When I tried to create a script, it is in UI format. When I use âedit in yamlâ, I can insert the copy what I wrote in template and paste in the yaml file. Except it doesnât save and doesnât run. I am sure I have made a mistake somewhere, just donât know what.
alias: Set Dimmer level
sequence: []
variables: |
{% set t1 = "12:00" %}
{% set t2 = as_timestamp(now()) %}
{% set t3 = today_at(t1)|as_timestamp %}
{% set t4 = t2 - t3 %}
# t4 is lapsed time in seconds.
{{ t2, t3, t4|int}}
# t5 is lapsed time in 10 minute blocks.
{% set t5 = (t4/600)|round(0, floor) %}
{{ t5 }}
# determine appropriate brightness level by decreasing 4% every 10 minutes.
{% set brightness_pct = 100 - 4 * t5 %}
{{ brightness_pct }}
brightness_pct: {{ {'value': brightness_pct} }}
mode: single
You are confusing script variables with Jinja variables and your configuration is very confusing. What is this script actually supposed to be doing? As it stands, it looks like itâs just defining a variable for itself then stopping⊠if you are trying to have the script respond, you need to include a Stop action.
alias: Set Dimmer level
sequence:
- variables:
brightness: >
{% set t1 = "12:00" %}
{% set t2 = now() %}
{% set t3 = today_at(t1) %}
{# t4 is lapsed time in seconds. #}
{% set t4 = (t2 - t3).total_seconds() %}
{# t5 is lapsed time in 10 minute blocks. #}
{% set t5 = (t4/600)|round(0, floor) %}
{# determine appropriate brightness level by decreasing 4% every 10
minutes. #}
{% set brightness_pct = 100 - 4 * t5 %}
{{ {'value': brightness_pct} }}
- stop: Return dimmer value
response_variable: brightness
mode: queued
FWIW, there are a number of Blueprints for light fading and circadian lighting.
I donât know what Jinja or Jinja2 is. Is it a variant of yaml? Regardless, how do I format the script so it return a variable so can be inserted into brightness pct field in the light turn on service?
Thanks for replying. This script is supposed to return a number called âbrightness_pctâ which I can somehow insert into the automation when it calls for light turn of service.
I will also look into blueprints exchange to see if someoneelse had already done this.
If the responding script isnât being given different inputs by each calling script/automation, there really isnât a point to using a responding script⊠you can just use a template sensor.
Regarding Jinja vs YAML, does this mean you can not copy/paste a template into a script? I watched a video here (Scripts - Home Assistant) where he seemed to use both, see time 9.59 in the video. Sorry I am very confused.
Donât worry about it. I didnât have and programming or computer science background when I started using Home Assistant, so I know how complex it can seem when youâre trying to wrap your head around it.
What I posted above is a template in a script, you just have to follow both the YAML rules and the Jinja rules. The sample from your original post was not following the rules for either.
In the video, Ed sets his script up a little differently than the one I posted above, but either will work. In his example he sets the variables up outside the action sequence like:
alias: Set Dimmer level
variables:
brightness: >
{% set t1 = "12:00" %}
{% set t2 = now() %}
{% set t3 = today_at(t1) %}
{# t4 is lapsed time in seconds. #}
{% set t4 = (t2 - t3).total_seconds() %}
{# t5 is lapsed time in 10 minute blocks. #}
{% set t5 = (t4/600)|round(0, floor) %}
{# determine appropriate brightness level by decreasing 4% every 10 minutes. #}
{% set brightness_pct = 100 - 4 * t5 %}
{{ {'value': brightness_pct} }}
sequence:
- stop: Return dimmer value
response_variable: brightness
mode: queued
Hi Drew, if you would indulge me. The script you help me with saved with no problem, except it doesnât run correctly. When I do a trace of where it went wrong, it shows "expected float for dictionary value @ data{âbrightness_pctâ].
id: â1706944419680â
alias: Kitchen Lights Dim
description: Dim LIghts gradually after 9 pm
trigger:
platform: state
entity_id:
light.kitchen_main_lights
to: âonâ
condition:
condition: time
after: â12:00:00â
before: â05:00:00â
weekday:
I am not sure what is the correct way to edit the last line, to enter the numeric value generated by the first script into the field for brightness_pct.
Remember that the actual percentage is stored in the variable value inside the dictionary being passed in the response. By using brightness, you are returning a dictionary, not a float⊠you need to return the percentage as a float:
I copied that last line into my script. Now it gives me this error: Error rendering data template: UndefinedError: âdict objectâ has no attribute âvalueâ
Do you mean the script or the automation? It should be in the automation, not the script. If that is the case, open the automation editor, at the top right click âTracesâ. If there are traces, click the 3-dot menu at the top right, select âDownload jsonâ and paste the contents here properly formatted.
Did you change the value for t1 in the script to 21:00 or whatever value is reasonable for testing based on your current local time? In the light.turn_on service call, the value for the template must render to between 0-100⊠or it will error out.
I tried download trace three times, each time it saved as json file. How do I upload that here? Yes I changed the starting time in the script to match the testing starting time.