Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Template entity with device_class timestamp displays as 'Unknown' following 2021.12 update #61673

Closed
robertwigley opened this issue Dec 13, 2021 · 20 comments

Comments

@robertwigley
Copy link

robertwigley commented Dec 13, 2021

The problem

Following the update to 2021.12 template entities with a device_class of timestamp show as unknown. I have a ton of these for converting input_datetime helpers into read-only sensors for display in Lovelace and they all now display as Unknown:

image

template:
# Template Sensors
  - sensor:
  # Front Door Lock sensors
      # Front Door Lock Last Activity sensor
      - name: Front Door Lock Last Activity
        device_class: timestamp
        icon: mdi:history
        state: "{{ states('input_datetime.front_door_lock_last_activity') }}"

What version of Home Assistant Core has the issue?

2021.12

What was the last working version of Home Assistant Core?

2021.11.5

What type of installation are you running?

Home Assistant OS

Integration causing the issue

No response

Link to integration documentation on our website

No response

Example YAML snippet

template:
# Template Sensors
  - sensor:
  # Front Door Lock sensors
      # Front Door Lock Last Activity sensor
      - name: Front Door Lock Last Activity
        device_class: timestamp
        icon: mdi:history
        state: "{{ states('input_datetime.front_door_lock_last_activity') }}"

Anything in the logs that might be useful for us?

No response

Additional information

Potentially related: https://community.home-assistant.io/t/sunset-sunrise-sensor-stopped-working-while-template-does/366287

@robertwigley robertwigley changed the title Issues with device_class timestamp following 2021.12 update device_class timestamp displays as 'Unknown' following 2021.12 update Dec 13, 2021
@martusi61
Copy link

Same problem with all my timestamp sensors, only from 2021.12.0

@robertwigley robertwigley changed the title device_class timestamp displays as 'Unknown' following 2021.12 update Template entity with device_class timestamp displays as 'Unknown' following 2021.12 update Dec 13, 2021
@Petro31
Copy link
Contributor

Petro31 commented Dec 13, 2021

@martusi61 @robertwigley You need your template to output a datetime object or a timestamp integer. Strings no longer work.

@Petro31
Copy link
Contributor

Petro31 commented Dec 13, 2021

There should be an error or warning in your logs mentioning this.

@robertwigley
Copy link
Author

I already tried adding the as_timestamp filter and also as a function. This produces an "Entity not available" error:

image

@Petro31
Copy link
Contributor

Petro31 commented Dec 13, 2021

Please check your logs, not the entity in the frontend. Thanks.

@martusi61
Copy link

martusi61 commented Dec 13, 2021

Sorry. It's true:
2021-12-13 08:16:02 ERROR (MainThread) [homeassistant.components.template.template_entity] TemplateError('TypeError: unsupported operand type(s) for -: 'NoneType' and 'float'') while processing template 'Template("{{ ((as_timestamp(states.sensor.cert_expiry_timestamp_xxxxxx_xxxxx_org.state) - as_timestamp(utcnow()) ) / 86400) | round }}")' for attribute '_attr_native_value' in entity 'sensor.scadenza_ssl_hassio'

@Petro31
Copy link
Contributor

Petro31 commented Dec 13, 2021

@martusi61 you have sensitive data in that post, remove it.

Fix your template with

{{ ((as_timestamp(states('sensor.xyz'), 0) - as_timestamp(0)) / 86400) | round }}

However you probably want to remove the timestamp device_class because it won't have an accurate time. What are you trying to display? The last cert refresh value?

@robertwigley
Copy link
Author

The only related entry I can see is this, which was not showing before the update to 2021.12:

2021-12-13 10:29:27 WARNING (MainThread) [homeassistant.components.sensor.helpers] sensor.front_door_lock_last_activity rendered timestamp without timezone: 2021-12-12 15:40:15

Is this something to do with the change to timestamp_local?

@Petro31
Copy link
Contributor

Petro31 commented Dec 13, 2021

The only related entry I can see is this, which was not showing before the update to 2021.12:

2021-12-13 10:29:27 WARNING (MainThread) [homeassistant.components.sensor.helpers] sensor.front_door_lock_last_activity rendered timestamp without timezone: 2021-12-12 15:40:15

Is this something to do with the change to timestamp_local?

