Home

Awesome

Variables+History

aka. variable

<img alt="Variable Logo" src="https://github.com/enkama/hass-variables/raw/master/logo/icon.png">

A Home Assistant Integration to declare and set/update variables.

Forked and updated from initial integration developed by rogro82

Home Assistant versions starting with v2024.7.2 will prevent Variables+History versions earlier than v3.4.5 from working. Be sure to upgrade.

Upgrading from v2 to v3

Existing variables will remain as yaml variables but instead of starting with variable., they will now start with sensor. If you would like to manage the variable using the UI configuration, you will need to delete the entity from your yaml and recreate it in the UI. This is also the only change needed when migrating from rogro82's version to this one

Installation

HACS (recommended)

  1. Ensure that HACS is installed
  2. Click Here to directly open Variables+History in HACS or<br/>   a. Navigate to HACS<br/>   b. Click + Explore & Download Repositories<br/>   c. Find the Variables+History integration <br/>
  3. Click Download
  4. Restart Home Assistant
  5. See Configuration below
<details> <summary><h3>Manual</h3></summary>

You probably do not want to do this! Use the HACS method above unless you know what you are doing and have a good reason as to why you are installing manually

  1. Using the tool of choice open the directory (folder) for your HA configuration (where you find configuration.yaml)
  2. If you do not have a custom_components directory there, you need to create it
  3. In the custom_components directory create a new folder called variable
  4. Download all the files from the custom_components/variable/ directory in this repository
  5. Place the files you downloaded in the new directory you created
  6. Restart Home Assistant
  7. See Configuration below
</details>

<a name="configuration"></a>Preferred Configuration

  1. Click Here to directly add a Variables+History sensor or<br/>   a. In Home Assistant, go to Settings -> Integrations<br/>   b. Click + Add Integrations and select Variables+History<br/>
  2. Add your configuration (see Configuration Options below)
  3. Click Submit

Configuration Options

First choose the variable type.

<details> <summary><h3>Sensor</h3></summary>
NameRequiredDefaultDescription
Variable IDYesThe desired id of the new sensor (ex. test_variable would create an entity_id of sensor.test_variable)
NameNoFriendly name of the variable sensor
IconNomdi:variableIcon of the Variable
Initial ValueNoInitial value/state of the variable. If Restore on Restart is False, the variable will reset to this value on every restart
Initial AttributesNoInitial attributes of the variable. If Restore on Restart is False, the variable will reset to this value on every restart
Restore on RestartNoTrueIf True will restore previous value on restart. If False, will reset to Initial Value and Initial Attributes on restart
Force UpdateNoFalseVariable's last_updated time will change with any service calls to update the variable even if the value does not change
Exclude from RecorderNoFalseFor Variables with large attributes (>16 kB), enable this to prevent Recorder Errors.
</details> <details> <summary><h3>Binary Sensor</h3></summary>
NameRequiredDefaultDescription
Variable IDYesThe desired id of the new binary sensor (ex. test_variable would create an entity_id of binary_sensor.test_variable)
NameNoFriendly name of the variable binary sensor
IconNomdi:variableIcon of the Variable
Initial ValueNoFalseInitial True/False value/state of the variable. If Restore on Restart is False, the variable will reset to this value on every restart
Initial AttributesNoInitial attributes of the variable. If Restore on Restart is False, the variable will reset to this value on every restart
Restore on RestartNoTrueIf True will restore previous value on restart. If False, will reset to Initial Value and Initial Attributes on restart
Force UpdateNoFalseVariable's last_updated time will change with any service calls to update the variable even if the value does not change
Exclude from RecorderNoFalseFor Variables with large attributes (>16 kB), enable this to prevent Recorder Errors.
</details> <details> <summary><h3>Device Tracker</h3></summary>
NameRequiredDefaultDescription
Variable IDYesThe desired id of the new device tracker (ex. test_variable would create an entity_id of device_tracker.test_variable)
NameNoFriendly name of the variable device tracker
IconNomdi:variableIcon of the Variable
Initial LatitudeYesLatitude
Initial LongitudeYesLongitude
Initial Location NameNoIf set, will show this as the state
Initial GPS AccuracyNoAccuracy in meters
Initial Battery LevelNoBattery level from 0-100%
Initial AttributesNoInitial attributes of the variable
Restore on RestartNoTrueIf True will restore previous value on restart. If False, will reset to Initial Latitude, Initial Longitude, Initial Location Name, Initial GPS Accuracy, Initial Battery Level, and Initial Attributes on restart
Force UpdateNoFalseVariable's last_updated time will change with any service calls to update the variable even if the value does not change
Exclude from RecorderNoFalseFor Variables with large attributes (>16 kB), enable this to prevent Recorder Errors.
</details> <details> <summary><h2>Alternate YAML Configuration</h2></summary>

