Yes, there is no way to format a timedelta that’s naturally built into python or jinja.
Just so you know, most languages don’t have this. I was surprised when I found out that C# does have it, but the implementation is terrible in comparison to the strftime equivalent.
Wow! That’s seems like something that should’ve been handled already. OK, thank you.
I use this on my “Router” uptime sensor , sure i didn’t bother about showing D/H/M, as i have a “personal perspective” of the word “UserFriendly” … it’s my system, only i need to understand
- type: custom:template-entity-row
name: Uptime
active: true
state: >
{% set s = states('sensor.rt_ax68u_d680_uptime')| int(3) %}{{
'%02d:%02d:%02d' % ((s/86400)|int(3),((s%86400) /
3600)|int(3),(s%3600) / 60) }}
Thanks. It doesn’t work for me. I get 00:00:00 using the whole thing. What type and format does your sensor output and what does the output of that template look like?
{% set s = states('sensor.uptime')| int(3) %}
{{'%02d:%02d:%02d' % ((s/86400)|int(3),((s%86400) / 3600)|int(3),(s%3600) / 60) }}
{{ s }}
{{ states('sensor.uptime') }}
gives me
if htat’s what you want, just use what I linked, no point in re inventing the wheel
{%- set up_time = as_timestamp(now())-as_timestamp(states('sensor.uptime')) %}
{%- macro phrase(name, divisor, mod=None) %}
{%- set value = ((up_time // divisor) % (mod if mod else divisor)) | int %}
{{- '{} {}'.format(value, name) if value | int > 0 else '' }}
{%- endmacro %}
{%- set values = [
phrase('w', 60*60*24*7),
phrase('d', 60*60*24, 7),
phrase('h', 60*60, 24),
phrase('m', 60),
] | select('!=','') | list %}
{{ values | join(', ') }}
Thanks, and I probably will. I just need another day to process why this takes 12 LOC.
I made the template easily expandable for people who don’t understand code. They can copy the template and remove lines that they don’t want. This could be much shorter and custom, but why bother when this works for all needs/wants
After comparing/contrasting the three example ways to represent the same time at the beginning of this comment, why does:
{{ now() }}
give me this?
2022-05-20 12:37:00.029958-04:00
Which is my local time EDT (UTC-4). So, it’s providing me with a local time and the UTC offset which is not a combination that comment suggests is correct (either the time should be 16:37 or the offset should be dropped).
Because I made a mistake in my explanation?
But I think you are right. I should have made that example read:
time (my time zone in UTC representation): 2018-12-14T14:57:27-05:00
Thanks for pointing it out.
Sensor reports “seconds”
Output in card ( D:H:M )
Edit: Without template
Edit: Actually this seems to have change at some point, last year it was only an INT, no commas, and no trailing s
That explains why it wasn’t working for me. Mine is a DTG.
OK, thanks. I wasn’t sure which format was correct or if HA was presenting it wrong.
{% if is_state("sensor.filey_tide", "Falling") %}Low{% else %}High{%
endif %} is {{ min(state_attr('sensor.filey_tide',
'next_high_tide_at'), state_attr('sensor.filey_tide',
'next_low_tide_at')) }}.
Currently, my tidal sensor outputs the full date and time - using the code above how would I get JUST the time?
CURRENT
Low is 2022-08-31 13:56:32
WHAT I NEED
Low is 13:56:32
We need more info about next_high_tide_at and what kind of object it is. Put this in the template editor please
{{ state_attr('sensor.filey_tide', 'next_high_tide_at') * 2 }}
and post the results here.
TypeError: unsupported operand type(s) for *: ‘datetime.datetime’ and ‘int’
Thanks, that means they are datetime objects, so…
{% set info = 'Low' if is_state("sensor.filey_tide", "Falling") else 'High' %}
{% set value = min(state_attr('sensor.filey_tide','next_high_tide_at'), state_attr('sensor.filey_tide','next_low_tide_at')) %}
{{ info }} is {{ value.time() }}.
I am sure this has been answered
If I have device_class: timestamp
sensor (it behaves differently to a non class sensor)
How do I add a custom timestamp that can be adjusted prior to hitting HA and won’t break the original timestamp format when sending it through mqtt discovery.
It doesn’t currently have an attribute
for the sake of readability we shall name Timestamp(is the true name) to Bobtime.
{{ value_json.Bobtime }}
works but no customization
{ "Bobtime": "2022-09-04T21:05:30+1000", "next": "value", }
example mqtt message
I have tried a things and I am also not set as sending it as a device_class
{{ value_json.Bobtime | as_timestamp | timestamp_custom('%H:%M:%S %d-%m-%Y) }}
``` async spits that back with an error. Which I guess is understandable as it breaks the desired TS format
This works when not device_class: timestamp
{{ value.json_Bobtime | as_datetime | timestamp_custom('%H:%M:%S %d-%m-%Y) }}
Also not liked by discovery
So can I do something like
{{ value_json.Bobtime | (strptime(state_attr('Bobtime.additional, 'mini_bob') | timestamp_custom('custom timestamp variable') }}
or
Do I need to add a json_attr template, and json_attr_topic and can I steal the Bobtime to make the attribute without actually having an attribute message…
I would like the `timestamp_custom( ) to be able to be changed by “$variable” before hitting discovery
Thanks so much…totally smashed it
{{ value_json.Bobtime | as_datetime }}