Convert "00:02:01" to "2 minutes 1 second"?

- alias: Dead devices
  trigger:
  - platform: template
    value_template: "{{ not is_state('device_tracker.echo_dot', 'home') }}"
    for: "00:02:01"
  action:
  - service: notify.ha_telegram
    data_template:
      message: "{{ trigger.to_state.attributes.friendly_name }} is now {{ trigger.to_state.state }} since {{ trigger.for }} minutes"

Hi - how could I convert “00:02:01” (the {{ trigger.for }}) in my message to something like “2 minutes 1 second” ?
timestamp_custom seems to be only for timestamps, not time periods ?

Thanks

Any chance you can convert this to a Node red automation?
Node red can easily do that with the function node using a few lines of JavaScript.

1 Like
      message: >
        {{ trigger.to_state.name }} is now {{ trigger.to_state.state }} since {{ trigger.for.seconds // 60 }} minutes and {{ trigger.for.seconds % 60 }} seconds.

If you want the message to have proper grammar (‘1 minute’ not ‘1 minutes’) then you can do it like this:

      message: >
        {% set min = trigger.for.seconds // 60 %}
        {% set sec = trigger.for.seconds % 60 %}
        {{ trigger.to_state.name }} is now {{ trigger.to_state.state }} since {{ min }} minute{{'s' if min > 1 else ''}} and {{ sec }} second{{'s' if sec > 1 else ''}}.

That’s still imperfect because it can report ‘0 minute’ or ‘0 second’ (should be seconds) but it’s ‘good enough’ for most purposes.

1 Like

Thanks ! :star_struck:
I got this working in Developer Tools > Template

{% set time = 62  %}
{% set min = time // 60 %}
{% set sec = time % 60 %}

{% if min > 0 -%}{{ min }} minute{{'s' if min > 1 else ''}}{%- endif %}{% if (sec > 0) and (min > 0) -%} and {%- endif %}{% if sec > 0 -%}{{ sec }} second{{'s' if sec > 1 else ''}}{%- endif %}

It’s avoiding “0 minute” or “0 second”, also “and” is only showing when there’s minutes AND seconds

Thanks I should give Node-RED a try

This should be able to handle all permutations, including cases where minutes is 0 and seconds is > 0 and where seconds is 0 and minutes is > 0 (you don’t want it to display ‘and’ for these cases).

{% set time = 65  %}
{% set min = time // 60 %}
{% set sec = time % 60 %}
{% set m = min~' minute' if min > 0 else '' %}
{% set m = m~'s' if min > 1 else m %}
{% set s = sec~' second' if sec > 0 else '' %}
{% set s = s~'s' if sec > 1 else s %}
{% set x = ' and ' if min > 0 and sec > 0 else '' %}
{{ m }}{{x}}{{ s}}


Screenshot from 2020-08-24 10-41-23

1 Like

Thanks, but what’s the ~ sign doing in your code ?

That’s the ‘concatenate strings’ operator.

Some people use + to concatenate strings and it works. However, that’s the same operator used for addition. That means if the two ‘strings’ you want to concatenate are actually integers or floats, they will be numerically added as opposed to concatenated.

Screenshot from 2020-08-24 12-06-17

If I want to concatenate 7 and 5 I should use ~ because if I use + they will be added.

Thanks for the help :slight_smile: