Problem with date from JSON

Hello everyone,

I use the following rest sensor to get a date from a JSON

  - platform: rest
    name: letzte_Leerung
    scan_interval: 3600
    json_attributes_path: '$.data[0].abholungen[-1:]'
    json_attributes:
      - leerungsdatum
    resource: https://abholung-api.art-trier.de/api/abholung/kunde/XXXXXXX?objektnummer=YYYYYY
    value_template: "{{ value_json.leerungsdatum}}"

and get the result:

2022-09-09 11:38:01 as an attribute of a sensor

I’d like to have the date in the format dd.mm.YYYY but I can’t get it to work.

Could anybody please help me?

Thanks a lot
Thorsten

Perhaps the ideas from aces that answered these two posts will give you some ideas:

1 Like

Good morning,

I’m a step further. I did in two steps now:

  - platform: rest
    name: datum_leerung
    scan_interval: 3600
    json_attributes_path: '$.data[0].abholungen[-1:]'
    json_attributes:
      - leerungsdatum
    resource: https://abholung-api.art-trier.de/api/abholung/kunde/XXXXXXX?objektnummer=YYYYYY
    value_template: "{{ value_json.leerungsdatum}}"


  - platform: template  
    sensors:
      letzte_leerung:
        value_template: "{{as_timestamp(state_attr('sensor.datum_leerung', 'leerungsdatum')) | timestamp_custom('%d.%m.%Y') }}"

This works as expected but I think there must be a more elegant way to do it. If anybody has got an idea. I’m still open for new ways.

Thanks in advance

1 Like

You can’t do it in one step because the REST Sensor doesn’t support templates in json_attributes

json_attributes:
      - leerungsdatum

Even the documentation shows that a Template Sensor must be used if you want to modify the value of a REST Sensor’s attribute.

An alternative is to modify the appearance of the REST Sensor’s state value because it supports a template.

    value_template: "{{ value_json.leerungsdatum | as_timestamp | timestamp_custom('%d.%m.%Y') }}"
3 Likes

Thank you, that was exactly what I was looking for.

For me the explanation you gave was in one step.

Thanks a lot

1 Like