Custom component to declare/set variables


Because I found myself creating more and more workarounds ( using input_booleans and sliders ) to get some basic things done through automations ( e.g. counters and saving/restoring state ) I wrote a small custom_component for variables.

It allows declaring variables (state) in the configuration and setting/updating variables through service calls.

Source can be found on github:

The readme on github contains all the information to get you set-up and examples for how to use it in automations.

A basic countdown timer example using variables:

    value: 0
      icon: mdi:alarm

      - service: variable.set_variable
          variable: test_timer
          value: 30
      - service: automation.turn_on
          entity_id: automation.test_timer_countdown

  - alias: test_timer_countdown
    initial_state: 'off'
      - platform: time
        seconds: '/1'
      - service: variable.set_variable
          variable: test_timer
          value_template: '{{ [((variable.state | int) - 1), 0] | max }}'

  - alias: test_timer_trigger
      platform: state
      entity_id: variable.test_timer
      to: '0'
      - service: automation.turn_off
          entity_id: automation.test_timer_countdown

I hope someone will have some use for it.


The component has been redesigned to match other components like input_select.
It has also been renamed to variable: to match the naming-scheme of other components.
As requested you can now set “restore: true” to restore a variables value after a restart.

Besides that i have also added some basic examples on github which might help out some users.

Component that just holds data
Looking for a variable component
MQTT Light control over the day
Configurable delay using templating
Lighting scene & Kodi automation - head scratcher
Custom counter component
Help with mqtt templating (from Domoticz)
Persistent Counters?
Dummy sensor whose state can be set by automations
Set counter value
Trying to configure motion history

This looks ace, I won’t have chance to try it out for a bit, but if it’s working as expected I think you should create a component for inclusion in HA as standard :+1:


Just what i have been missing in HA. This would be a great addition to the standard


Looks very interesting :smiley:


Playing with it at the moment.
It would be nice if the initial values (when changed from a service call) could be restored on HA restart.
Something like:

    value: 1234
    initial: True/False

If initial: True, the value is set on HA start, if False, value gets restored from DB.
Great work!



Its already on my todo-list as I also need it myself. It will require some changes but I hope to have it ready somewhere in the next few days.


The component has been redesigned to match other components like input_select.

It has also been renamed to variable: to match the naming-scheme of other components ( be aware that you will have to change every variables.{name} to variable.{name} in automations etc ).

As requested you can now set “restore: true” on a variable to restore its value after a restart. :sunglasses:

Besides that i have also added some basic examples on github which might help out some users.


Nice ill keep this in mind, should be useful


The counter component is partially covering the same ground.


It indeed covers one of its use cases, but variables are not limited to only numbers for timers/counters. They can hold any type of data/state. For some/most a counter component for basic timers will do, but I also use variables to store previous states etc in attributes ( e.g. light scenes ).

Its always nice to have options :smiley:


This will be another one:


^ I think the OP addresses many more possibilities, and in combination with the two components you mentioned will make a very powerful platform.


Thank you, looking forward to using this


Hey thanks for this component. Got me out of trouble yesterday when needing to sync up some template sensor values with a filtered set of mqtt key/value pairs. Variables to the rescue!

I do get the point that @fabaff is making above that there is a lot of parallel development going on in this space. In just the last week, I’ve seen PRs/Issues/Posts relating to variables, counters, timers, input_*s, thresholds etc. Feels like potential for duplication and/or disconnects.

I haven’t seen this component on the HA github yet so curious, @rogro82, if this has been discussed on the discord #devs channel ? Given how foundational this component could be, I’d be keen to know all is aligned as there are a bunch of other areas I’d like to adopt this.


This component is great, really simplified a problem I have had automating my work commute.

I have an irregular work commute - by bike, transit, and driving depending on the day to different places. Using variables and beacons, HA can keep track of what transit mode I am using currently (and also, which bike I took because why not).

    value: 'None'
    restore: true
      icon: mdi:transit-transfer
      name: "Transit Mode"
    value: 'None'
    restore: true
      icon: mdi:bike
      name: "Which Bike?"


I’ve set up Google Travel Times to pull all 3 transit modes from current location to my next work calendar event. HA can then tell me the fastest commute mode, but regardless of which I choose it will know based on what beacon goes with me and can tailor notifications accordingly. If I leave in my car, send driving directions and a photo of the map. If I leave on bike, send cycling directions and a photo of the destination in street view. If I take a bike to one appointment, send me a ‘leave now to avoid being late’ based on the cycling time to get to the next one. etc.

Am thinking I can add a simple ‘alarm’ for the bikes. If transit_mode is Bike, arm an alarm for the transit_bike that is out - get notifications when that beacon only is out of range

Anyways, still some work to do on this but it opens up a lot of possibilities


This is super handy and really flexible. What needs to happen to get this rolled into the standard distribution?


I also wanted to say thanks as this has helped me with my setup tremendously.


Hello @rogro82,

I just want to thank you for this great component. With it my HA now so more fluid and always in sync with my devices like after boot up. I really wish it’s added to standard components or it combined with the counter; as I use both the counter and this one for different things.

I do have a couple of questions though and they are thus:

  1. is it possible to use both “value_template” and “attributes_template” within the same function? As it seems it’s either has to be “value” when using attributes_template", or “attributes” when using “value_template”.

  2. I don’t seem to b able to give a variable a specific value without using “value_template” within a function.

Kind regards


This is great. I’ve got a few scripts that pass file names back and forth, and this makes life much easier. I just have one script post the string value and the other scripts can read it via curl. Thanks much! Hopefully we’ll see something like this in the default build.


I’m trying to set a variable, then use it in a persistent notification: what do I do wrong?

    value: 13
      service: persistent_notification.create
        message: {{variable.test_variabile.state}}
        title: "Custom subject"

It give me error at {{variable.test_variabile.state}}