Strftime doesn't seem to working properly when used in a automation variable

I get the following error in my automation

UndefinedError: 'str object' has no attribute 'strftime'

alias: TEST
description: ""
triggers: []
conditions: []
actions:
  - variables:
      varA: "{{ now()  }}"
      varB: "{{ varA.strftime('%Y-%m-%d') }}"
mode: single

But the following works in Jinja…

{% set varA = now() %}
{% set varB =  varA.strftime('%Y-%m-%d')   %}

{{ varB }}

It’s because your varA in Jinja is a DateTime object, which does have a strftime method, whereas your automation script sets it to a string, which does not have the method. I haven’t done much work with variables, but I’m guessing the type will always be a string, in which case you’d have to convert it back to a DateTime. So the equivalent in Jinja would be:

{% set varA = now() | string %}
{% set varB = (varA | as_datetime).strftime('%Y-%m-%d') %}
{{ varB }}

Yes, it seem to be correct. Thanks

You could also use

varB: "{{ varA | as_timestamp | timestamp_custom('%Y-%m-%d') }}"

Or this:

alias: TEST
description: ""
triggers: []
conditions: []
actions:
  - variables:
      varA: "{{ now()  }}"
      varB: "{{ varA[:10] }}"
mode: single