jazzyisj/unavailable-entities-sensor

TypeError: argument 1 must be str, not float

Closed this issue · 6 comments

My (unchanged) package is not working. Doing some debug I saw that this statement does not work

              {% set ignore_seconds = 60 %}
              {% set ignore_ts = (now().timestamp() - ignore_seconds)|as_datetime %}

Looks like as_datetime wants a string, at least for me!

TypeError: argument 1 must be str, not float

If you installed the package and have not changed anything at all in it you shouldn't be getting an error. I just downloaded a fresh copy from github and put it my config to check just in case I introduced an error with my last update. I'm not seeing this error.

I also don't think that statement can be the problem. The filter as_datetime wants an integer (or string that represents an integer) or a unix timestamp which that statement satisfies.

Can you copy this into the dev tools template? What is the result?

{% set ignore_seconds = 60 %}
{% set ignore_ts = (now().timestamp() - ignore_seconds)|as_datetime %}
{{ ignore_ts }}

Here's what I get.

image

Can you post the entire error from your log and we'll see if we can figure out what is going on?

The snippet you suggested give the same error - the second statement can't be executed and the template fails.
Can you show what value
{{ now().timestamp() }}
shows for you? For me it returns a float, which is strange
Screenshot 2022-01-21 at 09 49 35
.

I am on Home Assistant 2021.11.3

The timestamp() function should be returning a float. That's what a timestamp is. Maybe you are confusing a timestamp and a datetime object?

I am on Home Assistant 2021.11.3

Ah. This is your issue. Unix timestamp support for as_datetime was just added until v2021.12. I actually forgot the just added that capability. Guess I should put a note in the README. I apologize for any frustration.

Either update tp 2021.12 if you need the ignore seconds feature or change the template definition to this. (Removes the ignore seconds feature)

            {% if state_attr('group.ignored_unavailable_entities','entity_id') != none %}
              {{ states
                |rejectattr('domain','eq','group')
                |rejectattr('entity_id','in',state_attr('group.ignored_unavailable_entities','entity_id'))
                |selectattr('state','in',['unavailable','unknown','none'])|map(attribute='entity_id')|list }}
            {% endif %}

The timestamp() function should be returning a float. That's what a timestamp is. Maybe you are confusing a timestamp and a datetime object?
Really? That's interesting. Unix timestamps have been defined as ints since the beginning of time ;-)
https://www.epochconverter.com/

I am on Home Assistant 2021.11.3

Ah. This is your issue. Unix timestamp support for as_datetime was just added in v2021.12. I actually forgot the just added that capability. Guess I should put a note in the README. I apologize for any frustration.
I'll upgrade. Good idea about the README.
Thanks for the support.

Unix timestamps have been defined as ints since the beginning of time

Not when they support microseconds. But if that pun was intended it was pretty clever! :-)

image