An example of a countdown timer, and some related questions

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

This sounds like something you could do more cleanly in an App.

I am traveling at the moment but would be happy to put together an example when I get home.

Sorry to res an old thread. Do you think you could please put together a simple example? I am trying to track the amount the of time I have on a AppDaemon timer run_in(xxx, delay) (after a motion off event I wait XXX amount of time before I turn the light off) and display this in the front end?

Cheers
Zak

Hi there,

I’m struggling with calculation the time between to timestamps.
I have an alarm clock setup and want to calculate the time between the current time and my next alarm time, so I can cast this via TTS to my Chromecast and trigger this as I put my phone on my wireless charger next to my bed (with use of Tasker and a http post command)…

I can’t figure out how to calculate this.

Values that must be compared are:

Current time:

(now().strftime("%s") | int | timestamp_custom(" %H:%M"))

Wakeup time:

(states.sensor.alarmtime.state) | timestamp_custom(" %H:%M")

If someone is able to help me with this that would be awesome.

Check my post about loops and macros, there’s some time manipulations there that may be of use to your case.

Thanks, will look into this this weekend.

I did solve it by a workaround in the mean time but it’s not the prettiest solution.

https://community.home-assistant.io/t/calculating-time-left-untill-next-alarm/10328/11?u=bob_nl

Did you ever play with a timer in an App? Example?

Nothing so far …

Slightly related, I made a stopwatch that counts up instead of down before seeing this post:

I agree that this would probably be easier and more flexible using an app to save off time values and manipulate them though.

I can’t find the file: homeassistant/components/sensor/command_line.py