This custom component allows you to save current state of any entity and use its data later to restore it.
Additionally, you can create simple variables and use their values in scripts.
Using HACS (recommended)
This integration can be installed using HACS.
To do it search for Saver in Integrations section.
To install this integration manually you have to download saver.zip and extract its contents to config/custom_components/saver directory:
mkdir -p custom_components/saver
cd custom_components/saver
wget https://github.com/PiotrMachowski/Home-Assistant-custom-components-Saver/releases/latest/download/saver.zip
unzip saver.zip
rm saver.zipFrom the Home Assistant front page go to Configuration and then select Integrations from the list.
Use the "plus" button in the bottom right to add a new integration called Saver.
The success dialog will appear or an error will be displayed in the popup.
Add following code in configuration.yaml:
saver:Saves the state and parameters of the entity. Supports saving multiple entities.
service: saver.save_state
data:
entity_id: cover.living_roomRestores saved states (only for entities that support it). Supports restoring multiple entities.
service: saver.restore_state
data:
entity_id: cover.living_roomOptional attributes:
delete_after_run: allows to disable removing saved states after restoring them (default:true).transition: Number that represents the time (in seconds) the light should take to transition to the new state.
service: saver.restore_state
data:
entity_id:
- cover.living_room
- light.living_room
delete_after_run: false
transition: 3Deletes a saved state for an entity.
service: saver.delete
data:
entity_id: cover.living_roomDeletes saved states for entities that match provided regex.
service: saver.delete_regex
data:
entity_id_regex: light.living_room.*Sets a variable to a manual value, an entity state, or the current timestamp. Only one of value, value_entity or use_current_time can be used at a time.
Manual value:
service: saver.set_variable
data:
name: counter
value: 3Use the current state of an entity:
service: saver.set_variable
data:
name: living_room_temp
value_entity: sensor.living_room_temperatureStore the current date and time (ISO-8601, supports >24h comparisons):
service: saver.set_variable
data:
name: timer_start
use_current_time: trueDeletes a saved variable.
service: saver.delete_variable
data:
name: counterDeletes saved variables that match provided regex.
service: saver.delete_variable_regex
data:
regex: "counter_\\d+"Deletes all saved data.
service: saver.clearIt is possible to use saved data in templates using the saver namespace object:
{{ saver.variable("counter") }} # returns saved variable "counter"
{{ saver.entity("sun.sun") }} # returns saved state of "sun.sun" entity
{{ saver.entity("sun.sun", "azimuth") }} # returns "azimuth" attribute of saved "sun.sun" entityCompare a saved variable against a time or datetime value. Supports HH:MM, HH:MM:SS and full ISO-8601 datetime (2024-01-15T14:30:00). Plain time values are combined with today's date for comparison. When using use_current_time: true, the full datetime (including date) is stored, enabling comparisons across days.
{{ saver.cmp_time_after("timer_start", "12:05:00") }} # true if saved time is after 12:05 today
{{ saver.cmp_time_before("timer_start", "18:00") }} # true if saved time is before 18:00 today
{{ saver.cmp_time_after("timer_start", "2024-06-15T14:30:00") }} # compare against full datetime
{{ saver.cmp_time_after_now("timer_start") }} # true if saved datetime is after current datetime{{ saver.cmp_eq("status", "active") }} # true if variable equals "active" (string comparison)
{{ saver.cmp_neq("status", "active") }} # true if variable does not equal "active"
{{ saver.cmp_gt("counter", "10") }} # true if variable is greater than 10 (numeric)
{{ saver.cmp_lt("counter", "10") }} # true if variable is less than 10
{{ saver.cmp_gte("counter", "10") }} # true if variable is greater than or equal to 10
{{ saver.cmp_lte("counter", "10") }} # true if variable is less than or equal to 10Returns the number of seconds elapsed since the datetime stored in a variable. The variable must contain a parseable time or datetime value (e.g. set via use_current_time: true). When using full datetime values, elapsed time works correctly across days.
{{ saver.time_elapsed("timer_start") }} # seconds since the stored time
{{ saver.time_elapsed("timer_start") > 3600 }} # true if more than 1 hour has passedThe previous saver_variable and saver_entity functions are still available for backwards compatibility:
{{ saver_variable("counter") }}
{{ saver_entity("sun.sun") }}
{{ saver_entity("sun.sun", "azimuth") }}Saver provides custom automation conditions that appear in the Home Assistant automation UI. These mirror the template comparison functions and can be added visually.
Check if the elapsed time since a stored datetime variable exceeds or falls below a threshold (in seconds). At least one of above or below must be set. If both are set, both must be satisfied (range check).
condition: saver.time_elapsed
options:
variable: timer_start
above: 1800 # more than 30 minutes agocondition: saver.time_elapsed
options:
variable: timer_start
above: 60
below: 3600 # between 1 minute and 1 hourCompare a stored variable against a value. Available operators: eq, neq, gt, lt, gte, lte. Numeric operators (gt, lt, gte, lte) convert both sides to numbers.
condition: saver.compare_value
options:
variable: counter
comparison: gt
value: "10"Compare a stored datetime variable against another time or the current time. Available comparisons: after, before, after_now. For after and before, a compare_to value is required (supports HH:MM, HH:MM:SS, or ISO-8601 datetime).
condition: saver.compare_time
options:
variable: timer_start
comparison: after_nowcondition: saver.compare_time
options:
variable: timer_start
comparison: after
compare_to: "12:05:00"After the completion of the services mentioned before, the following events are fired:
| Service Function | Event ID | Provided Arguments |
|---|---|---|
| save_state | event_saver_saved_entity | entity_id |
| restore_state | event_saver_restored | entity_id |
| delete | event_saver_deleted_entity | entity_id |
| delete_regex | event_saver_deleted_entity_by_regex | entity_id_regex, entities |
| clear | event_saver_cleared | |
| set_variable | event_saver_saved_variable | variable, value |
| delete_variable | event_saver_deleted_variable | variable |
| delete_variable_regex | event_saver_deleted_variable_by_regex | variables, regex |
The events can be used to trigger further automations that depend on the completion of the services. The documentation is provided here.
If you want to support my work with a donation you can use one of the following platforms:
| Platform | Payment methods | Link | Comment |
|---|---|---|---|
| Ko-fi |
|
|
|
| buycoffee.to |
|
|
|
| PayPal |
|
|
|
| Revolut |
|
|
|
