Skip to content

PiotrMachowski/Home-Assistant-custom-components-Saver

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

77 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

HACS Default GitHub Latest Release GitHub All Releases Installations Community Forum Ko-Fi buycoffee.to PayPal.Me Revolut.Me

Saver

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.

Installation

Using HACS (recommended)

Open your Home Assistant instance and open a repository inside the Home Assistant Community Store.

This integration can be installed using HACS. To do it search for Saver in Integrations section.

Manual

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.zip

Configuration

Using UI

Open your Home Assistant instance and start setting up a new integration.

From 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.

YAML (Deprecated)

Add following code in configuration.yaml:

saver:

Available services

Save state

Saves the state and parameters of the entity. Supports saving multiple entities.

service: saver.save_state
data:
  entity_id: cover.living_room

Restore state

Restores saved states (only for entities that support it). Supports restoring multiple entities.

service: saver.restore_state
data:
  entity_id: cover.living_room

Optional 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: 3

Delete saved state

Deletes a saved state for an entity.

service: saver.delete
data:
  entity_id: cover.living_room

Delete saved states by regex

Deletes saved states for entities that match provided regex.

service: saver.delete_regex
data:
  entity_id_regex: light.living_room.*

Set variable

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: 3

Use the current state of an entity:

service: saver.set_variable
data:
  name: living_room_temp
  value_entity: sensor.living_room_temperature

Store the current date and time (ISO-8601, supports >24h comparisons):

service: saver.set_variable
data:
  name: timer_start
  use_current_time: true

Delete variable

Deletes a saved variable.

service: saver.delete_variable
data:
  name: counter

Delete variables by regex

Deletes saved variables that match provided regex.

service: saver.delete_variable_regex
data:
  regex: "counter_\\d+"

Clear

Deletes all saved data.

service: saver.clear

Using saved values in templates

It is possible to use saved data in templates using the saver namespace object:

Basic access

{{ 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" entity

Time comparisons

Compare 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

General comparisons

{{ 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 10

Elapsed time

Returns 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 passed

Legacy functions

The 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") }}

Automation Conditions

Saver provides custom automation conditions that appear in the Home Assistant automation UI. These mirror the template comparison functions and can be added visually.

Time elapsed

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 ago
condition: saver.time_elapsed
options:
  variable: timer_start
  above: 60
  below: 3600          # between 1 minute and 1 hour

Compare value

Compare 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 time

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_now
condition: saver.compare_time
options:
  variable: timer_start
  comparison: after
  compare_to: "12:05:00"

Events

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.

Support

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
  • PayPal
  • Credit card
  • Buy Me a Coffee at ko-fi.com
  • No fees
  • Single or monthly payment
  • buycoffee.to
  • BLIK
  • Bank transfer
  • Postaw mi kawę na buycoffee.to
    PayPal
  • PayPal
  • PayPal Logo
  • No fees
  • Revolut
  • Revolut
  • Credit Card
  • Revolut
  • No fees
  • Powered by

    PyCharm logo.

    About

    This custom component allows you to save current state of any entity and use its data later to restore it.

    Topics

    Resources

    License

    Stars

    Watchers

    Forks

    Packages

     
     
     

    Contributors

    Languages