Custom component to declare/set variables

Can you post your code?
Trying a quick test for me worked no issues

{% set mylow = 5 %}
{% set myhigh = 1200 %}
{{range(mylow ,myhigh +1)|random|timestamp_custom('%X',false)}}

Very useful component!

Thanks for creating and sharing.

Thanks for a great tool.

Question: Is it possible for a variableā€™s attributes to be save and restored after a restart of Home Assistant?

What Iā€™ve tried:

  • If I define attributes with values when I define the variable, these attributes are saved and restored.
  • if I later change these attributes the changes are not saved and are not restored.
  • If I add dynamic attributes to a variable these changes are not saved and are not restored.

The variableā€™s ā€œstateā€ is always saved and restored. I could stuff everything into a variableā€™s state, or I could create more variables, but it would nice to have a variableā€™s attributes saved too.

Iā€™ve just discovered this thread, and since many of you are using it to positive effect, I donā€™t think Iā€™m understanding it properly. How are ā€˜variablesā€™ different from, say, input_text types?

Variables are usually used on a temporary basis to help with a formula/calculation/template. Once the action /automation etc has been ran, this variable disappears.
input_text on the other side will remain, it all depends on what you want to do.

Actually, this is exactly what Iā€™m looking for. I want to be able to set the volume on a media_player temporarily in order to broadcast a TTS then set it back to what it was. I just posted a huge thing in another thread wondering if this was possible.

There was just talk of people wanting the system to restore them on startup and that confused me as to the difference between variables and input_*. I want my variables to be temporary and go away when the current script/automation is finished.

Do you have to declare and define these variables before you can use them? I was hoping for more of an ad-hoc type of variable. Donā€™t know if thatā€™s even possible.

it would look something like that, though Iā€™ve not tested it this way

{% set initial_volume =state_attr('media_player.google_home_mini','volume_level') %}

Then you can you it like that

    - service: media_player.volume_set
      data_template: 
        entity_id: media_player.google_home_mini
        volume_level: '{{initial_volume}}' 

How do you end up with that template getting executed, but not under a ā€œservice:ā€ heading? I donā€™t think the YAML syntax lends itself to what I want to do. I just canā€™t see how itā€™ll all fit together.

what is it that youā€™re trying to do?
Can you describe?

Record a media_playerā€™s volumelevel setting
Set volume on media player to something loud.
Play an announcement on TTS on media_player.
Set volume on media player back to what it was before.

Seems like overkill to have an input_number for this, because I donā€™t care about the value in it at any other time. I can do all those actions, just need to tuck away the ā€œprevious valueā€ temporarily.

Edit: I see examples like this, and I canā€™t see the difference between variable and input_whatever:

I think this is what i want to do. Iā€™m trying to set two variables using this component. Then, Iā€™m trying to cycle through a series of if statements to find the condition where the two variables are equal (I just put in the text ā€œlightā€ in the test code below). The portion of the code is:

set_ename:
alias: Set fname based on if statement
sequence:
service_template: >
{% if (variable.fname) == ā€œlightā€ %}
- service: variable.set_variable
data:
variable: ename
value: light.mbr_right
{% endif %}

This in the script.yaml where the name of the script is set_name. The two variables (fname and ename) are set up in the config.yaml, and are working.

I get an error that the service_template isnā€™t permitted in a script.

Can anyone help me understand how I can do this?

Specifically, ā€œthisā€ is to pass a name to the script, the script cycles through a list of if/then/else statements (which i tested in the template developer tool), and then when thereā€™s a match, the variable ename is set to some text. I canā€™t for the life of me figure out how to do this. Doesnā€™t seem like it should be that difficult now that I have a variable I can useā€¦

Update:
I modified the code to eliminate the if / endif lines (as well as a couple of other errant colons), the service_template line, and then ran the script. The variable ename does change to the value light.mbr_right, so at least I know that part of this worksā€¦ Thanks again for any guidance! The updated code is:

set_ename:
alias: Set entity name based on if statement
sequence:
- service: variable.set_variable
data:
variable: ename
value: light.mbr_kirby

After declaring a variable, can I globally use it like this?

- alias: 'Garage Lights off'
  trigger:
    - platform: state
      entity_id: binary_sensor.garage_motion
      to: 'off'
      for:
        minutes: variable.off_time
  action:

hi,

is this broken in hass.io version 0.84.1 ?

it was working fine prior to me upgrading.

Configuration invalid
CHECK CONFIG
Component not found: variable

thanks

1 Like

YES!
Same problem like other custom_components have.

check here also:

full error in log:

2018-12-13 11:36:59 ERROR (MainThread) [homeassistant.loader] Error loading custom_components.variable. Make sure all dependencies are installed
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/homeassistant/loader.py", line 92, in get_component
    module = importlib.import_module(path)
  File "/usr/local/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/config/custom_components/variable.py", line 20, in <module>
    from homeassistant.helpers.restore_state import async_get_last_state
ImportError: cannot import name 'async_get_last_state'
2018-12-13 11:36:59 ERROR (MainThread) [homeassistant.loader] Unable to find component variable
2018-12-13 11:36:59 ERROR (MainThread) [homeassistant.config] Package package_motion setup failed. Component variable does not exist (See /config/packages/package_motion.yaml:6).

@VDRainer would you be able to check if the solution in your other component could also be used in the component Variable? Maybe @rogro82 could you have a look whatā€™s wrong?

Looks like this has been fixed.
https://github.com/rogro82/hass-variables/pull/9

1 Like

ah yes, nice! really need this componentā€¦
thanks , and to @rogro82 for fixing!

Yes it should work again, I just merged the fix for 0.84 ( thanks to Tobias )

it does work, I can confirm, downloaded from your repo and replaced the old version.
Thank you very much.

btw this is on 0.84.1