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:

variable:
  test_timer:
    value: 0
    attributes:
      icon: mdi:alarm

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

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

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

I hope someone will have some use for it.

Updates:

2017-08-24:
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.

52 Likes

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:

5 Likes

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

1 Like

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:

variables:
  my_test_variable:
    value: 1234
    initial: True/False

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

Thanks!

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.

5 Likes

Nice ill keep this in mind, should be useful

The counter component is partially covering the same ground.

https://github.com/home-assistant/home-assistant/pull/9146

1 Like

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:

2 Likes

This will be another one:

https://github.com/home-assistant/home-assistant/pull/9112

1 Like

^ 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).

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

Automations:
https://pastebin.com/tsJwjH8m

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

1 Like

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

2 Likes

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?

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

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