Trying to setup a simple date time input/output format - getting random date and time

Ok, so this shouldn’t be as headache-inducing as its been, but I’m just try to accomplish a few things here.

  1. ultimately have a frontend Date & Time displayed from a timestamp i set in an input_datetime var in hass

  2. in an automation I’m trying this [below] method to format the date and time the way i want it ultimately displayed. But as you can see from the dev templating screen below, I’m getting this random date and time from my input_datetime thats obviously not right. How is this happening; where is it getting that December 31, 1969 and 7:00 PM from?

You can see the data is correct in the states above for that input_datetime.


date: >
        {{ now().strftime('%B  %d, %Y') | replace(" 0", "") }}
time: >
        {{ (now().strftime('%-I:%M %p')) }}
        
{{ states.input_datetime.cgm_sensor_start }}

================================

date: >
{% set date = states('input_datetime.cgm_sensor_start') | float  %}
{{ date | timestamp_custom("%B  %d, %Y", true) }}

time: >
{% set time = states('input_datetime.cgm_sensor_start') | int %}
{{ time | timestamp_custom("%-I:%M %p", true) }}

  1. Heres my next issue beyond this one. Even when the state is manually set to the format i want, its displayed in the front end in its original format. But more_info on that input_datetime shows the correct format?

I’m starting to think input_datetime cant handle these new formats I’m trying to input.
I’m assuming i need a sensor template to take the original datetime and convert it for display in the front end. But this is where I’m lost. There’s just too many things going on here that dont add up.

Could anyone post the method to do this efficiently and properly? I just need a date and time (i set) to be viewable in the frontend in the format of “July 4,2020 5:08 PM”. Maybe I’m going about this all wrong.

Thanks for any help you could provide,

You do realize that this is rubbish, right?

{% set date = states('input_datetime.cgm_sensor_start') | float  %}

The state value of an input_datetime is a string value and converting it with the float filter produces zero.

If you want to present the state of an input_datetime in a desired format, simply use its timestamp attribute.

{{ state_attr('input_datetime.cgm_sensor_start', 'timestamp') | timestamp_custom('%B %-d, %Y') }}
{{ state_attr('input_datetime.cgm_sensor_start', 'timestamp') | timestamp_custom('%-I:%M %p') }}
3 Likes

Thanks A lot. I knew that was wrong. Just one more little thing and I’m all good.
The output now is (‘July 4, 2020’, ‘5:08 AM’) with the ( and ‘ on both ends; how can i remove those?

Here my sensor.


  - platform: template
    sensors:
      cgm_sensor_start:
        friendly_name: "G6 Sensor Start: "
        value_template: >
            {% set date = state_attr('input_datetime.cgm_sensor_start', 'timestamp') | timestamp_custom('%B %-d, %Y') %}
            {% set time = state_attr('input_datetime.cgm_sensor_start', 'timestamp') | timestamp_custom('%-I:%M %p') %}
            {{date, time}}

The reason i want this static data as a sensor is because of where i can use it, and how it displays in the frontend.

Wow, speaking of rubbish… thats what my brain is today… why am i setting anything??? Ugh…

This is works perfectly. Thanks so much.


  - platform: template
    sensors:
      cgm_sensor_start:
        friendly_name: "G6 Sensor Start: "
        value_template: >
            {{ state_attr('input_datetime.cgm_sensor_start', 'timestamp') | timestamp_custom('%B %-d, %Y')}} // {{state_attr('input_datetime.cgm_sensor_start', 'timestamp') | timestamp_custom('%-I:%M %p') }}
    

This is combining the two variables to form a tuple:

            {{date, time}}

Change it to this:

            {{date}}, {{time}}

Yeah, that works too. But setting time and date vars was redundant

How are you getting this to work? I’m simply trying to display an input_datetime in a template, formatted for 12-hour time.

{{state_attr('input_datetime.coffee', 'timestamp') | timestamp_custom('%-I:%M %p')}}

This code outputs “11:30 PM”, even though input_datetime.coffee is actually set to 6:30 AM. Is there a new way to do this?

1 Like

sounds like it might be a timezone issue.

try this:

{{state_attr('input_datetime.coffee', 'timestamp') | timestamp_custom('%-I:%M %p', true) }}

or if that fails to fix it:

{{state_attr('input_datetime.coffee', 'timestamp') | timestamp_custom('%-I:%M %p', false)}}
1 Like

‘False’ saved the day! Thank you!

2 Likes