Date convert format from sensor file

Hello, this is the first time I publish desperate, i’m could not get it to work, the correct date format from a sensor.file that extracts the date from a json string, this is my configuration.yaml:

sensor 4:
  - platform: file
    name: batch_file_inicio
    file_path: Batch.json
    value_template: "{{ value_json.Depuracion_Rollbacks.Date_debut.Fecha | timestamp_custom('%d/%m/%Y') }}"
sensor 5:
  - platform: file
    name: batch_file_fin
    file_path: Batch.json
    value_template: >
        {% set date = states('sensor.batch_file_inicio') %}
        {{ as_timestamp(strptime(date, '%d/%m/%Y')) | timestamp_custom('%d/%m/%Y') }}
        {% if date == states('sensor.date') %} Batch 1
        {% else %} Batch 2
        {% endif %}

I have used other methods, but still have not succeeded. What is the most practical way to solve it?

The result of the above code gives me always: Batch 2

On my system, sensor.date reports in ISO format: YYYY-MM-DD

Does sensor.batch_file_inicio report its date in the same format?

Note:
Have you defined sensor.date in configuration.yaml?

Thanks Taras. I’m not sure if sensor.batch_file_inicio have any date format.
this is a small screenshot.
Capture

The screenshot explains why your template always reports Batch 2.

This line in the template can never be true

{% if date == states('sensor.date') %}

because the date format of sensor.batch_file_inicio is DD/MM/YY which is not the same as the date format of sensor.date which is YYYY-MM-DD.

You will have convert one of the two dates to a common format before comparing them.

Try this:

sensor 5:
  - platform: file
    name: batch_file_fin
    file_path: Batch.json
    value_template: >
        {% set today = now().timestamp() | timestamp_custom('%d/%m/%y') %}
        {% if states('sensor.batch_file_inicio') == today %} Batch 1
        {% else %} Batch 2
        {% endif %}

Taras that was great, now i have a batch 1. You make this very simple.
Many thanks.

1 Like