Good Morning,
I have created a picture elements in order fell free to format the element as i prefer but i need to convert the datetime shown in the pictuer below as “xx hour/day/month” ago.
Convert the template sensor to show it in the format you want, or make another template sensor.
Here’s an option if you only want it to show the next highest value time ago. (Borrowed macro from this thread) so it doesn’t say “1 days ago”
Might have to change the sensor.wfc_thermo. I just guessed on that one.
sensor:
- platform: template
sensors:
wcf_thermo_automation_last_triggered:
value_template: >
{% set last_triggered = states.sensor.wfc_thermo.attributes.last_triggered %}
{% set time_since = as_timestamp(now()) - as_timestamp(last_triggered) %}
{% macro phrase(value, name) %}
{%- set value = value | int %}
{%- set end = 's' if value > 1 else '' %}
{{- '{} {}{}'.format(value, name, end) if value | int > 0 else '' }}
{%- endmacro %}
{% if time_since < 3600 %}
{{ phrase(time_since | round(0), 'second') }} ago
{% elif time_since < 86400 %}
{{ phrase((time_since / 3600) | round(0), 'hour') }} ago
{% elif (time_since / 86400) < 7 %}
{{ phrase((time_since / 86400) | round(0), 'day') }} ago
{% elif (time_since / 86400) < 30 %}
{{ phrase((time_since / 86400 / 7) | round(0), 'week') }} ago
{% else %}
{{ phrase((time_since / 86400 / 7 / 4) | round(0), 'month') }} ago
{% endif %}
Keep in mind, it’s not too accurate, especially with the rounding. 1.6 weeks will be rounded to 2 weeks. And it also just uses a hardcoded 30 days per month rather than try to precise calculations…
You could always change the instances of round(0) to round(1) to have number such as 1.6 instead of 2…
The other way round would be to use the | timestamp_custom() filter
Filter timestamp_custom(format_string, local_time=True) converts an UNIX timestamp to its string representation based on a custom format, the use of a local timezone is default. Supports the standard Python time formatting options.
It’s really is hard to improve upon this, and I suppose it depends on how long you think you’ll be monitoring this condition before reset. (over a year ??? !!! )
30 days is the best average you are going to get for a month.
But given the variability, they may just want to stop at weeks as even my feeble interlect can work out that 52 weeks is around about a year. And that 32 weeks (etc.) is a multiple of 4 …
I suppose that some financial triggers might require a particular day, week, calendar month, breakdown - but I can’t think of why that would be relevant to HA. Though the things some people tie into HA has to be seen to be believed as they are really on the edge of reality.
Yeah ! I’m just waiting for a guy with a z wave network of x devices and a zigbee network of y devices wanting to know about local aircraft traffic integrations, as he’s susceptible to em radiation interacting with his central nervous system.
I also remember a guy who wanted to monitor his dog’s kennel occupancy with an ultrasonic sensor, till someone had to point out to him that ultrasonics drive dogs crazy !