The EPIC Time Conversion and Manipulation Thread!

Hi everyone,

I’ve been trying to convert an Epoch date to a format that has the exact same properties as the entities listed here: System Monitor - Home Assistant

The benefit of systemmonitor entities is they are stored as a date like “April 29, 2023 at 11:28 AM” but when adding to an entity card they display as “2 weeks ago”

For my epoch date, I have successfully format it to any datestamp I could think of, but when adding to an entity card it displays the date itself, rather than “2 weeks ago”

This is my current query (it’s from a template in the Rest API)

- name: "Last Boot (Test)"
        value_template: "{{ value_json['updated'] | int | as_datetime | as_local }}"

DateEpoch

Thanks for your help

Solution found, needed to add device_class: timestamp

- name: "Last Boot (Test)"
    device_class: timestamp
    value_template: "{{ value_json['updated'] | int | as_datetime | as_local }}"
1 Like

@petro, I think I got this from you, but I am not able to locate that post, documentation, etc.

{{ ((now() | string | as_datetime | string) [2:] ) }}
# [2:] removes the first 2 characters
{{ ((now() | string | as_datetime | string) [:2] ) }}
# [:2] removes everything right of the 2nd character

Can anyone point to where the string [2:] or string [:2] is documented? I am currently trying to learn how to parse and merge results back together. Normally, I would read a book, but there are none available on YAML. I would appreciate if anyone can give me additional syntax or links.
Thanks to All in advance

The : is the slicing operator. So, as per the comments, [2:] skips the 0th and 1st characters in the string and give you everything from the 2nd to the end. The end is the length of the string and implied when no end index is given.

@parautenbach, Thanks for the reply. I have reviewed your link, thanks again! I am new to python, and had no idea what language the Template was calling. So, if I understand correctly, that slicing operator is developed to work with a dimensional array. HA doesn’t support arrays, correct?
what would be the python to midstr$ of an element. extracting the 4th to the 9th characters?
Thanks!

The templating language is called Jinja

Indexing and Slicing works on both arrays and strings.

An entity can have attributes that contain arrays or other data types, but an entity’s state is always a string.

{{ "1234567890"[3:9] }} returns “456789”

Way cool. You make a great teacher.

As D explained (while I was sleeping), the templating language is Jinja. It’s linked to from the template editor under the dev tools. There’s a close relationship between Jinja and Python. I just find the Python docs a bit more comprehensive in some cases — like this one.

Strings are arrays of characters.

Since you’re on the learning path: It does, but it depends. Objects such as dictionaries and arrays (lists) can be stored in sensor attributes. States are always strings, so you need to cast it as necessary (even though they might look like numbers or datetimes).

So I have problem that I need to extract only hours / minutes from sensor which has all values…

Example of sensor:
sensor.sun_next_rising
2023-08-29T04:14:48+00:00
To compare with
states(‘sensor.time’)

So the goal is to push automation on exact time when the current time to compare with sun_next_rising is TRUE.

My automation:

- id: '116'
  alias: ZUNAJ - ugasni LED (zjutraj, sonce)
  trigger:
  - platform: template
    value_template: "{{ states('sensor.time') == **TO_USE(SENSOR.SUN_NEXT_RISING)** }}"
  condition: []
  action:
  - service: light.turn_on
    data:
      entity_id: light.shelly_rgbw2_mqtt_zunaj
      brightness: 230
      rgbw_color:
        - 0
        - 0
        - 0
        - 230
      effect: 0
  mode: single

So, do anybody know how to achieve this?

No template needed, just use the Sun Trigger

I’m using:
image

So need to convert sensor value as described… Sun Trigger is not good in my case…

I don’t understand how a card explains why you can’t use a Sun trigger for an automation, but whatever…

{{ today_at(states('sensor.time')) >= states('sensor.sun_next_rising')|as_datetime|as_local }}

1 Like

Because it’s different values which suits me better in my case.

Thank you for your help… it’s seem’s that’s working… will test in morning :slight_smile:

hmm… As I check this will not work…

Need to parse only hour and minutes, nothing else… otherwise status will be allways “False” as date will be not correct… How to get from both values only hour:minutes?

{{ today_at(states(‘sensor.time’)) }}
{{ states(‘sensor.sun_next_rising’)|as_datetime|as_local }}

Thank you again…

The today_at() function will update the date automatically so the dates will match when the time comes…

You can use strftime() to then use a string comparison.

{{ states('sensor.time') == 
(states('sensor.sun_next_rising') | as_datetime | as_local).strftime('%H:%M') }}
1 Like

Great… THANK YOU again… now works 100%…

Code:

# Ugasni LED luč: ZUNAJ (zjutraj, sonce)
- id: '116'
  alias: ZUNAJ - ugasni LED (zjutraj, sonce)
  trigger:
  - platform: template
    value_template: "{{ today_at(states('sensor.time')).strftime('%H:%M') == (states('sensor.sun_next_rising')|as_datetime|as_local).strftime('%H:%M') }}"
  condition: []
  action:
  - service: light.turn_off
    data:
      entity_id:
        - light.shelly_rgbw2_mqtt_zunaj
  mode: single

I’m trying to figure out how to use the Entity or Entities card to set seconds on an input_datetime entity.

When I add the entity to the entities card I get an input field that only shows HH:MM, but the entity is made up of HH:MM:SS. I can make changes to the seconds using the States developer tool, but I’m trying to keep everything on one card.

Electric Meter Card

I’m trying to do this because I have a working dual rate electricity meter (based on HA Glow) and I’ve got 2 sets of meter readings working perfectly and staying in sync with whats on the meter. I’ve noticed a slight drift due to the fact that meter is synced to its own clock which drifts from internet time by some 30 seconds a month. At the moment this is controlled by changing the trigger times in a Energy Meter Helper, but I’ve like to put all the offsets in one place (Entities Card).

Any guidance or comments would be really appreciated.

one of the mnost confusing posts for a non developer lol
Is there a stock standard template someone has that can do the below

{{ (states('sensor.laurens_bmw_charging_end_time')|as_datetime|as_local).strftime('%d %B %Y at %-I:%M %p') }}