petro31 / easy-time-jinja Goto Github PK
View Code? Open in Web Editor NEWEasy Time calculations for Home Assistant templates
License: MIT License
Easy Time calculations for Home Assistant templates
License: MIT License
hello
with easy_relative_time the french result isn't correct
ex: "5 days ago" is translate to "5 jours il y a" but the right translation is "Il y a 5 jours"
is there any way to solve this ?
I'd love to use a version of "easy_relative_time" ("easy_rounded_relative_time"?) where, if it's at e.g. 2 hours 59 minutes, it rounds to 3 hours and outputs "~3 hours".
as followup from https://discord.com/channels/330944238910963714/672223497736421388/1129046663105892403
using the main source timestamp template for various other template sensors:
- unique_id: next_alarm_timestamp
state: >
{% macro getalarm(offsetday=0) %}
{% set day = (now().weekday() + offsetday) % 7 %}
{% set offset = offsetday * 86400 %}
{% if day in range(5) %}
{% set hour = state_attr('input_datetime.alarmclock_wd','hour') %}
{% set minute = state_attr('input_datetime.alarmclock_wd','minute') %}
{% else %}
{% set hour = state_attr('input_datetime.alarmclock_we','hour') %}
{% set minute = state_attr('input_datetime.alarmclock_we','minute') %}
{% endif %}
{% set alarm = hour|float|multiply(3600) + minute|float|multiply(60) %}
{% set time = now().hour * 3600 + now().minute * 60 %}
{{(offset - time) + alarm if offsetday else alarm - time}}
{% endmacro %}
{% set weekday = is_state('input_boolean.alarmclock_wd_enabled','on') %}
{% set weekend = is_state('input_boolean.alarmclock_we_enabled','on') %}
{% set weekdays = [0,1,2,3,4] %}
{% set weekends = [5,6] %}
{% set day = now().weekday() %}
{% set nextday = day + 1 if day != 6 else 0 %}
{% if nextday in weekdays %}
{% if weekday %}
{% set offsetday = nextday %}
{% elif weekend %}
{% set offsetday = weekends[0] %}
{% else %}
{% set offsetday = None %}
{% endif %}
{% elif nextday in weekends %}
{% if weekend %}
{% set offsetday = nextday %}
{% elif weekday %}
{% set offsetday = weekdays[0] %}
{% else %}
{% set offsetday = None %}
{% endif %}
{% else %}
{% set offsetday = None %}
{% endif %}
{% if offsetday != None %}
{% set offset = offsetday-day if offsetday > day else offsetday - day + 7 %}
{% if (day in weekdays and weekday) or (day in weekends and weekend) %}
{% set today_alarm = getalarm()|float %}
{% else %}
{% set today_alarm = -1 %}
{% endif %}
{% set next_alarm = getalarm(offset)|float %}
{% set time = now().replace(second=0).replace(microsecond=0) %}
{% if today_alarm > 0 %}
{{as_timestamp(time) + today_alarm}}
{% else %}
{{as_timestamp(time) + next_alarm}}
{% endif %}
{% else %}
0
{% endif %}
and eg datetime sensor:
- unique_id: next_alarm_datetime
state: >
{% set timestamp = states('sensor.next_alarm_timestamp')|int(default=0) %}
{{as_datetime(timestamp)|as_local}}
device_class: timestamp
availability: >
{% set timestamp = states('sensor.next_alarm_timestamp')|int(default=0) %}
{{timestamp != 0}}
several the easy_time sensors produce unexpected results:
{% from 'easy_time.jinja' import speak_the_days %}
{{speak_the_days('sensor.next_alarm_datetime')}}
returns 'in 1 day', and you've posted it should be 'tomorrow'
{{states('sensor.next_alarm_timestamp')}}
returns 1689309000.0
{{states('sensor.next_alarm_datetime')}}
returns 2023-07-14 06:30:00+02:00
I also noted that because the main source sensors sets 0 when no alarm is set, the sensors using easy_time throw an error in that case.
Hope this suffices for you to check functionality, if not please let me know
followup from Discord
have a timestamp entity like
{{states('sensor.afvalwijzer_plastic')}}
which outputs ' 2024-02-02'
for various dashboards/templates, it would be very nice to be able to do
{% set trash = 'sensor.afvalwijzer_plastic' %}
{% from 'easy_time.jinja' import month %}
{{month(trash)}}
or
month(states(trash))
to output the month name
which would be much nicer than
{{month(as_datetime(states(trash)).month)}}
which currently does the job alright
please consider adding that?
thx
"In 2 hours and 8 minutes" reads "I 2 timmar och 8 minuter"
"I" should be "Om"
First, thank you for your work on this, useful!
In the Developer Tools (HA 2023.5.4), trying this function in the example, returns the error below:
last_day_in_month(month, weekday)
https://github.com/Petro31/easy-time-jinja#last_day_in_monthmonth-weekday
{% from 'easy_time.jinja' import last_day_in_month %}
{# Last Sunday in August #}
{{ last_day_in_month(8, 6) }}
{{ last_day_in_month(8, 6) | as_datetime }}
returns:
UndefinedError: 'day' is undefined
Returns a rather odd answer:
{% from 'easy_time.jinja' import next_dst_phrase %}
{# Next daylight savings time #}
{{ next_dst_phrase() }}
{{ next_dst_phrase() | as_datetime }}
Result type: string
gain 53 years
None
I have to do multiple imports to get below to run:
{% from 'easy_time.jinja' import days_in_month %}
{% from 'easy_time.jinja' import days_next_month %}
{% from 'easy_time.jinja' import days_last_month %}
{# Number of days this month #}
{{ days_in_month() | int }}
{# Number of days in december #}
{{ days_in_month(12) | int }}
{# Number of days in February #}
{# works on leap year #}
{{ days_in_month(2) | int }}
{# Number of days next month #}
{{ days_next_month() | int }}
{# Optionally add an offset for further in the future #}
{{ days_next_month(2) | int }}
{# Number of days last month #}
{{ days_last_month() | int }}
The weekday macro seems to be broken and not working according to doc.
The guide says it also accepts datetime optionally, but the macro itself does not have code to support datetime.
Furthermore the index for weekdays is calculated wrong since 1 is substracted. the datetime.weekday() function usually returns 0-6 (Mon-Sun), but the weekday macro expects 1-7
I fixed this locally with following idx line:
{%- macro weekday(weekday=None, language=default_language) %}
{%- set idx = weekday % 7 if weekday is integer and weekday >= 0 else weekday.weekday() if weekday is datetime else now().weekday() %}
{{- languages.get(language, {}).get('days', [_bad_value]*7)[idx] }}
{%- endmacro %}
Please check what would be the best official fix for weekday in order to support datetime arguments and keep documentation and weekday index in line with the weekday() datetime function
following our Discord chat, please add support for those datetimes?
https://discord.com/channels/330944238910963714/672223497736421388/1175066825042694334
this is what we talked about:
{% from 'easy_time.jinja' import month, weekday %}
{% set date = strptime(states('sensor.afvalwijzer_next_date'),'%d-%m-%Y')|as_local %}
{{ weekday(date.isoweekday()) }} {{ date.day }} {{date.month}} {{ month(date) }} {{month(date.month)}} {{ date.year }}
also please see your suggestion to self ;-)
https://discord.com/channels/330944238910963714/672223497736421388/1175060756652367983
fwiw, I had a custom_template all-in-1
{%- macro datum(datetime, add_year=false, add_hour = false) -%}
{%- set nu = as_timestamp(datetime) %}
{%- set months = ['januari','februari','maart','april','mei','juni','juli',
'augustus','september','oktober','november','december'] -%}
{%- set wdays = ['zondag','maandag','dinsdag','woensdag','donderdag',
'vrijdag','zaterdag'] -%}
{%- set wday = nu|timestamp_custom('%w',default=none)|int(default=0) -%}
{%- set month = nu|timestamp_custom('%m',default=none)|int(default=0) -%}
{%- set datum = nu|timestamp_custom(wdays[wday] ~ ' %-d ' ~ months[month-1]) -%}
{%- set datum = datum ~ ' ' ~ datetime.year if add_year else datum -%}
{%- set datum = datum ~ ', om ' ~ datetime.hour ~ ' uur' if add_hour else datum -%}
{{- datum -}}
{%- endmacro -%}
allowing this
https://discord.com/channels/330944238910963714/672223497736421388/1175030376046006312
{% from 'tijd.jinja' import datum %}
{{datum([ns.spring,ns.fall]|min,add_year=true, add_hour=true)}}
# non dst date times too
{{datum(strptime(states('sensor.afvalwijzer_next_date'),'%d-%m-%Y')|as_local, add_year=true, add_hour=true)}}
Since installing v2, the hour part of big_time_between is rounded, but given a .0
decimal place.
For example, the following returns 1.0 hour and 59 minutes
:
{% from 'easy_time.jinja' import big_time_between %}
{{ big_time_between(now(), now() + timedelta( hours = 1, minutes = 59 )) }}
It would be useful to have a short version of (or be able to customize) the relative times. So, instead of "2 hours and 59 minutes" it says "2 hrs 59 mins".
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.