Template to calculate yesterday from now

I need a value that is an ISO formatted date that is 1 day before today. I have tried the below template, that returns an empty string:

{{ as_timestamp(now()) - (24*3600) | timestamp_custom('%Y-%m-%d', True) }}

That would return 2017-03-27 if today is 28 March.

What is the correct syntax?

Youn need to add parentheses around the first part of the expression …
{{ (as_timestamp(now()) - (24*3600)) | timestamp_custom(’%Y-%m-%d’, True) }}

3 Likes

Hello,
I want to add the number of day in my ‘input_datetime.dernier_arrosager’’
I take your solution and make this code:

  action:
  - service: input_datetime.set_datetime
    data_template:
      entity_id: input_datetime.dernier_arrosage
      date: '{{ (as_timestamp(now()) + (states(''input_number.intervalle_arrosage'')*24*3600)) | timestamp_custom("%Y-%m-%d", True) }}'

but it don’t work.
I am stock on this for month now.
Please help :pleading_face:

All states are strings, so if you want to add it to a number, you need to convert it to a number using |float

      date: '{{ (as_timestamp(now()) + (states(''input_number.intervalle_arrosage'')|float*24*3600)) | timestamp_custom("%Y-%m-%d", True) }}'
1 Like

Thanks it work great.
:partying_face: :partying_face: :partying_face:

Simply
{{ now().date() - timedelta(days=1) }}

8 Likes