Help with new (default) templating in 2021.10

Because I was trying to help you but needed to know if the discussion still involved default values. It’s clearly not so I won’t be pursuing it and would appreciate if you don’t tag me again in the future. Thank you for your understanding and good luck.

1 Like

Hi @petro,
Except from the issues last night, I noticed the DST are one day too late.

next:
  spring: '2022-03-28 02:00:00+02:00'
  fall: '2021-11-01 02:00:00+02:00'
  event: '2021-11-01 02:00:00+02:00'
  days_to_event: 1
  phrase: gain an hour
friendly_name: Daylight Savings Times
device_class: timestamp

I think it should be:

spring: ‘2022-03-27 02:00:00+02:00’
fall: ‘2021-10-31 02:00:00+02:00’

Or do I miss something?

It works for me in my timezone. You have to update the template for your timezone. You have to set the correct hour that the change occurs.

EDIT: for my timezone, its 2. For your timezone, it’s 14:05? which is an odd time, anyways for it to work you need to put in a time past 14:05, i.e. 15. So previous should change from 2 to 15 in the template.

Updated the template so you don’t need to specify a time

As always, help really appreciated. :+1:

Hello guys,
Need help suppressing this warning. Tried to figure out myself using this link from above, but failing.

error:

Template warning: 'strptime' got invalid input '2021-08-27T00:13:16.555' when rendering template '{{ 91 - (( as_timestamp(now()) - as_timestamp(strptime(states.sensor.vj_ssl_cert_issued.state, "%Y-%m-%d")) )/ (3600*24)) | round(0) }}' but no default was specified. Currently 'strptime' will return '2021-08-27T00:13:16.555', however this template will fail to render in Home Assistant core 2022.1

code in question:

value_template: '{{ 91 - (( as_timestamp(now()) - as_timestamp(strptime(states.sensor.vj_ssl_cert_issued.state, "%Y-%m-%d")) )/ (3600*24)) | round(0) }}'

Se the example here: https://community.home-assistant.io/t/updating-templates-with-the-new-default-values-in-2021-10-x/346198#3-arguments

# returns time @ 10 am, if fails returns 0
{{ strptime("10:00", "%H:%M", 0) }}

Hi. Do you mean to add ,0 like so?

{{ 91 - (( as_timestamp(now()) - as_timestamp(strptime(states.sensor.vj_ssl_cert_issued.state, "%Y-%m-%d", 0)) )/ (3600*24)) | round(0) }}

I am getting state unavailable if I do.

Yes. That is correct, if you want the default to be 0.

And you are now seeing that there is an issue with your template. Where as before it was hidden from you.

Check the state of, sensor.vj_ssl_cert_issued

Also you’ve been here long enough to know this:

1 Like

If you wish, instead of this:

{{ 91 - (( as_timestamp(now()) - as_timestamp(strptime(states.sensor.vj_ssl_cert_issued.state, "%Y-%m-%d", 0)) )/ (3600*24)) | round(0) }}

you can do this:

{{ 91 - ((now() - states('sensor.vj_ssl_cert_issued'))|as_datetime|as_local) / timedelta(days=1)) | round(0) }}
1 Like

@ 123 Taras
That fixes the warning. Thx.

{{ 91 - ((now() - states("sensor.vj_ssl_cert_issued")|as_datetime|as_local) / timedelta(days=1)) | round(0) }}

@ tom_l
Thanks for the tip.

It’s possible to reduce the template further but the time difference calculation can produce a value that is up to one day shorter.

The round filter rounds up by default. In other words, if the time difference is 117 days, 20 hours, and 30 minutes, it will report 118 days.

The following method simply reports whole days only and ignores remaining hours and minutes. Effectively, it rounds down.

{{ 91 - (now() - states('sensor.vj_ssl_cert_issued')|as_datetime|as_local).days }}

Hello. After upgrading to core-2021.12.0, I am getting this error

error:

TemplateError('AttributeError: 'NoneType' object has no attribute 'tzinfo'') while processing template 'Template("{{ 91 - (now() - states("sensor.vj_ssl_cert_issued")|as_datetime|as_local).days }}")' for attribute '_attr_native_value' in entity 'sensor.vj_ssl_cert_expiry'`

code:

{{ 91 - (now() - states('sensor.vj_ssl_cert_issued')|as_datetime|as_local).days }}

It implies there’s something wrong with the sensor’s value because it can no longer be converted to a datetime object.

Yea, I was looking in the log some more and this maybe the cause.

Not sure if this warning is causing the error from this sensor: sensor.vj_ssl_cert_issued

error:

sensor.vj_ssl_cert_issued rendered timestamp without timezone: 2021-11-25T00:27:16.728

This is what the timestamp outputs as from a json file. 2021-11-25T00:27:16.728

So I found a different site that has a valid timestamp where HA will not produce error.

How can I get retrieve sub.domain.com from this sensor?

sensor.vj_cert_sslmate: State attributes

dns_names:
  - sub.domain.com # need to pull this
  - www.domain.com
not_after: '2022-02-23T23:59:59Z'
friendly_name: cert sslmate

I think I am close, but not quite there.

{{ state_attr('sensor.vj_cert_sslmate', 'dns_names') }}

I think I got it. This works.

{{ states.sensor.vj_cert_sslmate.attributes.dns_names[0] }}

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