HI.
Got a sensor that shows time left in Days (if days) Hours and minutes:
{% if days %}
{{ alarm | timestamp_custom('%-d:%-H:%-M', False) }}
{% else %}
{{ alarm | timestamp_custom('%-H:%-M', False) }}
{% endif %}
Ive used the %- to have it only show the digits without leading 0. So I can have tts speak it naturally.
Trying to customize the unit_of_measurement counting the occurrence of the ‘:’ in the output to determine it to be D:H:M or H:M.
easy enough in Jinja using |count(’:’) .
Customizing using custom-ui uses javascript and
sensor.time_until_next_alarm:
templates:
unit_of_measurement: >
if (state ==='Not set, relax') return null;
if (state.count(':') ) return 'D:H:M';
return 'H:M';
doesnt work… can’t find the javascript function to do this correctly though, so please need your help.
using below for now, but that’s incorrect, because of the changing number of digits (I caused myself using the %- technique. Could of course take that out and use (state.length > 5), but that would be a last resort option really
sensor.time_until_next_alarm:
templates:
unit_of_measurement: >
if (state ==='Not set, relax') return null;
if (state.length > 5) return 'D:H:M';
return 'H:M';
—update—
found it:
unit_of_measurement: >
if (state ==='Not set, relax') return null;
if (state.split(':').length - 1 > 1) return 'D:H:M';
return 'H:M';