Slider to set minutes in a timer (not a delay)

hi to all , i’m new in the HA word.
I’m trying to set the value of a timer using a slider

i’ve got a look to this topic, but it’s different…

here the code i’ve tryed without success…

input_number:
  slider:
    name: stairs_light
    icon: mdi:clock-start
    initial: 10
    min: 0
    max: 15
    step: 1

timer:
  stairs_light_timer:
    duration: '00:{{ states.input_number.stairs_light.state | int }}:00'  

and this is the error that refound the control of configuration

Invalid config for [timer]: offset 00:{{ states.input_number.stairs_light.state | int }} should be format 'HH:MM' or 'HH:MM:SS' for dictionary value @ data['timer']['stairs_light_timer']['duration']. Got '00:{{ states.input_number.stairs_light.state | int }}'

Can anybody help me?
thanks

I suspect data typing, in that HH:MM:SS is a string, and states.input_number.stairs_light.state|int is an integer.

Yeah i’m not sure that’s going to work indeed. And from the docs it doesn’t look like you can set a timer’s value programmatically…
The other way round would be to create an automation that runs every sec (potentially a resource killer) which has a condition to check whether the time since you initiated a virtual timer (input_boolean) is more than the tdureation set by your slider.
Maybe even have an automation that activates that previous automation when you turn the input_boolean on and one that turns the automation off when the input_boolean is turned off?

As mentioned above, you can’t use a template to set the initial timer duration. However, you can use a template to set the timer’s duration when you start it using the timer.start service. E.g.:

  - service: timer.start
    entity_id: timer.stairs_light_timer
    data_template:
      duration: >
        {{ states('input_number.slider')|int|multiply(60)
             |timestamp_custom('%X',false) }}

Also note that the entity_id of your “slider” is input_number.slider, not input_number.stairs_light. The value of the name: parameter is the entity’s friendly_name, not its object_id (where the object_id is the part of the entity_id after the dot.)

3 Likes

What happens if you start a timer based on that code then while the timer is running you then change the time input_number value?

I assume it captures the timer duration value at the instant the timer was started and keeps it until the timer times out based on the original value? Then the next time the timer start is called it then loads new input_number duration value?

it would yes. If you wanted to adjust the timer while it’s running you’d need to cancel/pause the timer, then restart it with the new value, so probably another automation.
You could even make it one step “more complicated” by restarting the timer with the actual time left to run based on new timer value and what has already elapsed. Let us know if you want assistance with this.

No assistance needed (yet…:wink:). Thanks tho.

I was just wondering because I have a random number generator and I was going to use it to set a timer for presence simulation.

Based on this thread it inspired me to try to work out the template to convert the number generated to a string for use in the timer value.

The problem was that the random number generator generates integers and if the integer is less than 10 it is only a single digit. But the time requires the format ‘00:00:00’. So i had to work out a way to stick the extra digit to the beginning if it was less than 10. Here is what I came up with:

value_template: >
  {% if states.sensor.random_hour.state|int < 10  %}
    0{{states.sensor.random_hour.state}}
  {% else %}
    {{states.sensor.random_hour.state}}
  {% endif %}

That way you end up with a value in the format 00.

I can then do that for minutes and seconds, too, as necessary.

Then (hopefully…) I can use those results in a template to set a random duration in a timer script as pnbruckner described above.

There may be a cleaner way of doing it but it was fun figuring out how to do it.

Hopefully it works when I’m done! :smile:

for a potential “cleaner” way to do it, you could generate a random number of seconds then convert that to a time format? Not sure it’s cleaner, but may be simpler?
This will convert 120 (seconds) into 02:00 as a string
{{120| timestamp_custom('%M:%S')}}

Replace 120 with your random entity

@pnbruckner I know you’re really good with such templates. Do you know why when adding hours to the above I get 1h 2min instead of 0h 2min? (in case @finity wants hours too)

Strange but when I copy your code and put in the hour I get ‘19:02:00’

Think I know why, it must be using UTC by default. can you try this and let me know what you see?
{{120| timestamp_custom('%H:%M:%S',0)}}

It’s now 00:02:00

1 Like

FYI, you don’t need a separate random number generator. This is what I use in all my simulations that need random time:

{{range(MIN_SEC,MAX_SEC+1)|random|timestamp_custom('%X',false)}}

Replace MIN_SEC and MAX_SEC with the minimum and maximum time you want in seconds. E.g., if you want from 5 sec to 10 min, use range(5,10*60+1).... The reason for the +1 is because the highest number range outputs is one less than the second argument.

Also, %X is a shortcut for `%H:%M:%S’.

1 Like

Phil,

Can you share where you get all that info from regarding formatting?
I think I’ve seen one of your posts with a link but can’t retrieve it.

Thanks

Hi Laurent,

Which formatting? Do you mean multiline YAML? If so: https://yaml-multiline.info/

Or do you mean the markdown cheatsheet about how to format code? If so: https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet#code

the yaml link is a good one,but was more looking at where you find out how to convert formats, like where did you find the tip for %X is a shortcut for `%H:%M:%S’.
Thanks

Oh. Well, I have to admit I saw someone else use %X. :slight_smile: But you’ll find it if you go to the Templates page in the frontend and click on Home Assistant template extensions. If you scroll down to where it defines the timestamp_custom filter, there’s a link to Python format options. Towards the end of the list is %X.

I should probably mention at this point, it says %X is “Locale’s appropriate time representation.” I suppose there may be locales where %X isn’t the same as %H:%M:%S, so your mileage may vary. :wink:

1 Like

if its of added value, here’s a nice reference for Python Time:
http://strftime.org