Pulling specific data with JSON

I’m sure this has been asked before but I can’t seem to find anything similar.

I am trying to pull just the date from a senor. Currently the sensor returns the date and time like this

2019-09-18 00:00:00

I’d like to strip the time “00:00:00” and leave just the “2019-09-18”

I’d love to take the date and make it something more user friendly eg. Wednesday Sept 18. But right now I’d be happy with just the date.

To get this data I’m using this to pull the start_time from a calendar entry:

  - platform: template
    sensors:
      collection_date:
        entity_id:
          - calendar.waste_collection
        value_template: "{{state_attr('calendar.waste_collection', 'start_time')}}"

Not sure what JSON has to do with this, but assuming the state of sensor.collection_date looks like your sample, then you can do this:

{{ as_timestamp(states('sensor.collection_date'))|timestamp_custom('%A %b %d') }}

@pnbruckner You’ve exposed my lack of JSON understanding :slight_smile:

As for the {{ as_timestamp ...

How and where do I properly use this? If there is a how-to I can read that would be great.

@pnbruckner

Thanks, I was hoping for something that doesn’t assume I have CS Degree. Unfortunately I need something with clear examples of code used and output.

I’ve played with the template Editor in HA, played with the variables to change the output.

I guess I’m able to make the leap on how to use the as_timestamp. Do I have to setup another sensor?

It depends on what you’re trying to achieve. That wasn’t completely clear. If you just want to change the existing template sensor, then maybe:

  - platform: template
    sensors:
      collection_date:
        value_template: >
          {{ as_timestamp(state_attr('calendar.waste_collection', 'start_time'))
             |timestamp_custom('%A %b %d') }}

@pnbruckner

Thank you for your help.

1 Like

This may help you out a bit: