Sure. I’m going to be linking all of the documentation so you can hopefully learn the process. One of the hardest things (other than learning YAML and JINJA2) is knowing what to google to find the right documents.
For a slider, the easiest thing to do is to create an input_number.
input_XXX are easy ways for you to input variables INTO home assistant with direct support. 95% of the other things, there is no way to manually change the state.
Here are a list of the various input things you can easily create
input_number:
# This one is currently in minutes with a step size of 30 seconds.
# Read the documentation to see what kind of resolution you might
# want. The only thing that really matters here is how it feels on the
# user interface.
# If you did seconds, you'd have to start doing mental math when you
# want to leave lights on for multiple minutes.
# Don't set an initial value unless you want it to reset every reboot.
light_timer_minutes:
name: "Light Timer"
min: 1
max: 1440
step: 0.5
After a config check and reboot, you can now straight up add that to lovelace. It will give you a slider that you can set. It will start at 1.0, and will remain at whatever you end up setting it to.
Create one of those for each group of things you might want to control with different off times. I think all lights with auto shut off should be fine following the same slider.
In addition to being able to control it on lovelace, we can now use it as a normal sensor for our needs. Here’s an example automation that will auto shut off the lights in the trigger in this amount of time WHENEVER they turn on (from any method).
# Add this to your automation section
- alias: auto_light_timer
trigger:
# Each light you want to do this, add another trigger.
- platform: state
entity_id: light.bathroom_light
to: 'on'
- platform: state
entity_id: light.master_bath_light
to: 'on'
- platform: state
entity_id: switch.random_switch
to: 'on'
action:
# Wait template will sit here until this evaluates to 'True', OR until the timeout happens
# (if you specify a timeout).
# is_state returns True or False if the entity id matches the state you ask it to compare.
# So, wait here until the light is off
- wait_template: "{{ is_state(trigger.entity_id, 'off') }}"
# Convert our input number to seconds (because we allow 0.5 steps)
timeout:
# Truncate to the nearest whole second. The result calculation needs to
# be an integer.
seconds: "{{ (states(input_number.light_timer_minutes) | float * 60.0) | int }}"
# Check the current state. Because we waited, the light could have been shut off
# manually. There's no need to turn it off again (though it technically wouldn't hurt.
# If a condition in an action sequence is false, the entire action stops right here.
- condition: template
# No need to continue unless the light is still on
value_template: "{{ is_state(trigger.entity_id, 'on') }}"
# Use homeassistant.turn_off here so we can handle any trigger that can be turned on/off.
# We could use a service_template and grab the domain (light, switch, etc) and call
# that specific turn off function, but this one takes care of all of that for us!
- service: homeassistant.turn_off
# We specify data_template here if any of the fields are going to be a template.
# In this case, entity_id will be a template because we are handling multiple
# lights here in one automation.
data_template:
entity_id: "{{ trigger.entity_id }}"
Here are some links to the various things I used.
homeassistant.turn_on
wait_template
trigger.to_state
is_state()
And there you have it. Any light/switch/input_boolean/BLAH that you add to the triggers there will turn off automatically after X seconds defined by the input slider.
I don’t expect you to 100% understand those templates. But hopefully they make sense what they are doing with the comments.