Global Variables in Home Assistant
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/Section | Can Use Variables | Notes | sensor | read | write | |
|---|---|---|---|---|---|---|
| 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:
| Limitation | Workaround | read | write | ||
| globally accessible | Use input_* helpers | input_text can handle max 255 characters limit.so anything more than shows an error in the logs. | detail | ||
| Use dict() | |||||
| Complex types can be tricky | Use json | json file |
|
| 1.use templating json
|
| Using the “Variable” custom integration(rogro82) | use custom integration https://github.com/rogro82/hass-variables | ||||
| use state object | 1.detail | ||||
| use template sensor | 1.detail | ||||
| use Trigger template sensor | Handle custom events | 1.detail | |||
| Mqtt sensor | |||||
| RESTful Sensor and RESTful Binary Sensor |
| post http://homeassistant.local:8123/api/states/:entity_id | 1.detail | ||
| command_line sensor | json | 1.detail |
json file
| Limitation | Workaround | read | write | ||
| json file | Integration | Integration | detail | ||
| json data via jija templating | /data/homeassistant202405/custom_template/stations.jinja | json file |
|
| 1.use templating json
|
| local json file | Reading JSON from a file using Python | detail | | | |
| use rest_command integration | This integration can expose regular REST commands as actions. Actions can be called from a script or in automation
2.detail | 1.detail | |||
| use file integration | 1.detail | ||||
| use python_script integration | Handle custom events | 1.detail | |||
| Mqtt sensor | |||||
| json file + template sensor in configuration.yaml | | ||||
| 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 | 1.detail | ||
| http json+rest_command | https://www.home-assistant.io/integrations/rest_command | |
| 1.detail 2.detail | |
| local json file+command_line | command_line sensor | json | | 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