Here is an example of a countdown timer. When you flip the “Motion Timer” input, it will start counting down from 30 seconds. When you turn it off, it will stop.
I’d love to have feedback on ways to optimize this. And, I’m curious about the following:
-
How can I dynamically change the sensor scan_interval from 1 to some other value so that it’s not polled when the timer is disabled. Or, simply how to disable the sensor when it’s not needed.
-
How can I feed the timer value into an input_slider so that it can act as a progress bar.
-
How can I set a global variable with the timer value of 30, and use it in both shell_command.yaml and script.yaml so the value doesn’t have to be changed in multiple locations.
-
Are there memory-resident variables that can be utilized so that I don’t have to store values in temporary files?
Important: You must modify homeassistant/components/sensor/command_line.py and change this:
MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=60)
to this:
MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=0)
automation.yaml:
##################################################################
- alias: Home Assistant startup
###################################################################
trigger:
platform: event
event_type: homeassistant_start
action:
service: shell_command.stop_timer
##################################################################
- alias: Start timer if input_boolean is turned on
###################################################################
trigger:
platform: state
entity_id: input_boolean.motion_timer
from: 'off'
to: 'on'
action:
service: homeassistant.turn_on
entity_id: script.start_motion_timer
##################################################################
- alias: Stop timer if input_boolean is turned off
###################################################################
trigger:
platform: state
entity_id: input_boolean.motion_timer
from: 'on'
to: 'off'
action:
service: homeassistant.turn_on
entity_id: script.stop_motion_timer
shell_command.yaml:
start_timer: 'echo $(($(date +%s) + 30)) > /tmp/hass_timer'
stop_timer: '/bin/rm -f /tmp/hass_timer'
sensors.yaml:
- platform: command_line
scan_interval: 1
name: Motion Timer
command: 'if [ -e /tmp/hass_timer ]; then s=$(($(</tmp/hass_timer)-$(date +%s))); if [ $s -lt 0 ]; then s=0; fi; echo "$s s"; fi'
value_template: '{{ value }}'
script.yaml:
###################################################################
start_motion_timer:
###################################################################
alias: "Turn off motion timer after delay"
sequence:
- service: homeassistant.turn_on
entity_id: input_boolean.motion_timer
- service: shell_command.start_timer
- delay:
seconds: 30
- service: homeassistant.turn_off
entity_id: input_boolean.motion_timer
###################################################################
stop_motion_timer:
###################################################################
alias: "Turn off motion timer"
sequence:
- service: homeassistant.turn_off
data:
entity_id: script.start_motion_timer
- service: homeassistant.turn_off
entity_id: input_boolean.motion_timer
- service: shell_command.stop_timer
input_boolean.yaml:
##################################################################
motion_timer:
###################################################################
name: Motion Timer
initial: off
groups.yaml:
##################################################################
## Home tab
###################################################################
Home Switches:
view: no
entities:
- sensor.motion_timer
- input_boolean.motion_timer
default_view:
view: yes
entities:
- group.home_switches
customize.yaml:
sensor.motion_timer:
friendly_name: Motion Timer
icon: mdi:timer
input_boolean.motion_timer:
friendly_name: Motion Timer
icon: mdi:toggle-switch