Set Timer Duration to a variable

I am running Home Assistant version 0.110.5 on a RP4.

I am trying to set a timer based on a sensor crated from my alexa integration. I was able to sort out how to get it in the right format (hh:mm:ss) but it will not let me set the timer with the variable.

"{{(as_timestamp(states('sensor.office_echo_dot_next_timer')) - as_timestamp(now())) | timestamp_custom('%H:%M:%S', 0)}}"

evaluates to

“00:04:07”

But when I try to use it to set duration I get an error that it is not in the right format.  I set up a sensor to store the variable thinking maybe that was causing the problem but I still get the same error.
      time_left_alexa_timer2:
        value_template: >-
          {% if is_state("sensor.office_echo_dot_next_timer", "unavailable") %}
            None
          {% else %}
            {{ (as_timestamp(states('sensor.office_echo_dot_next_timer')) - as_timestamp(now()))  | timestamp_custom("%H:%M:%S", 0)  }}
          {% endif %}

When I use the below it still does not work.

  - data:
      duration: '{{ states("sensor.time_left_alexa_timer2") }}'
    entity_id: timer.alexatimer
    service: timer.start

Any thoughts?

data_template

What petro said and your Template Sensor can’t return none because that’s an invalid value for a timer’s duration.

Try this:

      time_left_alexa_timer2:
        value_template: >-
          {% set nt = states('sensor.office_echo_dot_next_timer') %}
          {% if not nt in ['unknown', 'unavailable'] %}
            {{ (as_timestamp(nt) - now().timestamp()) | timestamp_custom("%H:%M:%S", False)  }}
          {% else %}
            00:00:00
          {% endif %}

NOTE
The use of False in timestamp_custom means you want UTC time. If you want local time, change it to True or simply remove it because True is the default.

1 Like

That was my issue. Thank you for the help!

That was one of your issues. Read 123’s post as well.