I’m looking for a few good examples of how to use a variable in automation.
When a condition is hit, I would like to store the brightness of a device in a variable, change the brightness of that device to 100%, wait for a while, then change the brightness back to the previously value.
When I refer to the brightness in a condition, it is described as folows…
entity_id: light.fanlinc_32_33_35_light
attribute: brightness
How can I assign a variable to this brigthness in one step and then set it back to that variable in a later step?
Thanks,
you should think a little differently about it. varibles are set for the scope of where they are declared and don’t vary (ironic eh?). unless you’re using jinja template varaibles, but that’s not suitable for you because it’s scope is w/in the one template…
so instead of thinking about saving it in one step in the middle of a squence. think about it in terms of “remembering what it was at the start of the scope”
so it becomes more like this:
description: ""
trigger:
- platform: state
entity_id:
- input_boolean.testbool
to: null
condition: []
action:
- service: light.turn_on
data:
brightness: 40
target:
entity_id: light.study_lights_2
- delay:
seconds: 10
- service: light.turn_on
data:
brightness: |
{{ saved_brightness }}
target:
entity_id: light.study_lights_2
variables:
saved_brightness: |
{{ state_attr('light.study_lights_2', 'brightness') }}
note that i put the variable save_brightness at the very bottom. i don’t have to do that. could put it up at the top. however i put it at the bottom just to illustrate that it’s about defining the value within the scope. not at a point in the sequence.
2 Likes
Thanks. That clarifies a lot and I got it working.