Updated 2016/07/25
Added Support of Device Trackers.
-
Do not use the tracker function if you use presence detect that stores persistent data (OwnTracks, MQTT, etc.) or polling (Nmap, ASUSWRT, etc)
-
The tracker function works great with presence detection that relies on devices reporting in their location. (Locative, Hass iOS beta app, etc…).
What this will do:
This will allow you to save the state of Device_Trackers, Input_Booleans, Input_Selects, & Input_Sliders on change to a .ini file & load these setting, (states), using the API, back into Home-Assistant on start/restart automatically.
Info:
-
I created this because I am constantly tweaking/editing Home-Assistant and use a lot of Input_Components with my automatons. I found it frustrating to have to keep changing these components, or seeing my device_trackers set to “Away” after restarting Hass.
-
At first I just used the “initial:” option to set defaults, which works OK most of the time, but wanted an option to not have to edit the config every time I wanted different options than “initial:”
Disclaimer:
-
My python-fu is not the strongest.
-
I take no responsibility if this causes harm to your Hass install, your PC, or get’s your daughter pregnant.
-
The python script was written with python2 in mind & needs to be edited to work natively through python3.
-
That said, it should work with any system that has python2 installed, even when using python3 for Hass.
Highly Recommended you DO NOT try to modify this to work with anything other than Input_Boolean, Input_Select, Input_Slider, & Certain Device_Trackers.
Python Script:
hass_saved_settings.py - Updated 2016/07/25
###################################################
# AUTOMATION EXAMPLE - LOAD SETTINGS #
###################################################
##Load Input States from .ini - hass/saved_states
- alias: "Load Input States from Settings.ini"
trigger:
platform: event
event_type: homeassistant_start
action:
- delay:
minutes: 1
- service: shell_command.load_input_settings
####################################################
# AUTOMATION EXAMPLE - SAVE SETTINGS #
####################################################
## Save Input States to .ini - hass/saved_states
- alias: "Save Input States to Settings.ini"
trigger:
## Input Boolean
- platform: state
entity_id: input_boolean.your_input_boolean_01
- platform: state
entity_id: input_boolean.your_input_boolean_02
## Input Select
- platform: state
entity_id: input_select.your_input_select_01
- platform: state
entity_id: input_select.your_input_select_02
## Input Slider
- platform: state
entity_id: input_slider.your_input_slider_01
- platform: state
entity_id: input_slider.your_input_slider_02
## Device Trackers
- platform: state
entity_id: device_tracker.your_ios_device
- platform: state
entity_id: device_your_android_device
action:
service: shell_command.save_input_settings
data_template:
section: '{{ trigger.entity_id }}'
settings: '{{ trigger.entity_id }}'
value: "{{ trigger.to_state.state }}"
#####################################
# SHELL_COMMAND EXAMPLE #
#####################################
## Save Input Settings
save_input_settings: 'PYTHON_SCRIPT_LOCATION/hass_saved_settings.py -act update_settings -sec {{ section }} -set {{ settings }} -val "{{ value }}"'
# ex. ''/home/mike/.homeassistant/shell_scripts/hass_settings/hass_saved_settings.py -sec {{ section }} -set {{ settings }} -val "{{ value }}"'
load_input_settings: 'PYTHON_SCRIPT_LOCATION/hass_saved_settings.py -act update_hass'
# ex. '/home/mike/.homeassistant/shell_scripts/hass_settings/hass_saved_settings.py -act update_hass'
Notes:
-
The delay: time in the “LOAD SETTINGS” may need to be tweaked depending on your setup. It was needed for me to make sure everything was loaded, if run to soon then the settings were not applied. You can lower or raise the delay, or even remove it, I just set it to 1 Minute to be safe.
-
There are settings in the hass_saved_settings.py near the top that need to be configured.
path = "" # Path to ini file. ex. "/home/mike/.homeassistant/saved_input_settings.ini" host = "" # Host & Port of Home-Assistant. ex. "localhost:8123" api_key = "" # Frontend / API Info. ex. "MyHassPassword"
-
You do not need to create the .ini file, it will be created automatically once the Save automation is triggered for the first time. (move a slider, switch a boolean, etc…)
-
Once going, you only need to edit the SAVE SETTINGS automation to track new inputs.
-
This will only load Inputs that are not set as default ( Input_Boolean != off / Input_Slider != 0 / Input_Select != “None” and != “No One”)
-
You may need to adjust the defaults for Input_Select. I have mine set as “None” and “No One”
you can find that on line 137 of the hass_saved_settings.py.
if value != "None" and value != "No One":
-
You can rename the python script to anything you want, you’ll just need to edit the Shell_Commands.
-
Updating the python script, you may need to manually edit your .ini to add new sections or you can just delete the .ini & a new one with the correct sections will be created.
OK, I think that’s all of it… Really hope this is useful & actually works for some of you. lol
I’ll try to help & answer any questions that I can, just be gentle as I am so new to python & Home-Assistant