gekomax
(Gekomax)
July 4, 2019, 2:10pm
1
Hi,
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:
concerti_quando_mese:
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:
{{numeroanno}}
or
{{mesi[mese]}}
or
{{weekgiorno[giorno]}}
or
{{numerogiorno}}
it display the correct value!!
So I think that just the last line is not good
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
gekomax
(Gekomax)
July 4, 2019, 2:55pm
3
Thank you very much!
it works
You saved my day
Non câè di che, ho tirato ad indovinare
Translated
Nothing at all, I just guessed
1 Like
123
(Taras)
July 4, 2019, 3:04pm
5
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!
.
2 Likes
123
(Taras)
July 4, 2019, 3:23pm
6
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() }}
petro
(Petro)
July 5, 2019, 1:19pm
8
it means that itâs a string and not a date time object. Pretty much tells @123 what he wants to know.
123
(Taras)
July 5, 2019, 1:40pm
9
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.