There are no global variables in Home Assistant that allow you to store and update data as a list.

 

Where You Can Use Variables

Feature/SectionCan Use VariablesNotes sensorreadwrite
Automations✅ ✓

1.Great for triggers, conditions, and actions

2.When the automation ends, the variable disappears (not persisted).

    
Scripts✅ ✓Especially with variables: block    
Templates✅ ✓Via Jinja2 templating engine

1.use a Trigger-based Template Sensor detail1

2.config/custom_templates.detail

 

 

 

 

UI (Lovelace)❌ ✘Variables don’t persist in UI unless part of a custom card    
Scenes❌ ✘Static by design    
input_text  input_text can handle max 255 characters    
Using a Static JSON File in Home Assistant  detail   

source:https://blog.usro.net/2025/05/mastering-home-assistant-variables-for-smarter-automations/

 

Note

1.Variables are local to the automation or script

Variables in a script do not persist across automation calls. They’re session-local.

2.Can’t define inside condition,Pre-calculate in variables:

 

 

LimitationWorkaround readwrite 
globally accessibleUse input_* helpersinput_text can handle max 255 characters limit.so anything more than shows an error in the logs.  detail
 Use dict()     
Complex types can be trickyUse json

json file

 

 

1.use templating json

 

Using the “Variable” custom integration(rogro82)   

use custom integration

https://github.com/rogro82/hass-variables

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 }}"
        }
 
 use state object   1.detail
 use template sensor   1.detail
 use Trigger template sensor  Handle custom events1.detail
 Mqtt sensor    
 RESTful Sensor and RESTful Binary Sensor 

 

post

http://homeassistant.local:8123/api/states/:entity_id

{
    "state": "below_horizon",
    "attributes": {
        "next_rising":"2016-05-31T03:39:14+00:00",
        "next_setting":"2016-05-31T19:16:42+00:00"
    }
}
1.detail
 command_line sensorjson  1.detail

json file

LimitationWorkaround readwrite 
json file  IntegrationIntegrationdetail
json data via jija templating

/data/homeassistant202405/custom_template/stations.jinja

{% macro stations_data() %}
[
    {
      "name": "Station One",
      "url": "https://one.example.com",
      "inital_volume": 120
    },
    {
      "name": "Radio two",
      "url": "https://two.example.com",
      "inital_volume": 100
    },
    {
      "name": "LiveTalk",
      "url": "https://three.example.com",
      "inital_volume": 110
    }
  ]
{% endmacro %}

json file

 

 

1.use templating json

 

local json fileReading JSON from a file using Pythondetail
import json

# Read from file and parse JSON
with open("sample.json", "r") as f:
    data = json.load(f)

print(data)
print(type(data))
import json
data = {
    "name": "sathiyajith",
    "rollno": 56,
    "cgpa": 8.6,
    "phone": "9976770500"
}

with open("sample.json", "w") as f:
    json.dump(data, f)
 
 use rest_command integration

This integration can expose regular REST commands as actions. Actions can be called from a script or in automation

detail

 

2.detail

  1.detail
 use file integration   1.detail
 use python_script integration  Handle custom events1.detail
 Mqtt sensor    
json file + template sensor in configuration.yaml  
template:
  - binary_sensor:
      - name: "Chinese Holiday Today"
        state: >
          {% set file = '/config/data/holiday-cn-2025.json' %}
          {% set data = read_file(file) | from_json %}
          {% set today = now().strftime('%Y-%m-%d') %}
          {% set holiday = data | selectattr('date', 'equalto', today) | list %}
          {{ holiday | count > 0 }}
        attributes:
          friendly_name: "Is today a Chinese holiday?"
  

json file +restful api template sensor in configuration.yaml

https://www.home-assistant.io/integrations/sensor.rest/

  

 

post

http://homeassistant.local:8123/api/states/:entity_id

{
    "state": "below_horizon",
    "attributes": {
        "next_rising":"2016-05-31T03:39:14+00:00",
        "next_setting":"2016-05-31T19:16:42+00:00"
    }
}
1.detail

http json+rest_command

https://www.home-assistant.io/integrations/rest_command 
rest_command:
  secured_command:
    url: "http://example.com/api/secure-endpoint"
    method: post
    authentication: digest
    username: "your_username"
    password: "your_password"
    payload: '{"data": "example"}'
    content_type: "application/json"

 

1.detail

2.detail

local json file+command_linecommand_line sensorjson
command_line:
  - sensor:
      name: JSON user
      command: python3 -c "import requests; print(requests.get('https://jsonplaceholder.typicode.com/users').text)"
      json_attributes_path: "$.[0].address"
      json_attributes:
        - street
        - suite
        - city
        - zipcode
      value_template: "{{ value_json[0].name }}"
 1.detail

 

Conclusion

In Home Assistant, "global variables" are typically implemented using Helper entities, specifically Input Booleans, Input Numbers, or Input Texts. These entities act as containers for values that can be accessed and modified by various automations, scripts, and even displayed in the user interface.

 

useful links

https://blog.usro.net/2025/05/mastering-home-assistant-variables-for-smarter-automations/

http://localhost:4999/boards/topic/37186/global-variables-in-home-assistant-yaml#56820

Comments


Comments are closed