Countdown shown in Frontend

Tags: #<Tag:0x00007f3274b7f5b8> #<Tag:0x00007f3274b7f428>

Some days ago, the community helped me out in finding what I needed to do to automate a switch in order to turn on for a period of time. What I have is this:
image

The automation turns on the switch for the minutes stipulated in the panel (in this case 15 minutes).

Now I want to show the time remaining when I activate the automation. Is there any solution for this? Could someone help me out to try this?

Thanks in advance!

Add a template sensor with a value template like this:

sensor:
  - platform: template
    sensors:
      radiator_time_remaining:
        friendly_name: 'Time Remaining'
        unit_of_measurement: "min"
        value_template: >
          {% set update = states('sensor.time') %}
          {% if is_state('switch.radiator', 'on') %}
            {{ [ (states('input_number.minuts')|int - (as_timestamp(now()) - as_timestamp(states.switch.radiator.last_changed))/60)|round(0) ,0 ] | max }}
          {% else %}
            0
          {% endif %}

I have assumed the entity_ids of your switch and input number. Change them if required. Also you must have a time sensor defined.

1 Like

Wow! That was perfect!

As you said, I changed the entity_id and it worked defining the time sensor as specified in the documentation. Thanks very much!!

One more question, if I want that the display shows minutes and seconds (25:00) instead fo inly minutes (25), it’s ok if I change the unit_of_measurement?

Thanks again for sharing your knowledge!

You can change the unit to dog years if you want but it’s not going to change the fact that this template will only update every minute.

Ok, thanks!

I do only partially understand why the remaining attribute can not be updated in frontend while is updated in the timer popup every second.
Anyway, I work-around this by pausing/restarting the timer every second for a fraction of a second. Of course for long time timers, it delays it, for my purpose (watering) it is ok enough. I am not very proud of that solution but couldn’t find better one.

Hi Tom,
I’ve been trying to do your template above but i’m struggling and i’m sure i’m doing it wrong someway as i can’t see where i put the length of time i want (30mins)
So basically i want a small card showing a toggle switch (which is a boolean toggle) and then the time remaining when the toggle switch is turned on.
So i created your template as below with the time sensor under it and they are in the packages folder and both sensors show up so i add the remaining time sensor and the booleen toggle to a entity card but nothing happens when i toggle it.
Can you tell where i’m going wrong ?
Thanks

sensor:
  - platform: template
    sensors:
      boost_time_remaining:
        friendly_name: 'Time Remaining'
        unit_of_measurement: "min"
        value_template: >
          {% set update = states('sensor.time') %}
          {% if is_state('input_boolean.boost_toggle', 'on') %}
            {{ [ (states('input_number.minuts')|int - (as_timestamp(now()) - as_timestamp(states.input_boolean.boost_toggle.last_changed))/60)|round(0) ,0 ] | max }}
          {% else %}
            0
          {% endif %}
          
  - platform: time_date
    display_options:
      - 'time'

boost2

The template sensor was originally constructed to use an input number to set the delay time in minutes. It is referenced here in the template:

{{ [ (states('input_number.minuts')|int

So if you want a fixed time of 30 minutes you could replace that with a number like so:

sensor:
  - platform: template
    sensors:
      boost_time_remaining:
        friendly_name: 'Time Remaining'
        unit_of_measurement: "min"
        value_template: >
          {% set update = states('sensor.time') %}
          {% if is_state('input_boolean.boost_toggle', 'on') %}
            {{ [ 30 - (as_timestamp(now()) - as_timestamp(states.input_boolean.boost_toggle.last_changed))/60)|round(0) ,0 ] | max }}
          {% else %}
            0
          {% endif %}

If you want a variable time use the original template but add this input number to your configuration:

input_number:
  minutes:
    name: Minutes
    min: 1
    max: 60
    step: 1

Also with recent changes to the template engine this line is no longer needed:

          {% set update = states('sensor.time') %}

And can be deleted.

sensor:
  - platform: template
    sensors:
      boost_time_remaining:
        friendly_name: 'Time Remaining'
        unit_of_measurement: "min"
        value_template: >
          {% if is_state('input_boolean.boost_toggle', 'on') %}
            {{ [ 30 - (as_timestamp(now()) - as_timestamp(states.input_boolean.boost_toggle.last_changed))/60)|round(0) ,0 ] | max }}
          {% else %}
            0
          {% endif %}

Thanks for that.
Unfortunately it’s not accepting the code the template editor is saying.
TemplateSyntaxError: unexpected ')', expected ']'
Where would i put the input number you mentioned in the template? would it be directly under the boost_time_remaining.
Thanks again

Sorry I missed a bracket when deleting the input_number from the template, try:

sensor:
  - platform: template
    sensors:
      boost_time_remaining:
        friendly_name: 'Time Remaining'
        unit_of_measurement: "min"
        value_template: >
          {% if is_state('input_boolean.boost_toggle', 'on') %}
            {{ [ (30 - (as_timestamp(now()) - as_timestamp(states.input_boolean.boost_toggle.last_changed))/60)|round(0) ,0 ] | max }}
          {% else %}
            0
          {% endif %}

Under the input_number: section in your configuration.yaml file, or use the configuration / helpers section in the user interface.

Thanks so much for that it works great now.
One last thing if i may, can the timer work with a script executed or anything else for that matter if so is it just a case of swapping the “input_boolean.boost_toggle” i have and change it to a script instead.

It can be used with anything that has a last_changed property. Which I think scripts have.

Note however that this property is affected by home assistant restarts.

1 Like

great thanks.
would it still be “On” if i was to use a script for the last changed state.

Ah, no, it wouldn’t.

1 Like

Hi @tom_l ,
Sorry to bother you again i’ve been trying to do this myself for a while now but can’t do it.
So you created me the countdown timer above.
When i press the “boost” button below it triggers a sensor called “sensor.home_boost” that increases the heating for 30 mins.
Now what i want to do is create a automation that when i press boost triggering this “sensor.home_boost” i want it to turn on the boost input boolean i have as that will then activate you countdown timer you did for me and will then show in my simple thermostat card.
I hope you understand what i mean and could help.
This is the automation i thought would work but its not.

alias: Boost
description: Turn boolean toggle on
trigger:
  - platform: state
    entity_id: sensor.home_boost
condition:
  - condition: template
    value_template: |
      {{ trigger.to_state is not none and
         trigger.to_state.state == 'true' }}
action:
  - service: input_boolean.turn_on
    data: {}
    entity_id: input_boolean.boost_toggle
mode: single

boost4