Sensor - Date formatting

I have a template sensor setup


- platform: template
  sensors:
    calendar_event_early_dismissal:
      friendly_name: Early Dismissal
      value_template: >
                        {{ as_timestamp(states.calendar.early_dismissal.attributes.start_time) | timestamp_custom('%A - %x') }}

when I put this in the Template in Home assistant I get the proper format “Monday - 09/02/19”
{{ as_timestamp(states.calendar.early_dismissal.attributes.start_time) | timestamp_custom(’%A - %x’) }}

However, the Sensor itself in home assistant comes up as
“sensor.calendar_event_no_school”
"Monday - 09/02/19 #57000.0 "

Can anyone explain why it puts the Numbers in the state, and how to exclude them as I just want the Date only.

Thanks in advance

here a bit of reading

http://strftime.org/

explain what each letter means

I was there, which is where I got my information from. However unless I am missing something it doesn’t explain why it is putting “#xxxxx” in the state and when I put the code in Home assistant template it shows it exactly how I want to see it.


mmmm never seen that before is it something the calendar has added

could it be Microsecond ??? from mid nite

What component are you using for calendar.early_dismissal?

Sorry I just noticed I screenshot ed from 2 different sensors (no_school, Early Dismisal), however they both do the same thing.

its comes from one of my Google Calendars, so the Google Calendar component.
However the states show this for
“calendar.no_school”
message: No School-Labor Day
all_day: true
offset_reached: false
start_time: 2019-09-02 00:00:00
end_time: 2019-09-03 00:00:00
location:
description:
friendly_name: No School

I am guessing it could be adding some sort of time in seconds which is part of the “Start_time” to the end which could explain the “#xxxxxx” but how do I remove that on the custom template via the sensor side.

I’ve done something somewhat similar, settings sensors to pickup if it’s a school_day or a work_day based on day of the week, as well as Google Calendar integrations.

This thread in the forums helped me a bunch with the formatting: The EPIC Time Conversion and Manipulation Thread!

What I did personally was create a sensor to grab the event date from a calendar (this one’s our school’s publicly available calendar):

    date_now_xxxx:
      entity_id: sensor.date
      value_template: >
        {% if states('calendar.xxxx') == 'off' %}
          off
        {% elif now().timestamp() >= as_timestamp(strptime(states.calendar.xxxx.attributes.start_time, '%Y-%m-%d %H:%M:%S')) and now().timestamp() <= as_timestamp(strptime(states.calendar.xxxx.attributes.start_time, '%Y-%m-%d %H:%M:%S'))  %}
          on
        {% else %}
          off
        {% endif %}

This would tell me if the event is active currently (on / off). Then I have another sensor to read the type of event and determine if it means school is in / out that day. I also have it reference our family calendar (for if we’re out of town/vacation), as well as days of the week (for if it’s a weekend).

date_schoolday:
      value_template: >
        {% set ct = states('sensor.date_time') %}
        {% set ct = as_timestamp(strptime(ct,'%Y-%m-%d, %H:%M')) %}
        {% if 'Saturday' in ct | timestamp_custom("%A") %}
          off
        {% elif 'Sunday' in ct | timestamp_custom("%A") %}
          off
        {% elif is_state("sensor.date_now_XXX","on") and 'no school' in states.calendar.XXX.attributes.message.lower() %}
          off
        {% elif is_state("sensor.date_now_YYY","on") and 'oliday' in states.calendar.YYY.attributes.message.lower() %}
          off
        {% elif is_state("sensor.date_now_YYY","on") and 'no school' in states.calendar.YYY.attributes.message.lower() %}
          off
        {% elif is_state("sensor.date_now_YYY","on") and 'family trip' in states.calendar.YYY.attributes.message.lower() %}
          off
        {% elif is_state("sensor.date_now_ZZZ","on") and 'holiday for all students' in states.sensor.ZZZ_calendar_event_0.attributes.name.lower() %}
          off
        {% elif is_state("sensor.date_now_ZZZ","on") and 'spring break' in states.sensor.ZZZ_calendar_event_0.attributes.name.lower()  %}
          off
        {% else %}
          on
        {% endif %}

This then outputs either date_schoolday is ‘on’ or ‘off’ and I can use it in conditions to fire / negate different automations.

You’ll obviously have to play with it to see how your calendar events and such are setup to ensure you’re reading them correctly (start vs. end date [or both!), event title/name/text, etc…

I’m not sure this is exactly what you’re after, but hopefully it helps.

1 Like

Thanks, I will take a look at that.

Right now I am just wanting to know when the Next day there was a early out or no school so my daughter can look at that instead of the school calendar for that information. but eventually would implant something like you have.

After playing with this all day I finally got it figured out by simply doing this

value_template: >
                        
                        {% set date= states.calendar.early_dismissal.attributes.start_time %}
                        {% set date= as_timestamp(states.calendar.early_dismissal.attributes.start_time) | timestamp_custom('%A - %x') %}
                        {{date}}

That’s identical to what you originally posted… just more verbose. Also, the first {% set date = %} is overwritten by your second one.

This portion you posted here:

is identical to this without setting it equal to a variable.

{{ as_timestamp(states.calendar.early_dismissal.attributes.start_time) | timestamp_custom('%A - %x') }}

Which is identical to what you posted originally in your question

I’m confused as to what this solves?

I am not sure why, but the way I have it eliminates the “#21900.0” which does change every minute, not sure exactly why it was showing up, Not sure why it makes a difference between how I have it now and originally had it but it now gone with the way I have it now.