Awkward error on startup

Hi,

I’ve been using HA for about two weeks now and I love it :slight_smile:

But I now realized this error on startup. Any clues appreciated. Where to look for the source etc.?

17-01-08 20:39:03 homeassistant.core: Error doing job: Task exception was never retrieved
Traceback (most recent call last):
  File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/homeassistant/helpers/template.py", line 99, in async_render
    return self._compiled.render(kwargs).strip()
  File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/jinja2/environment.py", line 989, in render
    return self.environment.handle_exception(exc_info, True)
  File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/jinja2/environment.py", line 754, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/jinja2/_compat.py", line 37, in reraise
    raise value.with_traceback(tb)
  File "<template>", line 1, in top-level template code
  File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/jinja2/sandbox.py", line 392, in getattr
    value = getattr(obj, attribute)
jinja2.exceptions.UndefinedError: 'sensor' is undefined

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3.4/asyncio/tasks.py", line 237, in _step
    result = next(coro)
  File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/homeassistant/components/automation/__init__.py", line 294, in async_trigger
    yield from self._async_action(self.entity_id, variables)
  File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/homeassistant/components/automation/__init__.py", line 376, in action
    yield from script_obj.async_run(variables)
  File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/homeassistant/helpers/script.py", line 115, in async_run
    yield from self._async_call_service(action, variables)
  File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/homeassistant/helpers/script.py", line 145, in _async_call_service
    self.hass, action, True, variables, validate_config=False)
  File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/homeassistant/helpers/service.py", line 88, in async_call_from_config
    config[CONF_SERVICE_DATA_TEMPLATE]))
  File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/homeassistant/helpers/service.py", line 84, in _data_template_creator
    for key, item in value.items()}
  File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/homeassistant/helpers/service.py", line 84, in <dictcomp>
    for key, item in value.items()}
  File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/homeassistant/helpers/service.py", line 86, in _data_template_creator
    return value.async_render(variables)
  File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/homeassistant/helpers/template.py", line 101, in async_render
    raise TemplateError(err)
homeassistant.exceptions.TemplateError: UndefinedError: 'sensor' is undefined

Thank you very much.

can you post your configuration.yaml file

I think I found it. I have an automation that sets a switch based on a value that I retrieve via command line. At this point in time the variable is just not set yet, right?

