Hello,
I also tried to get it to work. My solution was to NOT put it into a seperate Subfolder.
config\custom_components\variable.py
After that, the configured variables are visible.
I’m playing with Hass.io 0.68.1
Greetings
Sven
Hello,
I also tried to get it to work. My solution was to NOT put it into a seperate Subfolder.
config\custom_components\variable.py
After that, the configured variables are visible.
I’m playing with Hass.io 0.68.1
Greetings
Sven
It’s working me for in a NOT separate folder now too. I’m sure i tried that before but maybe hassio didn’t restart when i thought it had.
Hello ,Thank you.
Can these defined variables be used and written to from a bash shell script being executed using the shell component. Specifically I have 3 variables being created by the shell script and I want to make them available to HA.
Thanks, Ynot
I’m using a little python script with the API to populate variables at runtime.
After the variables.yaml has changed, watcher triggers the script, and the variables in HA are updated, if ‘restore: true’ is not set.
Maybe you can use it.
import yaml
import requests
config_file = '/home/homeassi/.homeassistant/variables.yaml'
url = 'http://localhost:8123/api/states/variable.'
headers = {'x-ha-access': 'your_password', 'content-type': 'application/json'}
def read_variables_yaml():
with open(config_file, 'r') as stream:
try:
config = yaml.load(stream)
except yaml.YAMLError as exc:
print(exc)
return config
json = read_variables_yaml()
errors = ''
for v in json:
variable = v
state = json[v]['value']
restore = json[v]['restore']
if not restore:
if 'attributes' in json[v]:
attributes = str(json[v]['attributes'])
data = '{"state": "' + str(state) + '", "attributes": ' + attributes + '}'
else:
data = '{"state": "' + str(state) + '"}'
data = data.replace("'",'"')
#print(variable, data)
r = requests.post(url+variable, data=data, headers=headers)
if r.status_code != 200 and r.status_code != 201:
errors = errors + 'ERROR:' + variable + ' - ' + str(r.status_code)
if errors != '':
print(errors)
The watcher job:
job1:
label: Watch variables
watch: /home/homeassi/.homeassistant/variables.yaml
exclude: []
events: ['modify']
options: []
recursive: false
command: python3 /home/homeassi/scripts/variables_set.py
Thanks a lot for this nice component. I played a lot with it around and used it in different ways.
Now, I want to use it to store three values coming in via MQTT, but this seems not working!
Here is what I tried: define a variable in configuration.yaml
variable:
lightlevelvalue:
value: 0
Then in automations.yaml
- alias: newlight
trigger:
- platform: mqtt
topic: WeatherStation/light
action:
- service: variable.set_variable
data:
variable: lightlevelvalue
value_template: '{{ (variable.state | int) + 1 }}'
This is working pretty well, but as soon as I want to update the variable with a value from my light sensor it fails. What happens is, that the variable just lists the string “{{ states.sensor.weatherstation_light_level.state | int }}’”. Code looks like this:
- alias: newlight
trigger:
- platform: mqtt
topic: WeatherStation/light
action:
- service: variable.set_variable
data:
variable: lightlevelvalue
value_template: '{{ states.sensor.weatherstation_light_level.state | int }}'
I tried several variants of the value statement without success e.g.:
Is it possible, that you can’t reach the sensor values from this point?
This here is the sensor:
Any comment or hint is welcome.
Cheers
Have you tried it with data_template
?
action:
- service: variable.set_variable
data_template:
variable: lightlevelvalue
value_template: '{{ states.sensor.weatherstation_light_level.state | int }}'
Thank you sooooo much, VDRainer!!! This was the trick. Works perfectly now.
Thanks for the variable component . Is it possible to use a variable in the delay option? So lets say I have a numeric value of 2000 that I want to use in an automation to delay it for 2000 miliseconds. And if I change the variable to 3000 the automation will have a delay of 3000 miliseconds.
Thanks!
Set your variable to be a delay in seconds
I use a random delay for my holiday lights
- delay: "{{range(0,1200+1)|random|timestamp_custom('%X',false)}}"
It can be easily adjusted to be a variable instead:
- delay: "{{variable.test_timer|timestamp_custom('%X',false)}}"
Thanks for the quick reply, when I try your delay with my own variable it says: delay template: UndefinedError: ‘variable’ is undefined. Do you know why?
EDIT: got him, when I use: - delay: "{{states.variable.test_var.state|timestamp_custom('%X',false)}}"
it is working fine!
EDIT2: the random on in delay is nice…but when I use 2 variables to replace the 0 and 1200 in range(0,1200) with 2 variables it is not working. Do you have an idea on that one?
Did you install/define the variable package as per original post?
@lolouk44 the component itself was working fine, after some trial and error I got it working! Thanks for your help, appreciate it
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:
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.