"UnboundLocalError: local variable 'device_friendly_name' referenced before assignment"

I’ve got a couple of the above errors in my log and wonder what needs to change to resolve them. I’m using “platform: min_max”, not sure whether the error is related. Examples with context:

(attempt to copy/paste/format in proper way for this forum; excuses if i did not succeed)

‘’’
2020-09-05 11:39:28 ERROR (MainThread) [homeassistant.helpers.entity] Update for sensor.door_window_sensor_158d00022d0129_battery_level fails
Traceback (most recent call last):
File “/srv/homeassistant/lib/python3.7/site-packages/homeassistant/helpers/entity.py”, line 263, in async_update_ha_state
await self.async_device_update()
File “/srv/homeassistant/lib/python3.7/site-packages/homeassistant/helpers/entity.py”, line 454, in async_device_update
await self.async_update() # type: ignore
File “/usr/lib/python3.7/asyncio/coroutines.py”, line 120, in coro
res = func(*args, **kw)
File “/home/homeassistant/.homeassistant/custom_components/attributes/sensor.py”, line 231, in async_update
self._name = device_friendly_name
UnboundLocalError: local variable ‘device_friendly_name’ referenced before assignment
2020-09-05 11:39:28 ERROR (MainThread) [homeassistant.helpers.entity] Update for sensor.door_window_sensor_158d00022da144_battery_level fails
Traceback (most recent call last):
File “/srv/homeassistant/lib/python3.7/site-packages/homeassistant/helpers/entity.py”, line 263, in async_update_ha_state
await self.async_device_update()
File “/srv/homeassistant/lib/python3.7/site-packages/homeassistant/helpers/entity.py”, line 454, in async_device_update
await self.async_update() # type: ignore
File “/usr/lib/python3.7/asyncio/coroutines.py”, line 120, in coro
res = func(*args, **kw)
File “/home/homeassistant/.homeassistant/custom_components/attributes/sensor.py”, line 231, in async_update
self._name = device_friendly_name
UnboundLocalError: local variable ‘device_friendly_name’ referenced before assignment
‘’’

The Unboundlocalerror: local variable referenced before assignment is raised when you try to use a variable before it has been assigned in the local context. Python doesn’t have variable declarations , so it has to figure out the scope of variables itself. It does so by a simple rule: If there is an assignment to a variable inside a function, that variable is considered local . To solve this problem, you can explicitly say it’s a global by putting global declaration in your function. The global statement does not have to be at the beginning of the function definition, but that is where it is usually placed. Wherever it is placed, the global declaration makes a variable to global variable everywhere in the function.

1 Like

Thank you! Since i’m working in YAML and not touching the python code, and afaik i can’t influence the order in which variables are created and referenced, i assume it is something in Python code i as user is using , and everyone using the code should have this…

Yes, I have this same issue.