Template error: TypeError: 'int' object is not callable

I’m putting together a template sensor that will tell me what bin needs to go out each week. Here’s the template I’ve got so far:

{% if (now().isocalendar().week % 2 == 0) and (now().isoweekday() == 3) and (now().hour() == 15) %}
Green Waste
{% elif (now().isocalendar().week % 2 == 1) and (now().isoweekday() == 3) and (now().hour() == 15) %}
Recycling
{% else %}
No Bin
{% endif %}

So basically if the week is an even week AND the day of the week is 3 (Wednesday) AND the hour is 3pm (15), then display “Green Waste”. If the week is an odd week, display “Recycling”. Otherwise, display “No Bin”

This works perfectly fine, but if I change it to this:

{% if (now().isocalendar().week % 2 == 0) and (now().isoweekday() == 4) and (now().hour() == 15) %}
Green Waste
{% elif (now().isocalendar().week % 2 == 1) and (now().isoweekday() == 4) and (now().hour() == 15) %}
Recycling
{% else %}
No Bin
{% endif %}

I get this error:

TypeError: 'int' object is not callable

The ONLY difference between the two, is that I’ve changed 3 (Wednesday) to 4 (Thursday)

What’s the go here? Is it because today is Thursday and that’s doing something weird?

Use now().hour not now().hour()

You can reduce it to this:

{% if now().isoweekday() == 4 and now().hour == 15 %}
{{ 'Green Waste' if now().isocalendar().week % 2 == 0 else 'Recycling' }}
{% else %}
No Bin
{% endif %}
1 Like

Yep, that fixed the issue, thanks!

But I’m still not sure why everything would grind to a halt by changing a 3 to a 4. Only thing I can think of is today being day 4 and that causing some issue with now().isoweekday(), and that I’d have issues with changing it to a 5 if I ran it tomorrow

AND are smart in so that as soon as one of the conditions are not met, it doesn’t consider the other ones, from left to right.
So if now().isoweekday() == 4 isn’t true, the hour part is not interpreted.

Today being actually “4”, the hour part is interpreted and fails for the reasons exposed by Taras.

1 Like

That makes perfect sense now. It’s also a good lesson for me to read the documentation, because I just assumed that if I could use now().isoweekday() but not now().isoweekday, then I should use now().hour() and not now().hour

  • now() returns a datetime object representing the current date and time.

  • isoweekday() is one of the object’s methods. It has several methods including isoformat(), weekday(), isocalendar(), etc.

  • hour is one of the object’s properties. It has several properties including year, month, etc.

There’s no method called hour() and there’s no property called isoweekday.

https://docs.python.org/3/library/datetime.html

1 Like