The EPIC Time Conversion and Manipulation Thread!

You can’t they only exist in the template.

can i create a string to refence later??

You can add them as attributes in a entity or make separate entities for each

You were asked before to format your code. Please format it for readability’s sake.

1 Like

Thank you,
My project included using the PIAware Json to begin to build a status card for my site.
The card included: temp up time, nearby aircraft, status, etc. All was straight forward except for converting time (in seconds) to wks, day, hrs, min.

The code in the sensor.yaml

  • platform: command_line
    command: “curl -s http://10.1.1.40:8080/data/aircraft.json | grep hex | wc -l”
    scan_interval: 60
    name: aircraft nearby

  • platform: rest
    name: FR24 Aircraft
    resource: http://10.1.1.40:8080/data/aircraft.json
    value_template: “{{ value_json.messages }}”
    method: GET
    scan_interval: 30
    json_attributes:

    • now
    • aircraft
  • platform: rest
    name: Piaware Status
    json_attributes:

    • dump978_version
    • cpu_load_percent
    • time
    • system_uptime
    • piaware
    • cpu_temp_celcius
    • dump1090_version
      resource: http://10.1.1.40:8080/data/status.json
      method: GET
      value_template: “{{ value_piaware_status}}”
      scan_interval: 15
  • platform: template
    sensors:
    dump978_version:
    friendly_name: “PI Dump978”
    value_template: “{{ state_attr(‘sensor.piaware_status’, ‘dump978_version’) }}”

    cpu_load_percent:
    friendly_name: “PI CPU Load”
    value_template: “{{ state_attr(‘sensor.piaware_status’, ‘cpu_load_percent’) }}”
    unit_of_measurement: ‘%’

    time:
    friendly_name: “PI CPU Time”
    value_template: “{{ state_attr(‘sensor.piaware_status’, ‘time’) }}”

    system_up:
    friendly_name: “Pi Uptime”
    value_template: “{{ ((state_attr(‘sensor.piaware_status’, ‘system_uptime’) / 604800) % 604800) | round(0) }} Wks, {{ ((state_attr(‘sensor.piaware_status’, ‘system_uptime’) / 86400) % 7) | round(0) }} Days, {{ ((state_attr(‘sensor.piaware_status’, ‘system_uptime’) / 3600) % 24) | round(0) }} Hours, {{ ((state_attr(‘sensor.piaware_status’, ‘system_uptime’) / 60) % 60) | round(0) }} Minutes.”

    piaware:
    friendly_name: “PI PiAware Status”
    value_template: “{{ state_attr(‘sensor.piaware_status’, ‘piaware’)[‘message’] }}”

    cpu_temp_celcius:
    friendly_name: “PI CPU Temp”
    value_template: “{{ state_attr(‘sensor.piaware_status’, ‘cpu_temp_celcius’) | round(1)}}”
    unit_of_measurement: ‘°C’

    dump1090_version:
    friendly_name: “PI Dump1090”
    value_template: “{{ state_attr(‘sensor.piaware_status’, ‘dump1090_version’) }}”

Thank you our guidance, learned a lot,

Folks, from an attribute of a sensor I have a date (as string). :white_check_mark:
I convert it to a date object using the as_timestamp function. :white_check_mark:
I want to format the date to contain the day of the week – in German.
What I get when I use %A in the timestamp_custom function (with date 2023-04-07 as an example) is Friday instead of Freitag.
And this is independent of the language setting in my profile! Even when set to Deutsch (German) I get Friday!
I’d love to have the HA GUI in English but in my template the weekday(s) in German …

Anything I can do about that? :flushed:
:pray:

Home Assistant’s templating language defaults to English, regardless of the language you’ve chosen for Home Assistant’s UI or even the operating system’s locale.

What you can do is create a template to convert from English to German. Here’s an example:

1 Like

Thanks so much, @123, for the help!! :hugs: Learned something new today thanks to you. :blush: :+1::+1:

Just to share a bit of what I’ve done with it, maybe it helps somebody else: I learned how to write a macro/function and wrote one that takes a date sensor’s name, get its date attribute and formats it according to my needs (with German weekday name in front, then a comma, then the German date version with dd.㎜.yyyy).
Here’s the code that works fine in the developer tools’ template tab:

{% macro formatted_date(datesensor) -%}
{% set thedate = as_datetime(state_attr(datesensor, 'date')) %}
{{ 
['Montag', 'Dienstag', 'Mittwoch', 'Donnerstag', 'Freitag', 'Samstag', 'Sonntag'][thedate.weekday()]
}}, {{ thedate.strftime('%d.%m.%Y') }}
{%- endmacro %}

Makro-Test: {{ formatted_date('sensor.cal_valentine_s_day') }} 

As you can see I have set up a few (31) holiday dates by use of the Calendarific integration. In the above example you see the Valentine’s day date sensor which holds the number of days until Valentine’s day occurs (the exact date is in its date attribute).

Now it’s getting a bit off-topic: I use a lovelace entities card (pimped with the secondary-entity-row HACS frontend add-on) and want to write the exact date of that holiday into the secondary info of the sensor.
This works fine – in English:


Here’s the start of that card’s code:

type: entities
title: Feiertage
show_header_toggle: true
header:
  image: >-
    https://www.home-assistant.io/images/dashboards/header-footer/balloons-header.png
  type: picture
icon: mdi:party-popper
entities:
  - type: section
    label: Feiertage in NRW
  - entity: sensor.cal_valentine_s_day
    type: custom:secondaryinfo-entity-row
    secondary_info: >-
      {{ as_timestamp(state_attr('sensor.cal_valentine_s_day', 'date')) |
      timestamp_custom('%A, %d.%m.%Y')  }}
...

Now I “just” need to use my macro for the secondary_info.
But where can I put the macro definition so that I can use it as the secondary info?

I tried to put it at the top of this card’s code – didn’t work.
I tried to put it into the view’s code – not even saveable:

Sorry for being off-topic but maybe someone has a suggestion here?

1 Like

just for clarification, it’s python that’s doing this. The codebase for HA is set to english, therefor it returns english for the date & time libraries. Language also isn’t tied to the backend and the backend doesn’t know what langauge is set on the frontend. There has been changes to rectify this, however I doubt those changes will affect .strptime. When translations actually hit templates, it will most likely only hit HA added methods. e.g. timestamp_custom, etc

Thanks for this thread, helped me heaps.

2 Likes

I’m using input_datetime helpers but can not template them (for a card) just to show hh:mm instead of hh:mm:ss.

What do i need to do to achieve this?

Thanks for your help!

For display purposes you can use:

{{ states('input_datetime.time_only')[:-3] }}

Thank you.

Is there any comprehensive tutorial on that topic?

The method I showed is called string slicing. It is a standard Python method that is available in Jinja. There are many python tutorials where it is covered.

Beyond the official docs ( 1 & 2 ), one good written tutorial for Jinja templates in HA (with examples) can be found at: mysmarthome/readme.md at 1da8b4c837dee92397081e2b9dd93a574abf8043 · skalavala/mysmarthome · GitHub

Struggling with the following:

service: input_datetime.set_datetime
data:
  timestamp: "{{ states('sensor.2_hour_window_cheapest_electra') }}"
target:
  entity_id: input_datetime.vaatwasser_start

where values are as follows:

sensor.2_hour_window_cheapest_electra: 2023-03-17T11:00:00+00:00
input_datetime.vaatwasser_start: 2023-03-17 11:00:00

so a UTC formatted timestamp gets converted to a local timestamp. Desired result is that UTC format is maintained.

What am I missing?

The output from your sensor is a string representation of a datetime not a timestamp. Change timestamp: to datetime:

A timestamp is a number that represents the current time in seconds.

A datetime is a object that represents the current time in a ‘code-friendly’ manner.

Thanks for your suggestion.

If I run the following service call, UTC format is not mainained:

service: input_datetime.set_datetime
data:
  datetime: "{{ states('sensor.2_hour_window_cheapest_electra') }}"
target:
  entity_id: input_datetime.vaatwasser_start

as I am am getting the following results:

sensor.2_hour_window_cheapest_electra: 2023-03-17T12:00:00+00:00
input_datetime.vaatwasser_start: 2023-03-17 12:00:00

BTW: I am in NL, so correct local time for input_datetime.vaatwasser_start should read 2023-03-17 13:00

If you want the time in your timezone, you have to specify that in your template.

"{{ states('sensor.2_hour_window_cheapest_electra') | as_datetime | as_local }}"

YES! That works. Thanks much.