Variables created via YAML will all start with sensor. and cannot be edited in the UI.

You can have a combination of Variables created via the UI and via YAML.

Add the component variable to your configuration and declare the variables you want.

NameyamlRequiredDefaultDescription
Variable ID<key>:YesThe desired id of the new sensor (ex. test_variable would create an entity_id of sensor.test_variable)
NamenameNoFriendly name of the variable sensor
Initial ValuevalueNoInitial value/state of the variable. If Restore on Restart is False, the variable will reset to this value on every restart
Initial AttributesattributesNoInitial attributes of the variable. If Restore on Restart is False, the variable will reset to this value on every restart
Restore on RestartrestoreNoTrueIf True will restore previous value on restart. If False, will reset to Initial Value and Initial Attributes on restart
Force Updateforce_updateNoFalseVariable's last_updated time will change with any service calls to update the variable even if the value does not change
Exclude from Recorderexclude_from_recorderNoFalseFor Variables with large attributes (>16 kB), set to True to prevent Recorder Errors.

Example:

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
  current_power_usage:
    force_update: true

  daily_download:
    value: 0
    restore: true
    attributes:
      state_class: measurement
      unit_of_measurement: GB
      icon: mdi:download
</details>

Services

There are instructions and selectors when the service is called from the Developer Tools or within a Script or Automation.

variable.update_sensor

Used to update the value or attributes of a Sensor Variable

NameKeyRequiredDefaultDescription
Targetstarget:<br />  entity_id:YesThe entity_ids of one or more sensor variables to update (ex. sensor.test_variable)
New ValuevalueNoValue/state to change the variable to
New AttributesattributesNoWhat to update the attributes to
Replace Attributesreplace_attributesNoFalseReplace or merge current attributes (False = merge)

variable.update_binary_sensor

Used to update the value or attributes of a Binary Sensor Variable

NameKeyRequiredDefaultDescription
Targetstarget:<br />  entity_id:YesThe entity_ids of one or more binary sensor variables to update (ex. binary_sensor.test_variable)
New ValuevalueNoValue/state to change the variable to
New AttributesattributesNoWhat to update the attributes to
Replace Attributesreplace_attributesNoFalseReplace or merge current attributes (False = merge)

variable.update_device_tracker

Used to update the value or attributes of a Device Tracker Variable

NameKeyRequiredDefaultDescription
Targetstarget:<br />  entity_id:YesThe entity_ids of one or more device tracker variables to update (ex. device_tracker.test_variable)
LatitudelatitudeNoLatitude
LongitudelongitudeNoLongitude
Location Namelocation_nameNoIf set, will show this as the state
Delete Location Namedelete_location_nameNoRemove the Location Name so state will be based on Lat/Long (boolean)
GPS Accuracygps_accuracyNoAccuracy in meters
Battery Levelbattery_levelNoBattery level from 0-100%

variable.toggle_binary_sensor

Used to toggle the state or update attributes of a Binary Sensor Variable. If the binary_sensor state is None, the toggle service will not change the state.

