If it shows UTC, that means you didn’t set your time up properly. It should show your timezone, not UTC.
Assuming you’re in London, your TZinfo should show Europe/London
If it shows UTC, that means you didn’t set your time up properly. It should show your timezone, not UTC.
Assuming you’re in London, your TZinfo should show Europe/London
A-ha! Mystery solved, thank you. My setup was done before all the visual configuration was added, so it was disabled in the front end. I added time_zone: "Europe/London"
to configuration.yaml and it’s now showing correctly. Thanks for the pointers.
Hi,
I’m trying to manage a countdown in ESPhome, I starter here
{{state_attr("timer.sprinklercountdown","finishes_at")}}
{{now()}}
{{as_timestamp(now())}}
{{as_timestamp(state_attr("timer.sprinklercountdown","finishes_at"))-as_timestamp(now())}}
{{state_attr("timer.sprinklercountdown","duration")}}
{{
(
as_timestamp(state_attr("timer.sprinklercountdown","finishes_at"))
-
as_timestamp(now())
)
|
timestamp_custom('%H:%M:%S')
}}
but count “returns” +1h (it should be 01:29:09)
countdown
02:29:09
finishes_at
2022-11-09T10:00:10+00:00
now
2022-11-09 09:31:00.019906+01:00
now timestamp
1667982660.019962
finishes_at timestamp
5349.979902982712
duration
1:30:00
countdown
02:29:09
thoughts?
{{ state_attr("timer.sprinklercountdown","finishes_at")|as_datetime - utcnow() }}
Thank you @petro , can you explain me why utcnow fix it as the now is seems to be in the same tz
the result here should b3 1:30, why i got 2:30
finishes_at
2022-11-09T10:00:10+00:00
now
2022-11-09 09:31:00.019906+01:00
Because your finishes at attribute doesn’t have a timezone associated with it, meaning it’s utc. So you need to compare it against now, but in the utc timezone.
Thank you @petro got it,
so, it will not impacted as well by DST? correct?
one more if I can:
How can I remove milliseconds?
1:14:59.761721
i tried
|timestamp_custom(‘%H:%M:%S’)
but
ValueError: Template error: timestamp_custom got invalid input '1:12:01.218924' when rendering template
I’ve been fighting with this for a full day and I’m not sure why it isn’t working.
I have a handful of input_datetime.xxxx entities that I’m updating with a Node-RED flow. I can see the state change to the current day and time in Developer Tools → States:
Also in that screen shot is a template sensor that I want to update when the intput_datetime entity changes. Here’s the template sensor definition:
sensor:
- unique_id: time_left_home
name: Time Left Home
icon: "mdi:home-clock"
state: >
{% set today = as_timestamp(now()) | timestamp_custom("%y-%m-%d") %}
{% set yesterday = as_timestamp(now()-timedelta(1)) | timestamp_custom("%y-%m-%d") %}
{% set left = as_timestamp(states('input_datetime.time_left_home')) | timestamp_custom("%y-%m-%d") %}
{% set day = as_timestamp(states('input_datetime.time_left_home')) | timestamp_custom("%A") %}
{% set time = as_timestamp(states('input_datetime.time_left_home')) | timestamp_custom("%-I:%H %p") %}
{% if left == today %}
at {{ time }}
{% elif left == yesterday %}
yesterday at {{ time }}
{% else %}
{{ day }} at {{ time }}
{% endif %}
I can update the input_datetime easily, but the state of the sensor does not change. Why is that?
Template looks fine. Could be optimized, but that’s not the problem. Therefor your problem is most likely due to poorly formatted yaml. The lack of a dash in front of the sensor has me thinking that you’re overwriting the sensor section later on or you’re pairing it with a trigger accidentally.
Yeah, as I tend to build iteratively, optimized code is not my forte. Every so often, I get motivated to clean things up and eliminate a goodly half of what I’ve pecked out previously.
That looks like it was my issue, thank you. I also noticed that I was using ("%-I:%H %p")
where I should have had ("%-I:%M %p")
.
Very very new to templating, so,
I am trying to configure a template for a sensor value (uptime in seconds), the value comes from a json feed. Found the following:
{## Imitate available variables: ##}
{% set my_test_json = {
“uptime”: 1064848
} %}
{%- set TIME_MAP = {
‘Weeks’: (my_test_json.uptime / 604800) % 604800,
‘Days’: (my_test_json.uptime / 86400) % 7,
‘Hours’: (my_test_json.uptime / 3600) % 24,
‘Minutes’: (my_test_json.uptime / 60) % 60,
‘Seconds’: (my_test_json.uptime % 60)
}
-%}
The Uptime is {{ TIME_MAP }}
{% set time_min = ((my_test_json.uptime % 3600) / 60) | int %}
minutes {{ time_min }}
The result is:
The Uptime is {‘Weeks’: 1.7606613756613756, ‘Days’: 5.32462962962963, ‘Hours’: 7.791111111111093, ‘Minutes’: 47.46666666666715, ‘Seconds’: 28
minutes 47
Two questions: Please help
1- how to trim response to whole numbers “Weeks: 1.7606613756613756” to “Weeks: 1”. snd so on
2-how do i reference TIME_MAP as an entity on a card???
{## Imitate available variables: ##}
{% set my_test_json = {
"uptime": 1064848
} %}
{%- set TIME_MAP = {
'Weeks': ((my_test_json.uptime / 604800) % 604800) | round(0),
'Days': ((my_test_json.uptime / 86400) % 7)| round(0),
'Hours': ((my_test_json.uptime / 3600) % 24)| round(0),
'Minutes': ((my_test_json.uptime / 60) % 60) | round(0),
'Seconds': (my_test_json.uptime % 60) | round(0)
}
-%}
The Uptime is {{ TIME_MAP }}
{% set time_min = ((my_test_json.uptime % 3600) / 60) | int %}
minutes {{ time_min }}
Wow Thank you for the very fast reply,
however " | round(0)" was the very first ft thing I tried, looked as though the “round” was not executed
{%- set TIME_MAP = {
‘Weeks’: (my_test_json.uptime / 604800) % 604800 | round(0),
‘Days’: (my_test_json.uptime / 86400) % 7| round(0),
‘Hours’: (my_test_json.uptime / 3600) % 24| round(0),
‘Minutes’: (my_test_json.uptime / 60) % 60| round(0),
‘Seconds’: (my_test_json.uptime % 60)
}
-%}
The Uptime is {{ TIME_MAP }}
results in:
The Uptime is {‘Weeks’: 1.7606613756613756, ‘Days’: 5.32462962962963, ‘Hours’: 7.791111111111093, ‘Minutes’: 47.46666666666715, ‘Seconds’: 28}
that’s because yours isn’t like the code I suggested. You are putting the round in the wrong place.
groupings matter.
right now the way you are rounding is only rounding the value after the % (604800, 7, 24 & 60).
This is an option too:
{{ ("1064848" | as_timedelta).days }}
Unfortunately, you can only get days, seconds and microseconds directly this way. For weeks, you still need to do a calculation. See datetime — Basic date and time types — Python 3.11.0 documentation.
You can avoid rounding altogether by doing integer division.
Days:
{{ 1064848 // 24 }}
Please format your code for readability.
Hi all,
I’m trying to calculate the exact time when my car is finished charging. There is a sensor which returns a string formatted time:
{{ states('sensor.car_charging_time_left') }}
Result type: string
01:40
I tried converting the result to a UNIX epoch timestamp. But have been unsuccessful so far. I also need to take into account that the calculated time will be on the next day.
Complete code I’m working on:
{% set time_left = 'sensor.car_charging_time_left' %}
Battery full time: {{ ( now().timestamp() + ((states('time_left')) | float) | timestamp_custom("%H:%M") ) }}
{% set time_left = (states('sensor.car_charging_time_left') ~ ':00') | as_timedelta %}
{% if time_left %}
Battery full time: {{ (now() + time_left).strftime('%H:%M') }}
{% else %}
unknown
{% endif %}
Hi Petro,
It looks like there is a minor delta in the time you calculate, as to what supposed to be calculated:
{% set time_left = (states('sensor.car_charging_time_left') ~ '00') | as_timedelta %}
{% if time_left %}
Battery full time: {{ (now() + time_left).strftime('%H:%M') }}
{% else %}
unknown
{% endif %}
current time: {{ now().strftime('%H:%M') }}
sensor time left: {{ states('sensor.car_charging_time_left') }}
petro time left: {{ time_left }}
Result:
Battery full time: 13:26
current time: 13:18
sensor time left: 00:05
petro time left: 0:08:20
sorry, line 1 should be
{% set time_left = (states('sensor.car_charging_time_left') ~ ':00') | as_timedelta %}
I have been working to learn template utilization…
The idea being to convert uptime seconds to a more descriptive form…
{## Imitate available variables: ##}
{% set my_test_json = {
“uptime”: 1064848
} %}
{% set time_wks = ((my_test_json.uptime / 604800) % 604800) | round(0) %}
{% set time_dys = ((my_test_json.uptime / 86400) % 7) | round(0) %}
{% set time_hrs = ((my_test_json.uptime / 3600) % 24) | round(0) %}
{% set time_min = ((my_test_json.uptime / 60) % 60) | round(0) %}
TimeUp: {{ time_wks }} Weeks, {{ time_dys }} Days, {{ time_hrs }} Hours, {{ time_min }} Minutes
results in:
TimeUp: 2 Weeks, 5 Days, 8 Hours, 47 Minutes
My question: How do i reference these variables (time_wks, time_dys, etc) in an entities statement (entities card)???