lexlex
January 25, 2025, 5:40pm
1
Hi guys. I have a little problem and need some help.
I want to set the brightness of a light entity with a variable. However it always throws an error and I can’t understand it.
Here is the error message:
This is how I set the variable under actions:
variables:
initial_brightness: >-
{{ (100 / 255 * state_attr('light.bathroom_lights', 'brightness')) |
round(0) }}
And this is how I want to set the brightness of the light entity:
alias: Reset brightness if lights are on
if:
- condition: device
type: is_on
device_id: 304b64c5e900bfe731168467dd096990
entity_id: cae3bc1dbb14c4fc2fd717a3b218f1d2
domain: light
then:
- action: light.turn_on
metadata: {}
data:
brightness_pct: "{{ initial_brightness }}"
target:
entity_id: light.bathroom_lights
I’ve followed the variables docs from HA but nothing helps. Would really appreciate if someone could give me a hint.
Thanks
Edwin_D
(Edwin D.)
January 25, 2025, 6:02pm
2
The second bit looks like a script(but not quire either)? If so, then you should pass the data to the script when calling.
action: script.reset_brightness_if_lights_are_on
data:
initial_brightness: "{{ initial_brightness }}"
If not, you should show more of your yaml. Most likely the variable is defined in a different scope, and therefore undefined in the place where you use it.
lexlex
January 25, 2025, 6:38pm
3
Hi Edwin_D, thanks for your reply. Here is the partially full yaml. I’ve cut the rest at the end.
YAML:
Summary
alias: Turn on Bathroom Lights by Motion
description: ""
triggers:
- type: motion
device_id: e6643a3eaeb54fea6d4195dd86fce4ee
entity_id: d51c9a2e90480a8d566d0a943968820f
domain: binary_sensor
trigger: device
conditions: []
actions:
- alias: Reset brightness if lights are on
if:
- condition: device
type: is_on
device_id: 304b64c5e900bfe731168467dd096990
entity_id: cae3bc1dbb14c4fc2fd717a3b218f1d2
domain: light
then:
- action: light.turn_on
metadata: {}
data:
brightness_pct: "{{ initial_brightness }}"
target:
entity_id: light.bathroom_lights
- action: notify.mobile_app_pixel_8_pro
metadata: {}
data:
title: 💡 auto turn off
message: clear_notification
data:
tag: BathroomLightsOff
channel: Toasts
progress: -1
progress_max: 10
alias: Clear grace period progress notification
- alias: Set Natural Light Scene if lights are off
if:
- condition: device
type: is_off
device_id: 304b64c5e900bfe731168467dd096990
entity_id: cae3bc1dbb14c4fc2fd717a3b218f1d2
domain: light
then:
- action: scene.turn_on
metadata: {}
data: {}
target:
entity_id: scene.bathroom_lights_natural_light
enabled: true
- alias: Wait for Sensor stopped detecting motion for 50s
wait_for_trigger:
- type: no_motion
device_id: e6643a3eaeb54fea6d4195dd86fce4ee
entity_id: d51c9a2e90480a8d566d0a943968820f
domain: binary_sensor
trigger: device
for:
hours: 0
minutes: 0
seconds: 50
- variables:
initial_brightness: >-
{{ (100 / 255 * state_attr('light.bathroom_lights', 'brightness')) |
round(0) }}
mode: restart
Edwin_D
(Edwin D.)
January 25, 2025, 7:18pm
4
Variables should not lt be part of the action array (note the - before it) but a level higher I think.
Also, if the light is off, the template will throw an error. It is likely that is what you are testing for (I can’t tell because of those wretched device conditions), but the template is evaluated when the light is off too. So I’d also add a | float(0) to it to prevent the error.
The automation trace should show the variable values, though you might need to download the trace json to see it.
lexlex
January 25, 2025, 11:54pm
5
I’ve looked at the documentation here and they say that the variables in the variables action can be templated.
I’ve also added the “float(0)” as you said but the error still persists.
First run when lights are off. Variable gets set properly:
Second run, when lights are on but with a different brightness level. The variable should set the brightness to the previous state:
Mayhem_SWE
(Magnus Helin)
January 26, 2025, 12:22am
6
Obviously you cannot use the variable before it exists.
Variables are not retained between automation runs.
lexlex
January 26, 2025, 1:25am
7
thank you for the tip! I thought they will be retained. I’ve created a Number Helper now and it works flawless.