no. If that's a timestamp device, it needs a TZ.


        state: "{{ states('input_datetime.front_door_lock_last_activity') | as_datetime | as_local }}"

@robertwigley
Copy link
Author

state: "{{ states('input_datetime.front_door_lock_last_activity') | as_datetime | as_local }}"

Resolves the issue. Thank you! There was no mention of this in the breaking changes, not that I could see anyway.

@Petro31
Copy link
Contributor

Petro31 commented Dec 13, 2021

It is mentioned, but you probably didn't see/understand it. It's this Breaking change here:

Timestamp/Date device class value changes
When using datetime and date device classes in sensors, the returned native value must (respectively) be a datetime or date Python object.

Returning an iso formatted date(time) string in these cases is now deprecated and will write a deprecation warning in the logs. This fallback/backward compatibility will be removed in Home Assistant 2022.2.

(@frenck - #52671)

@Petro31
Copy link
Contributor

Petro31 commented Dec 13, 2021

Can this be closed then?

@robertwigley
Copy link
Author

Thanks for the link. Maybe I am being blind, but I still can't find/see it in the Release Notes.

@robertwigley
Copy link
Author

I have found it. It's under "Updates for custom integration developers". No wonder I never saw it. This should be in the main breaking changes. Yes, this can be closed. Thanks for your assistance.

@martusi61
Copy link

@martusi61 you have sensitive data in that post, remove it.

Fix your template with

{{ ((as_timestamp(states('sensor.xyz'), 0) - as_timestamp(0)) / 86400) | round }}

However you probably want to remove the timestamp device_class because it won't have an accurate time. What are you trying to display? The last cert refresh value?

I needed the difference in days between a timestamp and today's date, however your suggestion brought up the sensor, but as "unknow", I followed your advice and it worked by removing the "device_class: timestamp", in fact the template returns a number of days not another timestamp. Thanks for everything, now is OK.

@Petro31
Copy link
Contributor

Petro31 commented Dec 13, 2021

@martusi61 you have sensitive data in that post, remove it.
Fix your template with

{{ ((as_timestamp(states('sensor.xyz'), 0) - as_timestamp(0)) / 86400) | round }}

However you probably want to remove the timestamp device_class because it won't have an accurate time. What are you trying to display? The last cert refresh value?

I needed the difference in days between a timestamp and today's date, however your suggestion brought up the sensor, but as "unknow", I followed your advice and it worked by removing the "device_class: timestamp", in fact the template returns a number of days not another timestamp. Thanks for everything, now is OK.

Just remove the timestamp as a device class then. No need for it if you want the number of days.

@martusi61
Copy link

@martusi61 you have sensitive data in that post, remove it.
Fix your template with

{{ ((as_timestamp(states('sensor.xyz'), 0) - as_timestamp(0)) / 86400) | round }}

However you probably want to remove the timestamp device_class because it won't have an accurate time. What are you trying to display? The last cert refresh value?

I needed the difference in days between a timestamp and today's date, however your suggestion brought up the sensor, but as "unknow", I followed your advice and it worked by removing the "device_class: timestamp", in fact the template returns a number of days not another timestamp. Thanks for everything, now is OK.

Just remove the timestamp as a device class then. No need for it if you want the number of days.

Exac

@arifroni
Copy link

arifroni commented Jan 11, 2022

i have a uptime sensor setup like this

- unique_id: intel_nuc5i3_uptime
      device_class: timestamp
      state:
        "{% set d, h, m, s = states('sensor.intel_nuc5i3_system_uptime').split(':')  | map('int') %}
        {{ as_datetime(states('sensor.date_time_iso')) - timedelta(days=d, hours=h, minutes=m, seconds=s) }}"

but i get following error
sensor.template_intel_nuc5i3_uptime rendered timestamp without timezone: 2021-12-30 08:28:14
how can i fix this?

@robertwigley
Copy link
Author

but i get following error sensor.template_intel_nuc5i3_uptime rendered timestamp without timezone: 2021-12-30 08:28:14 how can i fix this?

It looks to be the same as the error I was getting. Try adding | as_local to the end to give it your local timezone.

@arifroni
Copy link

It looks to be the same as the error I was getting. Try adding | as_local to the end to give it your local timezone.

I get following error
AttributeError: 'datetime.timedelta' object has no attribute 'tzinfo'

@github-actions github-actions bot locked and limited conversation to collaborators Feb 12, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants