Help with new (default) templating in 2021.10

I’ve searched a lot and tried whatever I could think of but still get the warning on a missing default for a time. It is super annoying that it doesn’t tell me which one as I have multiple, but since it is one warning, and only one of my templates is different from the others, I am guessing it is the last one… but why, I have no idea.

I reviewed the Jinja documentation but I have a lot more learning to do before I understand it…

These are my templates. Can someone kindly help me understand what I did wrong? I copied them from examples I found so and they appear to work but I have no idea whether they are correct.

I believe the “False, 0” part was what made most of the missing default warnings go away, but one is left… Do I have to add a default after “states.sensor.date.state” in the last one? Just guessing…

  - sensor:
      - name: "Scheduled Alarm Kids"
        icon: "hass:clock-outline"
        state: "{{ state_attr('input_datetime.scheduled_alarm_kids', 'timestamp') | timestamp_custom('%H:%M', False, 0) }}"


  - sensor:
      - name: "People Wake-Up Time"
        icon: "hass:clock-outline"
        state: "{{ state_attr('input_datetime.next_wake_up_alarm_people', 'timestamp') | timestamp_custom('%H:%M', False, 0) }}"


  - sensor:
      - name: "Adjusted Alarm Kids"
        icon: "hass:clock-end"
        state: "{{ state_attr('input_datetime.adjusted_alarm_kids', 'timestamp') | timestamp_custom('%H:%M', False, 0) }}"


  - sensor:
      - name: "Adjusted People Wake-Up Time"
        icon: "hass:clock-end"
        state: "{{ state_attr('input_datetime.adjusted_people_wake_up_time', 'timestamp') | timestamp_custom('%H:%M', False, 0) }}"

  - sensor:
      - name: "Expanded Date"
        state: "{{ as_timestamp(states.sensor.date.state) | timestamp_custom('%A - %d %B, %Y', False, 0) }}"
        icon: "mdi:calendar"

Edit: This is the warning:

2022-02-08 09:03:46 WARNING (MainThread) [homeassistant.helpers.template] Template warning: 'timestamp_custom' got invalid input '00:00' when compiling template '{{ '00:00' | timestamp_custom('%H:%M') }}' but no default was specified. Currently 'timestamp_custom' will return '00:00', however this template will fail to render in Home Assistant core 2022.1

Edit2: These templates are part of a Node Red based automation where I went through hell trying to get node red to ignore the date part of the alarm. In some places I managed but in some others it still shows me a date too. It is quite possible that I am also not handling the value type (string/date/time/whatever) which may be what it is complaining about in the first part of the warning. IS that the case? If so I need to go hunt down that issue too.

you’re looking at the wrong templates. The template in question is in your error. You should look in your config for this template:

EDIT: Also, that template will always fail render because timestamp_custom requires an int and you’re feeding it a string.

1 Like

@petro Thanks for the input. I did a search and found that it is likely due to Keymaster which I can’t modify (I can but I don’t want to break the Keymaster integration):

As for the string vs int… I suspected that. Thanks for pointing it out! I’ll try to figure out what I did wrong.

Edit: @petro Were my other templates above correct? Or did your string vs int comment apply to all of them?

I don’t understand how to make the last backup template correctly? Reading the documentation here and I don’t really understand what needs to be specified here

I tried to use these sensor options

  • {{ as_timestamp(state_attr("sensor.snapshot_backup", "last_snapshot")) | timestamp_custom("%d.%m.%Y %H:%M",format_string, default) }}'

  • {{ as_timestamp(strptime(state_attr("sensor.snapshot_backup","last_snapshot"), '%d.%m.%Y %H:%M' )) | timestamp_custom("%d.%m.%Y %H:%M") }}

  • {{ as_timestamp(state_attr('sensor.snapshot_backup', 'last_snapshot')) | timestamp_custom("%d.%m.%Y %H:%M", default) }}

I get these errors in the logs

Template variable warning: ‘default’ is undefined when rendering ‘{{ as_timestamp(state_attr(“sensor.snapshot_backup”, “last_snapshot”)) | timestamp_custom(“%d.%m.%Y %H:%M”, default) }}’

Template warning: ‘as_timestamp’ got invalid input ‘None’ when rendering template ‘{{ as_timestamp(strptime(state_attr(“sensor.snapshot_backup”,“last_snapshot”), ‘%d.%m.%Y %H:%M’ )) | timestamp_custom(“%d.%m.%Y %H:%M”) }}’ but no default was specified. Currently ‘as_timestamp’ will return ‘None’, however this template will fail to render in Home Assistant core 2022.1

Template warning: ‘strptime’ got invalid input ‘None’ when rendering template ‘{{ as_timestamp(strptime(state_attr(“sensor.snapshot_backup”,“last_snapshot”), ‘%d.%m.%Y %H:%M’ )) | timestamp_custom(“%d.%m.%Y %H:%M”) }}’ but no default was specified. Currently ‘strptime’ will return ‘None’, however this template will fail to render in Home Assistant core 2022.1

Try


{{ as_timestamp(state_attr('sensor.snapshot_backup', 'last_snapshot'), now() )  |timestamp_custom('%d.%m.%Y, %R', true, now() ) }}

now()

is representing the timestamp default value.

1 Like

Thank you very much. Works. :smiley: :partying_face:

Tell me, how can I do it correctly so that there is no warning below?

Template variable warning: ‘default’ is undefined when rendering '{{ is_state(“light.yeelight_ceiling4_0x111111”, “off”) and (now() - states.light.yeelight_ceiling4_0x111111.last_changed).seconds > 5 }}

Template warning: ‘strptime’ got invalid input ‘None’ when rendering template '{{ is_state(“light.yeelight_ceiling4_0x111111”, “off”) and (now() - states.light.yeelight_ceiling4_0x111111.last_changed).seconds > 5 }}

{{ is_state("light.yeelight_ceiling4_0x111111", "off") and (now()
- states.light.yeelight_ceiling4_0x111111.last_changed ).seconds > 300 | int(default=0) }}


{{ is_state("light.yeelight_ceiling4_0x111111", "off") and (now()
- states.light.yeelight_ceiling4_0x111111.last_changed ).seconds > 300 | default('', true) }}


{{ is_state("light.yeelight_ceiling4_0x111111", "off") and (now()
- states.light.yeelight_ceiling4_0x111111.last_changed ).seconds > 300 | default(0) }}

Hi! I keep getting this error
ValueError: Template error: strptime got invalid input '2022-05-29 08:30:00' when rendering template
with this template:

    sensors:
      cgm_sensor_utloper_om:
        friendly_name: "G6 Sensor utløper om: "
        value_template: >
          {% set days =  (( as_timestamp(strptime(states.input_datetime.cgm_sensor_start.state, "%d-%m-%YT%H:%M:%S")) + 864000 - as_timestamp(now()) )/ (3600*24)) | round(0) | int %}
          {%- if days < 1 -%}
          I DAG!
          {%- else -%}
          {%- if days < 2 -%}
          I morgen...
          {%- endif -%}
          {%- if days > 1 -%}
          {{ days }} Dager
          {%- endif -%}
          {%- endif -%}
        icon_template: >
          mdi:leak-off 

Can someone help with what i need to change?

You need to change this: %d-%m-%YT%H:%M:%S

The sensor’s value is 2022-05-29 08:30:00 which is year-month-day hours:minutes: seconds.

However the strptime pattern you are using is this: %d-%m-%YT%H:%M:%S which is for converting a datetime in a very different format. You’ll need to modify it.

1 Like

Yes. As Taras says. Change “%d-%m-%YT%H:%M:%S” to “%Y-%m-%d %H:%M:%S”

1 Like