Timer duration from input number in a button

I added a button like this:

type: button
tap_action:
  action: call-service
  service: timer.start
  service_data:
    entity_id: timer.roaster_second_crack
    duration: >-
      {{
      states('input_number.roaster_time_second_crack')|int|timestamp_custom('00:%M:%S')
      }}
hold_action:
  action: call-service
  service: timer.finish
  service_data:
    entity_id: timer.roaster_second_crack
show_icon: true
show_name: true
name: Countdown

where input_number.roaster_time_second_crack is:

input_number:
  roaster_time_second_crack:
    name: "Roasting sec. crack timer"
    min: 120
    max: 240
    step: 1
    mode: slider

This does not work as Failed to call service timer/start. offset {{ states('input_number.roaster_time_second_crack')|int|timestamp_custom('00:%M:%S') }} should be format 'HH:MM' or 'HH:MM:SS' for dictionary value @ data['duration'].

I checked in the template debugger that {{ states('input_number.roaster_time_second_crack')|int|timestamp_custom('00:%M:%S') }} produces 00:02:20, and in fact I use this approach for automation’s “for” trigger, it is only this button which does not like templates, why is that? I could probably avoid timers, trying to get my head around it… Thanks,

Have you tried it without formatting it? Because I don’t see the point of doing that for this use case. So like this:

duration: >- 
{{ states('input_number.roaster_time_second_crack')|int }}

Just tried, the same error. And how can it work at all as states('input_number.roaster_time_second_crack')|int produces 120 which is not “HH:MM” nor “HH:MM:SS”?

It should work because duration take a timestamped input or seconds as value. But there is something else going on here.
Are you always formatting your template data as follows?

{{
Something
}}

Can you try it like this:

{{ Something }}

I entered it manually in the YAML editor as a single line, saved, then opened again in the YAML editor - and it appeared then multiline. So I’d say it does not matter. I am pretty sure that piece just does not handle templates as this control supports very different actions and probably some special syntax is needed.

The docs don’t state that tap_action is templateable. If it were, you’d need to use service_template (ref), and there’s no mention of service_template in the frontend source.

Perhaps make the button run a script, and have the template in the script?

2 Likes

Never wrote a script, a new can of worms. Opened https://www.home-assistant.io/integrations/script , wrote my script, got error on mode: single, juuuust lovely :slight_smile: It is single by default so no big deal but the doc is out of sync.

It may be your installation that’s out of sync. mode: single is new with 0.113, which only came out yesterday.

true, I did not update mine for a month or two. The update though broke/fixed xiaomi gateway, had to re-add it.