Unsupported operand type(s) for -: 'NoneType' and 'float'

I have a template sensor that is throwing an error when it tries to update. The error is

Update for sensor.calendly_event_offset fails
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/homeassistant/helpers/entity.py", line 220, in async_update_ha_state
    await self.async_device_update()
  File "/usr/local/lib/python3.7/site-packages/homeassistant/helpers/entity.py", line 375, in async_device_update
    await self.async_update()
  File "/usr/local/lib/python3.7/site-packages/homeassistant/components/template/sensor.py", line 191, in async_update
    self._state = self._template.async_render()
  File "/usr/local/lib/python3.7/site-packages/homeassistant/helpers/template.py", line 191, in async_render
    return self._compiled.render(kwargs).strip()
  File "/usr/local/lib/python3.7/site-packages/jinja2/asyncsupport.py", line 76, in render
    return original_render(self, *args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/jinja2/environment.py", line 1008, in render
    return self.environment.handle_exception(exc_info, True)
  File "/usr/local/lib/python3.7/site-packages/jinja2/environment.py", line 780, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python3.7/site-packages/jinja2/_compat.py", line 37, in reraise
    raise value.with_traceback(tb)
  File "<template>", line 1, in top-level template code
TypeError: unsupported operand type(s) for -: 'NoneType' and 'float'

The line in my yaml file is

        {% if as_timestamp(states.calendar.nick_calendly.attributes.start_time) - as_timestamp(strptime(states.sensor.date_time.state, "%Y-%m-%d, %H:%M")) < 180 %}on{% else %}off{% endif %}

Any ideas on what I need to do to fix this? Thanks!

What have you tried? You could try simplifying your template & then add parts.

Try putting parentheses around the as_timestamp expressions to explicitly show the subtraction operation.

Thanks for the reply! So you mean like this?

{% if (as_timestamp(states.calendar.nick_calendly.attributes.start_time)) - (as_timestamp( strptime(states.sensor.date_time.state, "%Y-%m-%d, %H:%M" ) ) ) < 180  %}on{% else %}off{% endif %}

I tried that and am still receiving the same error.

The error makes it seem like maybe the start time isn’t the correct value type. I would assume that you can’t subtract a Nonetype from a float.

Paste that line into Home Assistant’s Template Editor and debug it there.

Thanks! I never realized how helpful the template page is for troubleshooting. The issue (at least for now) is that I was trying to subtract one time from another time, but one of the calendars didn’t have any events, so instead of returning a time it was returning “None”. :man_facepalming:

So I updated the template value to this:

{% if state_attr('calendar.nick_calendly', 'start_time') == None %}off{% else %}{% if (as_timestamp(states.calendar.nick_calendly.attributes.start_time)) - (as_timestamp( strptime(states.sensor.date_time.state, "%Y-%m-%d, %H:%M" ) ) ) < 180  %}on{% else %}off{% endif %}{% endif %}

I want to see how this works over the next week before I declaire this officially fixed, but I ran some tests this evening and it appears to be working now.:crossed_fingers:

I believe your template can be reduced to the following:

  value_template: >
    {% set st = state_attr('calendar.nick_calendly', 'start_time') %}
    {% if st != None %}
      {{ 'on' if as_timestamp(st) - now().timestamp() < 180 else 'off' }}
    {% else %}
      off
    {% endif %}
1 Like

If we’re shrinking it as much as we can I reckon this’ll do it…

value_template: >
  {{ 'on' if (state_attr('calendar.nick_calendly', 'start_time')
    and as_timestamp(st) - now().timestamp() < 180)
    else 'off' }}

Could wrap it in double quotes and stick it all on one line if desired.

2 Likes

Where is your template defining the variable called st?

This’ll also do the trick:

  value_template: >
    {% set st = state_attr('calendar.nick_calendly', 'start_time') %}
    {{ 'on' if st != None and as_timestamp(st) - now().timestamp() < 180 else 'off' }}
1 Like

I have the same error message but can’t see any indication which template is causing it, any ideas on how to find which one is producing this ?

2019-05-28 08:06:13 ERROR (MainThread) [homeassistant.core] Error doing job: Task exception was never retrieved

Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/homeassistant/components/automation/__init__.py", line 280, in async_trigger
    if not skip_condition and not self._cond_func(variables):
  File "/usr/local/lib/python3.7/site-packages/homeassistant/components/automation/__init__.py", line 401, in if_action
    return all(check(hass, variables) for check in checks)
  File "/usr/local/lib/python3.7/site-packages/homeassistant/components/automation/__init__.py", line 401, in <genexpr>
    return all(check(hass, variables) for check in checks)
  File "/usr/local/lib/python3.7/site-packages/homeassistant/helpers/condition.py", line 352, in template_if
    return async_template(hass, value_template, variables)
  File "/usr/local/lib/python3.7/site-packages/homeassistant/helpers/condition.py", line 331, in async_template
    value = value_template.async_render(variables)
  File "/usr/local/lib/python3.7/site-packages/homeassistant/helpers/template.py", line 191, in async_render
    return self._compiled.render(kwargs).strip()
  File "/usr/local/lib/python3.7/site-packages/jinja2/asyncsupport.py", line 76, in render
    return original_render(self, *args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/jinja2/environment.py", line 1008, in render
    return self.environment.handle_exception(exc_info, True)
  File "/usr/local/lib/python3.7/site-packages/jinja2/environment.py", line 780, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python3.7/site-packages/jinja2/_compat.py", line 37, in reraise
    raise value.with_traceback(tb)
  File "<template>", line 1, in top-level template code
TypeError: unsupported operand type(s) for -: 'float' and 'NoneType'

Haha. Thanks everyone! I’m still learning, so for me my goal is to get something working. I wasn’t thinking about optimization, but I’ll update it!

@Holdestmade - Again, for me I had two times that I was subtracting from one another, but in my case one of my values was returning “None” instead of a time since that calendar had no events on it.

To figure out which one it was I copied my entire value_template into the template editor in Home Assistant, which is down with the developer tools at the bottom of the sidebar.

Then, I edited that block removing all of the code around the two variables that I was trying to subtract from one another. So I started with:

{% if (as_timestamp(states.calendar.nick_calendly.attributes.start_time)) - (as_timestamp( strptime(states.sensor.date_time.state, "%Y-%m-%d, %H:%M" ) ) ) < 180  %}on{% else %}off{% endif %}

and ended with:

states.calendar.nick_calendly.attributes.start_time 
states.sensor.date_time.state

Then you wrap those in curly braces so that the template editor will output their values:

{{ states.calendar.nick_calendly.attributes.start_time }}
{{ states.sensor.date_time.state }}

For me, that top one (nick_calendly.attributes.start_time) was the calendar that had no events and was returning “None” instead of a time, so I added the additional logic to check for that first and return “off” if it didn’t have any events.

@123

So to get this working I had also setup a sensor called date_time that just returned the current time every minute and was using that in my calculation for this. It looks like you have replaced my need for that using now().timestamp().

I just wanted to verify that I should be able to delete that date_time sensor all together (since it was created just to get this to work) and this template value will still update itself every minute.

Thanks!

Thanks for the reply but my first issue is finding which template is causing the error, I have many and everything seems to work OK, just getting that error in the log every now and then. I see from your log you have “Update for sensor.calendly_event_offset fails” but in mine it just says “Error doing job: Task exception was never retrieved”

I guess I could just go through all until I find it !

Oh yeah, sorry about that. I have no idea why your error doesn’t show the specific template like mine does. Good news is that you should be able to narrow it down to templates that have some kind of subtraction in them. ¯\_(ツ)_/¯

Found it. It was a time condition in an automation checking the last_triggered attribute and when HASSIO is restarted or just automations reloaded, the last_triggered gets set to None. Obvious now but you can’t subtract a None from a time.
Thanks for your help, I didn’t even notice the subtraction symbol in the error message, thought it was just a hyphen ! and that enabled me to look through just the subtract templates.

Not quite.

After a restart, an automation’s last_triggered attribute remains at whatever date/time it was previously triggered. If it was never triggered then the attribute will be set to null (which is represented for templating purposes as None).

In the following screenshot, ‘hyphen_tester’ was triggered yesterday. When Home Assistant was restarted today, the automation’s last_triggered displays yesterday’s date/time.

In contrast, ‘boolean_tester’ is an automation I used a long time ago then commented it out in the automation file. Today I uncommented the code, restarted Home Assistant and the automation’s last_triggered is null.

Just re-tried this and all my automations do return to null on a restart or reloading of automations.

I had the automations domain excluded from recorder.yaml, thought that may be why but makes no difference if I include it.

That’s curious. Which version of Home Assistant are you using?

I have 0.91 on my test system (docker installation) and the automation domain is excluded from recorder.

Screenshot%20from%202019-05-29%2008-13-16

Didn’t even notice this before, maybe I should make a new post ?

Copied from info page:

|arch|x86_64|
|dev|false|
|docker|true|
|hassio|true|
|os_name|Linux|
|python_version|3.7.3|
|timezone|Europe/London|
|version|0.93.1|
|virtualenv|false|

Seems I am not the only one, lots of posts on here, all say they are reset on restart

Wonder how yours aren’t ?

The only substantive difference from your configuration is neither of my two systems (production and test) use hass.io.

Production:

arch i686
dev false
docker false
hassio false
os_name Linux
python_version 3.6.7
timezone America/redacted
version 0.89.1
virtualenv true

Test:

arch x86_64
dev false
docker true
hassio false
os_name Linux
python_version 3.7.3
timezone America/redacted
version 0.91.0
virtualenv false

I have many automations on my test system. I create them while helping solve problems for community members, then I comment them out in the automations.yaml file. Here are three automations on my test system. Two of them were previously commented out and were uncommented for this experiment (then the test system was restarted). Those two appear with last_triggered = null.

I triggered the two automations (not manually from the Services page but via their defined triggers) and the States page shows their updated last_triggered attribute.

Then I threw the kitchen sink at it:

  • I restarted Home Assistant (from the Configuration menu)
  • Reloaded automations
  • Stopped/restarted the docker container (thereby restarting Home Assistant again)

The end-result was all automations retained their last_triggered values.

As per the scientific method, the existence of black swans disproves the theory that all swans are white. An automation’s last_triggered attribute can survive a restart/reload.

Now is it because both systems aren’t hass.io or is it due to something else?