Template sensor: how to display a custom date?


I set up this code in order to set a sensor wich display a custom-formatted date value. I want the value of the code is:

weekday + day + month + year

This is the code:

      friendly_name: 'data concerto'
      value_template: >
        {% set mesi = ["gennaio ", "febbraio ", "marzo ", "aprile ", "maggio ", "giugno ", "luglio ", "agosto ", "settembre ", "ottobre ", "novembre ", "dicembre "] %}
        {% set mese = as_timestamp(states.calendar.wunderlist_tasks_and_reminders.attributes.start_time) | timestamp_custom('%m') | int -1 %}
        {% set weekgiorno = ["domenica ", "lunedì ", "martedì ", "mercoledì ", "giovedì ", "venerdì ", "sabato "] %}
        {% set giorno = as_timestamp(states.calendar.wunderlist_tasks_and_reminders.attributes.start_time) | timestamp_custom('%w',false) | int %}
        {% set numerogiorno = as_timestamp(states.calendar.wunderlist_tasks_and_reminders.attributes.start_time) | timestamp_custom('%d') | int %}
        {% set numeroanno = as_timestamp(states.calendar.wunderlist_tasks_and_reminders.attributes.start_time) | timestamp_custom('%Y') | int %}
        {{weekgiorno[giorno] + numerogiorno + mesi[mese] + numeroanno}}

Unfortunatelly the sensor give me just a “unknown” value.

BUT If I just wrote:








it display the correct value!!

So I think that just the last line is not good :pleading_face:

Any advice?
Thanks a lot

I think it isn’t like joining a string. You better try to print the definitions in a separated manner in a single line.

{{weekgiorno[giorno]}} {{numerogiorno}} {{mesi[mese]}} {{numeroanno}}

I didn’t see string concatenation on the tutorials.

1 Like

Thank you very much!
it works
You saved my day :grinning::+1:

Non c’è di che, ho tirato ad indovinare :stuck_out_tongue:
Nothing at all, I just guessed

1 Like

To concatenate strings you use a tilde ~ as opposed to a plus sign +. The plus sign can work but in certain situations it may be misinterpreted as addition. The preference is to use tilde.

{{weekgiorno[giorno] ~ numerogiorno ~ mesi[mese] ~ numeroanno}}

From Jinja2’s documentation:

Adds two objects together. Usually the objects are numbers, but if both are strings or lists, you can concatenate them this way. This, however, is not the preferred way to concatenate strings! For string concatenation, have a look-see at the ~ operator. {{ 1 + 1 }} is 2 .

Converts all operands into strings and concatenates them.
{{ "Hello " ~ name ~ "!" }} would return (assuming name is set to ‘John’)
Hello John!.


I’m curious to know if the start_time attribute’s value is a datetime object or a string.

Can you paste this into the Template Editor and let me know what it reports?

{% set st = state_attr('calendar.wunderlist_tasks_and_reminders', 'start_time') %}

String?: {{ st is string }}
Start Time: {{ st }}
Year: {{ st.year }}
Month: {{ st.month }}
Day: {{ st.day }}
Weekday: {{ st.isoweekday() }}


Error rendering template: UndefinedError: ‘str object’ has no attribute ‘isoweekday’

What does it mean?

it means that it’s a string and not a date time object. Pretty much tells @123 what he wants to know.

Thanks for carrying out the test. If it had been a datetime object (as opposed to a string), it might have presented an opportunity to simplify the template.