Jan 09 06:28:09 homeassistant hass[13694]: INFO:homeassistant.components.automation:Executing Set partymode select to value based on sensor
Jan 09 06:28:09 homeassistant hass[13694]: INFO:homeassistant.core:Bus:Handling <Event logbook_entry[L]: entity_id=automation.set_partymode_select_to_value_based_on_sensor, name=Set partymode select to value based on sensor, message=has been triggered, domain=automation>
Jan 09 06:28:09 homeassistant hass[13694]: INFO:homeassistant.helpers.script:Script Set partymode select to value based on sensor: Running script
Jan 09 06:28:09 homeassistant hass[13694]: INFO:homeassistant.helpers.script:Script Set partymode select to value based on sensor: Executing step call service
Jan 09 06:28:09 homeassistant hass[13694]: ERROR:homeassistant.core:Error doing job: Task exception was never retrieved
Jan 09 06:28:09 homeassistant hass[13694]: Traceback (most recent call last):
Jan 09 06:28:09 homeassistant hass[13694]: File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/homeassistant/helpers/template.py", line 99, in async_render
Jan 09 06:28:09 homeassistant hass[13694]: return self._compiled.render(kwargs).strip()
Jan 09 06:28:09 homeassistant hass[13694]: File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/jinja2/environment.py", line 989, in render
Jan 09 06:28:09 homeassistant hass[13694]: return self.environment.handle_exception(exc_info, True)
Jan 09 06:28:09 homeassistant hass[13694]: File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/jinja2/environment.py", line 754, in handle_exception
Jan 09 06:28:09 homeassistant hass[13694]: reraise(exc_type, exc_value, tb)
Jan 09 06:28:09 homeassistant hass[13694]: File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/jinja2/_compat.py", line 37, in reraise
Jan 09 06:28:09 homeassistant hass[13694]: raise value.with_traceback(tb)
Jan 09 06:28:09 homeassistant hass[13694]: File "<template>", line 1, in top-level template code
Jan 09 06:28:09 homeassistant hass[13694]: File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/jinja2/sandbox.py", line 392, in getattr
Jan 09 06:28:09 homeassistant hass[13694]: value = getattr(obj, attribute)
Jan 09 06:28:09 homeassistant hass[13694]: jinja2.exceptions.UndefinedError: 'sensor' is undefined
Jan 09 06:28:09 homeassistant hass[13694]: During handling of the above exception, another exception occurred:
Jan 09 06:28:09 homeassistant hass[13694]: Traceback (most recent call last):
Jan 09 06:28:09 homeassistant hass[13694]: File "/usr/lib/python3.4/asyncio/tasks.py", line 237, in _step
Jan 09 06:28:09 homeassistant hass[13694]: result = next(coro)
Jan 09 06:28:09 homeassistant hass[13694]: File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/homeassistant/components/automation/__init__.py", line 294, in async_trigger
Jan 09 06:28:09 homeassistant hass[13694]: yield from self._async_action(self.entity_id, variables)
Jan 09 06:28:09 homeassistant hass[13694]: File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/homeassistant/components/automation/__init__.py", line 376, in action
Jan 09 06:28:09 homeassistant hass[13694]: yield from script_obj.async_run(variables)
Jan 09 06:28:09 homeassistant hass[13694]: File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/homeassistant/helpers/script.py", line 115, in async_run
Jan 09 06:28:09 homeassistant hass[13694]: yield from self._async_call_service(action, variables)
Jan 09 06:28:09 homeassistant hass[13694]: File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/homeassistant/helpers/script.py", line 145, in _async_call_service
Jan 09 06:28:09 homeassistant hass[13694]: self.hass, action, True, variables, validate_config=False)
Jan 09 06:28:09 homeassistant hass[13694]: File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/homeassistant/helpers/service.py", line 88, in async_call_from_config
Jan 09 06:28:09 homeassistant hass[13694]: config[CONF_SERVICE_DATA_TEMPLATE]))
Jan 09 06:28:09 homeassistant hass[13694]: File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/homeassistant/helpers/service.py", line 84, in _data_template_creator
Jan 09 06:28:09 homeassistant hass[13694]: for key, item in value.items()}
Jan 09 06:28:09 homeassistant hass[13694]: File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/homeassistant/helpers/service.py", line 84, in <dictcomp>
Jan 09 06:28:09 homeassistant hass[13694]: for key, item in value.items()}
Jan 09 06:28:09 homeassistant hass[13694]: File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/homeassistant/helpers/service.py", line 86, in _data_template_creator
Jan 09 06:28:09 homeassistant hass[13694]: return value.async_render(variables)
Jan 09 06:28:09 homeassistant hass[13694]: File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/homeassistant/helpers/template.py", line 101, in async_render
Jan 09 06:28:09 homeassistant hass[13694]: raise TemplateError(err)
Jan 09 06:28:09 homeassistant hass[13694]: homeassistant.exceptions.TemplateError: UndefinedError: 'sensor' is undefined

Configuration.yaml is a bit of work, will post it later.

Without the source it’s a little hard to tell, but something named sensor seems to be undefined.

This is because your template is testing the sensor value before the sensor is initialized. Basically, HA is trying to read the value of the sensor before the sensor entity actually exists.

This is not a problem as it gets fixed the next round of polling. But if it bothers you, you just have to wrap your template in an “if” that checks for the existence of the entity first as explained by @ih8gates here:

1 Like

Thanks. But how would I do that in this scenario?

alias: "Set partymode boolean to on based on sensor"
  trigger:
    platform: state
    entity_id: sensor.party_mode_status
    to: 'on'
  action:
    service: input_boolean.turn_on
    data_template:
      entity_id: input_boolean.partymode_active

The sensor.party_mode.status is a variable that gets read via command line (modbus).