NameKeyRequiredDefaultDescription
Targetstarget:<br />  entity_id:YesThe entity_ids of one or more binary sensor variables to toggle (ex. binary_sensor.test_variable)
New AttributesattributesNoWhat to update the attributes to
Replace Attributesreplace_attributesNoFalseReplace or merge current attributes (False = merge)
<details> <summary><h2>Legacy Services</h2></summary>

These will only work for Sensor Variables

These services are from the previous version of the integration and are being kept for pre-existing automations and scripts. In general, the new variable.update_ and variable.toggle_ services above should be used going forward.

Both services are similar and used to update the value or attributes of a Sensor Variable. variable.set_variable uses just the variable_id and variable.set_entity uses the full entity_id. There are instructions and selectors when the service is called from the Developer Tools or within a Script or Automation.

variable.set_variable

NameKeyRequiredDefaultDescription
Variable IDvariableYesThe id of the sensor variable to update (ex. test_variable for a sensor variable of sensor.test_variable)
ValuevalueNoValue/state to change the variable to
AttributesattributesNoWhat to update the attributes to
Replace Attributesreplace_attributesNoFalseReplace or merge current attributes (False = merge)

variable.set_entity

NameKeyRequiredDefaultDescription
Entity IDentityYesThe entity_id of the sensor variable to update (ex. sensor.test_variable)
ValuevalueNoValue/state to change the variable to
AttributesattributesNoWhat to update the attributes to
Replace Attributesreplace_attributesNoFalseReplace or merge current attributes (False = merge)
</details>

Example service calls

action:
  - service: variable.update_sensor
    data:
      value: 30
    target:
      entity_id: sensor.test_timer
action:
  - service: variable.update_sensor
    data:
      value: >-
        {{trigger.to_state.name|replace('Motion Sensor','')}}
      attributes:
        history_1: "{{states('sensor.last_motion')}}"
        history_2: "{{state_attr('sensor.last_motion','history_1')}}"
        history_3: "{{state_attr('sensor.last_motion','history_2')}}"
    target:
      entity_id: sensor.last_motion
action:
  - service: variable.update_binary_sensor
    data:
      value: true
      replace_attributes: true
      attributes:
        country: USA
    target:
      entity_id: binary_sensor.test_binary_var

Example timer automation

script:
  schedule_test_timer:
    sequence:
      - service: variable.update_sensor
        data:
          value: 30
        target:
          entity_id: sensor.test_timer
      - service: automation.turn_on
        data:
          entity_id: automation.test_timer_countdown

automation:
  - alias: test_timer_countdown
    initial_state: 'off'
    trigger:
      - platform: time_pattern
        seconds: '/1'
    action:
      - service: variable.update_sensor
        data:
          value: >
            {{ [((states('sensor.test_timer') | int(default=0)) - 1), 0] | max }}
        target:
          entity_id: sensor.test_timer
  - alias: test_timer_trigger
    trigger:
      platform: state
      entity_id: sensor.test_timer
      to: '0'
    action:
      - service: automation.turn_off
        data:
          entity_id: automation.test_timer_countdown

Examples

<details> <summary><h3>Play and Save TTS Messages + Message History - Made by <a href="https://github.com/jazzyisj">jazzyisj</a></h3></summary>

https://github.com/jazzyisj/save-tts-messages

This is more or less an answering machine (remember those?) for your TTS messages. When you play a TTS message that you want saved under certain conditions (i.e. nobody is home), you will call the script Play or Save TTS Message script.play_or_save_message instead of calling your tts service (or Alexa notify) directly. The script will decide whether to play the message immediately, or save it based on the conditions you specify. If a saved tts message is repeated another message is not saved, only the timestamp is updated to the most recent instance.

Messages are played back using the Play Saved TTS Messages script "script.play_saved_tts_messages". Set an appropriate trigger (for example when you arrive home) in the automation Play Saved Messages automation.play_saved_messages automation to call this script automatically.

Saved messages will survive restarts.

BONUS - OPTIONAL TTS MESSAGE HISTORY

You can find the full documentation on how to do this and adjust this to your needs in here.

</details>

More examples can be found in the examples folder.