A Home Assistant component to declare and set/update variables (state).
Copy variable.py to your home-assistant custom_components folder
Add the component variable
to your configuration and declare the variables you want.
variable:
countdown_timer:
value: 30
attributes:
friendly_name: 'Countdown'
icon: mdi:alarm
countdown_trigger:
name: Countdown
value: False
light_scene:
value: 'normal'
attributes:
previous: ''
restore: true
A variable 'should' have a value and can optionally have a name and attributes, which can be used to specify additional values but can also be used to set internal attributes like icon, friendly_name etc.
In case you want your variable to restore its value after restarting you can set restore to true.
To update a variables value and/or its attributes you can use the service call variable.set_variable
The following parameters can be used with this service:
- variable: string (required) The name of the variable to update
- value: any (optional) New value to set
- value_template: template (optional) New value to set from a template
- attributes: dictionary (optional) Attributes to set or update
- attributes_template: template (optional) Attributes to set or update from a template ( should return a json object )
- replace_attributes: boolean ( optional ) Replace or merge current attributes (default false = merge)
action:
- service: variable.set_variable
data:
variable: test_timer
value: 30
action:
- service: variable.set_variable
data:
variable: last_motion
value: "livingroom"
attributes_template: >
{
"history_1": "{{ variable.state }}",
"history_2": "{{ variable.attributes.history_1 }}",
"history_3": "{{ variable.attributes.history_2 }}"
}
variable:
test_timer:
value: 0
attributes:
icon: mdi:alarm
script:
schedule_test_timer:
sequence:
- service: variable.set_variable
data:
variable: test_timer
value: 30
- service: automation.turn_on
data:
entity_id: automation.test_timer_countdown
automation:
- alias: test_timer_countdown
initial_state: 'off'
trigger:
- platform: time
seconds: '/1'
action:
- service: variable.set_variable
data:
variable: test_timer
value_template: '{{ [((variable.state | int) - 1), 0] | max }}'
- alias: test_timer_trigger
trigger:
platform: state
entity_id: variable.test_timer
to: '0'
action:
- service: automation.turn_off
data:
entity_id: automation.test_timer_countdown
More examples can be found in the examples folder.