Help with showing remaining time in UI

Hey all,
Home Assistant 0.103.5

Im playing around with motion and timers, so that lights come on and go off.
The slider seems to work well, however i want to show in teh UI, how long until the timer is “finished” - eg. you have 1 minute until times is out and the lights go off.

i cant work out how to use the timer, to populate the value from the slider when its triggered dynamically based on when the automation is triggered?

config: 

timer:
  timer_motion_kitchen:
    duration: '00:15:00'  ##<---???????? what should this be, as i want it to be dynamic, based on the slider value used in the automation

input_number:
  timer_kitchen_motion_slider:
    name: Timer Kitchen Motion Timer
    initial: 5
    min: 5
    max: 120
    step: 5

############### kitchen ###############
- id: kitchenlightsonmotion
  alias: kitchen lights on motion
  trigger:
  - entity_id: binary_sensor.motion_sensor_kitchen_motion
    from: 'off'
    platform: state
    to: 'on'
  condition: 
    condition: state
    entity_id: sun.sun
    state: below_horizon
  action:
  service: timer.start
    data:
      entity_id: timer_motion_kitchen
  service: light.turn_on
    data:
      entity_id:
        - light.kitchen_deconz

- id: kitchenlightsoffafter30minnomotion
  alias: kithcen lights off no motion slider
  trigger:
    platform: state
    entity_id: binary_sensor.motion_sensor_kitchen_motion
    to: 'off' 
    for:
      minutes: "{{ states('input_number.timer_kitchen_motion_slider')|int }}"
  action:
  - service: light.turn_off
    data:
      entity_id:
        - light.kitchen_deconz

Sorry, I thought I was following you until that point.

I’m not exactly sure what you are wanting to do and I can’t see it from the code you posted above either.

state.attributes of timer are
duration
remaining


    @property
    def state_attributes(self):
        """Return the state attributes."""
        return {
            ATTR_DURATION: str(self._duration),
            ATTR_REMAINING: str(self._remaining),
        }

So if you use a timestamp.now() + remaining
You can work out when it will finish as well

You best way to implement this is in the off automation set a delay and use the ‘00:{{inputslider}}:00’.
I don’t think you can change the timer itself.

So your automations turn the light on when motion is first detected, and off when there has been no motion detected for the number of minutes specified by the input_number. (Accept that there are typos in your first automation, so it won’t work as is.)

I think what you’re asking for is to be able to display the amount of time left before the second automation triggers and turns off the light. Is that it?

If so, I’m not sure there’s any way to do that, since there’s no knowing how long binary_sensor.motion_sensor_kitchen_motion will be in the 'on' state, even after it stops detecting motion. (Most motion detectors stay “on” for a minimum amount of time, even without further motion.)

1 Like

thanks! this is what i wanted to do
ideally, having the light flash quickly when there was only 1 min left before going off (eg. wave your arms around, the lights will go off and you have been sat around on the sofa too long!)

If that’s what you’re trying to do, then you don’t really need to show the amount of time left in the UI. You could just do something like the following (which doesn’t need a timer):

############### kitchen ###############
- id: kitchenlightsonmotion
  alias: kitchen lights on motion
  trigger:
  - entity_id: binary_sensor.motion_sensor_kitchen_motion
    platform: state
    to: 'on'
  condition: 
  - condition: state
    entity_id: sun.sun
    state: below_horizon
  action:
  - service: light.turn_on
    entity_id: light.kitchen_deconz

- id: kitchenlightsoffafter30minnomotion
  alias: kitchen lights off no motion slider
  trigger:
  - platform: state
    entity_id: binary_sensor.motion_sensor_kitchen_motion
    to: 'off' 
    for:
      minutes: "{{ states('input_number.timer_kitchen_motion_slider')|int }}"
  action:
  - service: light.turn_off
    entity_id: light.kitchen_deconz

- alias: kitchen lights off warning
  trigger:
  - platform: state
    entity_id: binary_sensor.motion_sensor_kitchen_motion
    to: 'off' 
    for:
      minutes: "{{ states('input_number.timer_kitchen_motion_slider')|int - 1 }}"
  action:
  - service: light.turn_off
    entity_id: light.kitchen_deconz
  - delay:
      seconds: 2
  - service: light.turn_on
    entity_id: light.kitchen_deconz

The last automation will trigger one minute earlier than the automation that turns the light off. It will “flash” the light off & back on as a warning. If motion is then detected in the next minute then the automation that turns the light off will not fire (and its “timer” will restart.)

If you light supports flashing then the action in the last automation could be changed to this:

  action:
  - service: light.turn_on
    entity_id: light.kitchen_deconz
    data:
      flash: short

Not sure about this because I don’t have a light that supports the flash parameter. :slightly_smiling_face: