Script not saving after editing in yaml

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

Help!

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.

Also the very flexible Ashley’s light fader script might be worth your time to take a look at.

1 Like

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.

YAML is the configuration structure/syntax used by Home Assistant.

Jinja is the templating language used by Home Assistant (that’s what all those {{...}} things are).

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

Thank you so much. I will give this a try.

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:
    • sun
    • mon
    • tue
    • wed
    • thu
    • fri
    • sat
      action:
  • service: script.1707096702958
    metadata: {}
    data: {}
    response_variable: brightness
  • service: light.turn_on
    target:
    entity_id: light.kitchen_main_lights
    data:
    brightness_pct: ‘{{ brightness }}’
    mode: single

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:

...
  - service: light.turn_on
    target:
      entity_id: light.kitchen_main_lights
    data:
      brightness_pct: '{{ brightness.value | float(0) }}'
mode: single

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.

Why not use the easy option of a custom made Integration for exactly this?

Sorry I meant Automation. And there is only “download trace” and no “download json”. I screen shot that page here.


I hope this would suffice since I don’t know how to properly format the coding in this forum.

I changed the time on line 11 to “12:00” only for testing purpose, so I can tests this during the day.

I think it used to be call “Download json”
 but “Download trace” is what you should post.

No, we need to be able to see the what part is failing and why
 Community Guidelines - How to Ask a Good Question - Format it Correctly

That would involve downloading something from Github? Another can of warm I am not ready to open yet.

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.

NO, it’s in HACS.