Can someone please tell me how I can remove the leading zero on a time that is before 10:00 am/pm? I’m looking to format the time (09:17) into (9:17) for a web view.
Thanks.
Can someone please tell me how I can remove the leading zero on a time that is before 10:00 am/pm? I’m looking to format the time (09:17) into (9:17) for a web view.
Thanks.
Assuming you’re using a template, you should be able to use lstrip. If you give it an argument, it’ll only strip the matched argument.
{% set timetext = '09:14' %}
{% set timestamp = now().strftime("%I:%M") %}
{{ timetext.lstrip('0') }}
{{ timestamp.lstrip('0') }}
Play with this in your template dev tool.
That worked for me. Here’s the code I ended up using (for everyone else’s use):
platform: template
sensors:
sunset_time:
value_template: '{% set timestamp = as_timestamp(states.sun.sun.attributes.next_setting) | timestamp_custom("%I:%M") %} {{ timestamp.lstrip("0") }}'
friendly_name: "Sunset"
That’s a template of what? Where does that go? How can I use that method to remove leading zero of clock on a display in esphome? Thanks. need 9:35 instead of 09:35
time:
- platform: sntp
id: esptime
display:
- platform: ssd1306_i2c
model: "SSD1306 128x64"
address: 0x3C
lambda: |-
it.strftime(64, 32, id(myfontx), TextAlign::CENTER, "%I:%M", id(esptime).now());
Given that I’ve searched before to no avail (clearly missing this thread!), and I’m finally getting rid of that 0, I’m responding to the last question to make sure future newbies know what to do:
The code to create your template sensor goes into your config.yaml. The above example creates a sensor that shows the time of the sunset. Here’s my code that shows the current time.
platform: template
sensors:
- name: Time
state: >
{% set timestamp = as_timestamp(states('sensor.date_time_iso')) | timestamp_custom('%I:%M %p') %}
{{ timestamp.lstrip("0") }}
I also have an ESPHome oled display. Instead of using esptime, I added my sensor as a text sensor, then displayed that. Here’s the relevant ESPHome code:
text_sensor:
- platform: homeassistant
id: current_time
entity_id: sensor.time
internal: true
display:
...
lambda: |-
// Print time (from homeassistant sensor)
if (id(current_time).has_state()) {
it.printf(64, 61, id(font1), TextAlign::BASELINE_CENTER, "%s", id(current_time).state.c_str());
}
Clearly all here are good sleepers and no one has seen that just after midnight 00:15, say, comes out as :15 after the lstrip method.
What can I do to get 0:15?
Found the solution in another thread: Template - date - hour question - should be simple! - #3 by tom_l
Simply using %-H will do the trick!