Hey all. I’ve created a custom component called
var for generic variable entities. This component is basically a slightly more flexible template sensor. Variable entity attributes can be set manually from a service or dynamically through the use of templates and/or SQL queries. All of a variable’s attributes (including dynamic template attributes) can be set using the
var.set service. Variables can be displayed in Lovelace in all of the same places that sensors are displayed. Each variable entity contains one value which can be of any type (a tuple, for example).
Variables can be updated based on events, including the state changed events of other entities.
I’ve found this component to be a bit more convenient than template sensors which require separate backing data.
I should also note that there is another custom component for variables that is floating around, “hass-variables” by rogro82. Their component has different aims than this one, and there were a few things I wanted to do differently which is why I made this one.
Let me know what you think!
- Create variable entities by adding them under
- Set the value of one or more variables using the
var.setservice - values can be computed using
- Set any other variable attributes using the
entity_picture) - these can be computed using
- Specify a
value_templateand a variable will update dynamically using that template
- Specify attribute templates (e.g.,
entity_picture_template) and those attributes will update dynamically
- Display variables in Lovelace UI in the same way as other sensor components
- Update a variable whenever the state of one or more specified entities changes (
- Update a variable whenever one or more specified events fire (
- Update templates (e.g.,
friendly_name_template) dynamically using
- Update the value of a variable using an SQL query (useful for history statistics)
- Use the results of an SQL query in a template
- It was tedious to create a corresponding separate template sensor for each entity in the UI.
- I wanted a single general-purpose component, with a generic name, that could be used to store, update, and display values using templates.
- I didn’t like using named UI components to store first-class data (e.g.
- I wanted to be able to work with data directly from the home assistant database (especially custom events) without having to create and flip-flop between a bunch of different entities.
- I wanted a custom component that I could extend with more features in the future.