Help with new (default) templating in 2021.10

What is the goal and which functions have you tried to achieve your goal?

the goal is to get the alarm in minutes so that I can trigger an automation (for the future of course). I am getting the alarm in minutes from my previous template and your modified one which works fine. My only concern was the build up of errors in my log.

I tried to use states(... instead of is_state but it doesnt work…

If that’s the goal then you are going about it the wrong way. However, this discussion is now completely off-topic because it no longer has anything to do with the recent changes concerning default values.

you asked for the goal mate but thanks for your help :slight_smile:

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] }}