Changing timestamp

I have a sensor populated by

value_template: '{{ value_json.data[1].Date }}'

This comes back in the standard format

2020-02-04T07:00:00

Is there a way on the same value_template line line to change the same sensor to just 2020-02–04? I have searched around and the only way I can find a way to create another separate sensor.

Thanks

value_template: "{{ as_timestamp('value_json.data[1].Date') | timestamp_custom('%m/%d/%Y')"

This should do it. If you are interested, I am keeping a list of date and time related template code on this page on my blog for just such an occasion.

You can have any format with these temlates, just change the format in timestamp_custom("%Y-%m-%d"). By that I mean, you could have it say “Monday, September 24, 2020” with timestamp_custom("%A, %B %d, %Y"). https://strftime.org/

  - platform: rest
    name: Black Bin
    resource: https://www.peterborough.gov.uk/api/jobs/2020-1-01/2020-12-31/100090181402
    value_template: >
      {% set current = value_json.jobs_FeatureScheduleDates[0].nextDate %}
      {{ strptime(current, '%Y-%m-%dT%H:%M:%S') | timestamp_custom("%Y-%m-%d") }}

  - platform: rest
    name: Green Bin
    resource: https://www.peterborough.gov.uk/api/jobs/2020-1-01/2020-12-31/100090181402
    value_template: >
      {% set current = value_json.jobs_FeatureScheduleDates[1].nextDate %}
      {{ strptime(current, '%Y-%m-%dT%H:%M:%S') | timestamp_custom("%Y-%m-%d") }}

or

  - platform: rest
    name: bindata
    json_attributes:
      - jobs_FeatureScheduleDates
    resource: https://www.peterborough.gov.uk/api/jobs/2020-1-01/2020-12-31/100090181402
    value_template: 'OK'
  - platform: template
    sensors:
      black_bin:
        friendly_name: Black Bin
        value_template: >
          {% set current = state_attr('sensor.bindata','jobs_FeatureScheduleDates')[0].nextDate %}
          {{ strptime(current, '%Y-%m-%dT%H:%M:%S') | timestamp_custom("%Y-%m-%d") }}
      green_bin:
        friendly_name: Green Bin
        value_template: >
          {% set current = state_attr('sensor.bindata','jobs_FeatureScheduleDates')[0].nextDate %}
          {{ strptime(current, '%Y-%m-%dT%H:%M:%S') | timestamp_custom("%Y-%M-%d") }}

Easier but no flexabilty way

But you could also be lazy and just separate the string because you aren’t changing the order of the %Y-%m-%d

  - platform: rest
    name: Black Bin
    resource: https://www.peterborough.gov.uk/api/jobs/2020-1-01/2020-12-31/100090181402
    value_template: '{{ value_json.jobs_FeatureScheduleDates[0].nextDate.split('T')[0] }}'

  - platform: rest
    name: Green Bin
    resource: https://www.peterborough.gov.uk/api/jobs/2020-1-01/2020-12-31/100090181402
    value_template: '{{ value_json.jobs_FeatureScheduleDates[1].nextDate.split('T')[0] }}'

Or if you want a single rest sensor and 2 template sensors:

  - platform: rest
    name: bindata
    json_attributes:
      - jobs_FeatureScheduleDates
    resource: https://www.peterborough.gov.uk/api/jobs/2020-1-01/2020-12-31/100090181402
    value_template: 'OK'
  - platform: template
    sensors:
      black_bin:
        friendly_name: Black Bin
        value_template: "{{ state_attr('sensor.bindata','jobs_FeatureScheduleDates')[0].nextDate.split('T')[0] }}"
      green_bin:
        friendly_name: Green Bin
        value_template: "{{ state_attr('sensor.bindata','jobs_FeatureScheduleDates')[1].nextDate.split('T')[0] }}"

Awesome thanks again @petro

Sorry to revisit this, but the output format is still incorrect. I am using this code to keep the timestamp format

- platform: rest
    name: Black Bin
    resource: https://www.peterborough.gov.uk/api/jobs/2020-1-01/2020-12-31/100090181402
    value_template: >
      {% set current = value_json.jobs_FeatureScheduleDates[0].nextDate %}
      {{ strptime(current, '%Y-%m-%dT%H:%M:%S') | timestamp_custom("%Y-%m-%d") }}

However, this converts 2020-02-04T07:00:00 to 2020-02-04 07:00:00

I don’t understand why the time is still there. What have I done wrong? Thanks again.

sorry, probably another typo on my part. Just use this:

      {% set current = value_json.jobs_FeatureScheduleDates[0].nextDate %}
      {{ as_timestamp(current) | timestamp_custom("%Y-%m-%d") }}
1 Like

Many thanks, that’s it