Custom component to declare/set variables

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

It seems that after upgrading HA to 0.84.2 (and applying the latest hass-variables fix), variables with "restore": true don’t preserve their state after HA restart anymore. I have opened a new bug: https://github.com/rogro82/hass-variables/issues/11.

Hi i just upgraded to 0.84.2 and updated the variable.py. Still getting errors.

2018-12-16 11:58:36 ERROR (MainThread) [homeassistant.loader] Error loading custom_components.variable. Make sure all dependencies are installed

Traceback (most recent call last):

File "/usr/src/app/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 "&lt;frozen importlib._bootstrap&gt;", line 994, in _gcd_import

File "&lt;frozen importlib._bootstrap&gt;", line 971, in _find_and_load

File "&lt;frozen importlib._bootstrap&gt;", line 955, in _find_and_load_unlocked

File "&lt;frozen importlib._bootstrap&gt;", line 665, in _load_unlocked

File "&lt;frozen importlib._bootstrap_external&gt;", line 678, in exec_module

File "&lt;frozen importlib._bootstrap&gt;", line 219, in _call_with_frames_removed

File "/config/custom_components/variable.py", line 14, in &lt;module&gt;

from homeassistant.helpers.restore_state import async_get_last_state

ImportError: cannot import name 'async_get_last_state'

2018-12-16 11:58:36 ERROR (MainThread) [homeassistant.loader] Unable to find component variable

Any help please…

I have the same problem. Hope having a solution soon

Have a look here

1 Like

Are you sure you updated to the latest variable.py correctly because your log shows an import from the previous version:

File "/config/custom_components/variable.py", line 14, in &lt;module&gt;
from homeassistant.helpers.restore_state import async_get_last_state
ImportError: cannot import name 'async_get_last_state'

Line 14 should be:

Thank you

Hi there, when I tried to install the custom component HA complains on startup:

2018-12-20 15:43:36 ERROR (MainThread) [homeassistant.loader] Error loading custom_components.variable. Make sure all dependencies are installed
Traceback (most recent call last):
File “/home/homeassistant/.pyenv/versions/3.6.3/envs/homeassistant-3.6.3/lib/python3.6/site-packages/homeassistant/loader.py”, line 92, in get_component
module = importlib.import_module(path)
File “/home/homeassistant/.pyenv/versions/3.6.3/lib/python3.6/importlib/init.py”, line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File “”, line 994, in _gcd_import
File “”, line 971, in _find_and_load
File “”, line 955, in _find_and_load_unlocked
File “”, line 665, in _load_unlocked
File “”, line 678, in exec_module
File “”, line 219, in _call_with_frames_removed
File “/home/homeassistant/.homeassistant/custom_components/variable.py”, line 14, in
from homeassistant.helpers.restore_state import RestoreEntity
ImportError: cannot import name ‘RestoreEntity’

Do I need to install the dependency?

You need to run 0.84.x for the latest version of this component.

Ah thanks… that solved my problem! :wink: