Hello, i have multiple scrips/automations that modifies serval input_select helpers, all work correctly until an HA restart when all the dynamic input_select revert to the static one.
I have searched all topics and info and did not find any solution to this, if someone has any idea will be greatly appreciated.
The closest and best idea that I can see is using pyscript to create a service call to save and restore the input_select after restart, unfortunately I have no knowledge of python, but I can follow some simple coding to a point. Best example is this old post
What i understand is that is creating a service call to run a python script that saved the input_select helper options and state to a .ini file and after restart repopulate the helper.
What I want is to use it with multiple entities (input_select helpers) so it needs to generate the ini file based on the name of the entity.
Here is the code that I coped from the other post:
For save
# Write file from input_select.
import sys
# File I/O is done in a called module - ensure the path includes it. Note that this only needs to be added
# to one file ... pyscript picks it up when loading the files.
if "/config/pyscript_modules" not in sys.path:
sys.path.append("/config/pyscript_modules")
import file_ops
# This runs as a service, and is normally run from an automation.
@service
def save_input_select(entity=None, result=None):
log.debug(f"got entity {entity}, result {result}")
# Inputs are required. They are defined as optional so we can just log an error instead of encountering
# an exception.
if entity is None or result is None:
log.error("entity and result are both required, exiting")
# Domain must be input_select.
elif not entity.startswith("input_select."):
log.error("input entity must be domain input_select, exiting")
# All seems well.
else:
# List of all movies
movies = result['movies']
all_movies = []
for movie in movies:
all_movies.append(movie['label'])
all_movies.sort()
log.debug(f"movie list {all_movies}")
# Populate the input select.
# input_select.set_options(entity_id=entity, options=all_movies)
# Write the list to a file so we can restore the input select after a restart.
task.executor(file_ops.write_movie_file, all_movies)
For restoring the helper
# Restore the given input select from the previously saved list.
import file_ops
# This runs as a service, and is normally run from an automation.
@service
def restore_input_select(entity=None):
log.debug(f"got entity {entity}")
# Input entity is required. It is defined as optional so we can just log an error instead of encountering
# an exception.
if entity is None:
log.error("entity is required, exiting")
# Domain must be input_select.
elif not entity.startswith("input_select."):
log.error("input entity must be domain input_select, exiting")
# All seems well.
else:
# Read the backup file.
try:
all_movies = task.executor(file_ops.read_movie_file)
except FileNotFoundError:
log.error(f"saved movie file not found, exiting")
sys.exit(f"file not found")
except pickle.UnpicklingError:
log.error(f"saved movie file not usable, exiting")
sys.exit(f"file not usable")
log.debug(f"movie list {all_movies}")
# Populate the input select.
input_select.set_options(entity_id=entity, options=all_movies)
Anyone can help me with modifications to the code to fit my needs?