The EPIC Time Conversion and Manipulation Thread!

@finity you were right!! the “>” was the missing piece.
I read the @Didgeridrew suggestion, but, as I’m a ‘hardware guy’ I googled ‘python multi-line quote symbol’ and couldn’t find anything useful

hahaha :rofl:
thank you both for the excellent support!

have a nice weekend!

1 Like

Hi all, I am not at all good with templates but I’m trying to use one in a Markdown card to extract the date only from a timestamp, but don’t really know how to do it. Wondering if someone can please help me.

type: markdown
content: >-
  **ON THE WAY:**

  {% for package in
  states.sensor.seventeentrack_packages_in_transit.attributes.packages %} **{{
  package.friendly_name }}:** Last tracked on {{ package.timestamp }} {{
  package.info_text }}. 

  {% endfor %}

In the above I’d like the timestamp to only show the date, in the format dd-mm-yy. Could someone please show me how this is done, if it is actually possible?

assuming “package.timestamp” returns a valid timestamp it should be:

{{ package.timestamp | timestamp_custom("%d %h %Y") }}

I think it should be:

{{ package.timestamp | timestamp_custom("%d-%m-%y") }}

https://docs.python.org/3/library/datetime.html

1 Like

oops! you’re right.

I was playing around with another template and posted the wrong thing.

thanks for the correction.

1 Like

Hey there,

Probably you can help me. I have no idea what I am doing wrong. I am trying to get m GameDay sensor right (which I did with this tutorial Create an "Upcoming Sports Game" card with Google Calendar) I was already writing down my request in this group but no one could help me

I used the sensor configuration everyone else has but it shows not what it’s supposed to. It should be like "next Game Saturday 26th at 15.30 but instead it’s formatted terribly

Thats my code

 #Eintrachts Next Up Date
  - platform: template
    sensors:
      eintracht_date:
        entity_id: calendar.eintracht_frankfurt_spielplan_profimannschaft
        friendly_name: Eintrachts next game
        value_template: >-
          {% if is_state('calendar.eintracht_frankfurt_spielplan_profimannschaft', 'on') %}
            Playing Now
          {% else %}
            {% set st = state_attr('calendar.eintracht_frankfurt_spielplan_profimannschaft', 'start_time') %}
            {% if st != None %}
              {{ as_timestamp(st)  | timestamp_custom("%A, %b %d at %-I:%M %p") }}
            {% else %}
              No games scheduled
            {% endif %}
          {% endif %}
          
  #Eintrachts Next Opponent
  - platform: template
    sensors:
      eintrachts_opponent:
        entity_id: calendar.eintracht_frankfurt_spielplan_profimannschaft
        friendly_name: Eintrachts Next Opponent
        value_template: >-
          {{ states.calendar.eintracht_frankfurt_spielplan_profimannschaft.attributes.message }}

image

What does the lovelace card config look like?

The card code looks like this

title: null
type: custom:vertical-stack-in-card
cards:
  - type: entities
    entities:
      - type: custom:template-entity-row
        entity: calendar.eintracht_frankfurt_spielplan_profimannschaft
        image: /local/icons/Eintracht.png
        name: Next Game
        secondary: >-
          {{states.calendar.eintracht_frankfurt_spielplan_profimannschaft.attributes.message
          }}
        state: >-
          {{states.calendar.eintracht_frankfurt_spielplan_profimannschaft.attributes.start_time
          }}
styles:
  card:
    - font-size: 5px

The way it’s displayed is because you are using the original calendar information instead of the statre of the sensor you created.

try replacing the state line in the card with this:

state: "{{states('sensor.eintracht_date') }}"
1 Like

THANK YOU SO MUCH! That fully helped and saved my Day :heart:

1 Like

have been around for this before, but am having trouble again.

an mqtt sensor has value

2022-03-06T16:02:11Z and my local time is 1 earlier than that.

I want it to show a timestamp (and have the frontend show the relative time) so use:

{{as_datetime(value).isoformat()}} # or
{{as_datetime(value)}}

resulting in

which is still an put off.

trying as_local Adds an hour…

now how do I get it to subtract that 1 hour…?

I did see the Zulu time being mentioned as early in this thread as The EPIC Time Conversion and Manipulation Thread! - #11 by finity but it doesnt really help me find the answer.

All 3 are identical time. One is your local time, the other are utc. If the time is wrong, that means your input is wrong.

you mean the source?

just tried:

{{value|as_timestamp|timestamp_local}}

resulting in

its not really very important, and no system automation depend on it, I just like to get it right…

1 Like

It seems that your system thinks you are in a TZ 1 hour ahead of UTC instead of an hour before.

Yes, so if you were to take the +1 hour of your time zone and subtract it from local you get utc. If the input time is local, then you need to convert it as a local timestamp, not utc like you’re doing

yes, exactly… but my system time is ok

This is both of you not understand that the conversion is treating the sting as utc. Make it timezone agnostic by removing the Z. Then use as datetime and as local

nevermind I misread the problem and got my +1 & -1 reversed.

I think based on this:

right!

went from


to

and is still an hour off, because now it states the 16:02 time (and it should show 15:02)

full disclosure:

  - platform: mqtt
    unique_id: watermeter_smart_gateways_startup_time
    state_topic: watermeter/smart_gateways/startup_time
    name: Watermeter startup time
    value_template: >
     {{value.split('Z')[0]|as_timestamp|timestamp_local}}
    device_class: timestamp

Yes there’s 3 types of datetimes: local, utc, and none. None has no concept of a what timezone it is, so you can then attach one to it… by using as_local.