NameError: name 'hass' is not defined

Hi all

I need your help with python. I have this python script from Set lamp colour depending on train status and everything was working well until now.

I get an error NameError: name 'hass' is not defined when I run the script manually.

I am running HA 0.60 with python 3.6.

Can anyone please help me, maybe @balloob?

You can’t run the script manually as the environment is sandboxed. You can only execute it as service call.

I mean when I run the python command i.e. python trains_state.py and I get NameError: name 'hass' is not defined

The script doesn’t work anymore as the lights don’t turn on as per the train status.

Anyone …?

The hass object is passed into the script by Homeassistant when and only when run as a “python script”

If running manually or as a shell command it won’t work.

@andrewdolphin thanks for the reply.

Does that mean that the script is wrong? Because the same script is used by others and it works fine.

Looking through that thread all those people are caling it as a python script which would pass the hass object into the script.

Hi @andrewdolphin

It was all working for me before and I have followed the guide in that thread on how the script should work.

Unfortunately now it’s not working at all for me. I suppose something has changed in my python_script.py that is causing this issue.

I have the python folder and all that so wonder what the problem is.

Do you have any idea on how I could debug this issue?

Hi,

Maybe you are using the hass object inside a function, phyton scripts in HA restrict global variables :pensive:, so if that is the case you have to pass the hass object as a default parameter, for example:

def setTtsMessage(myMsg,myMedia=“media_player.hub_sala”,vol=0.7,keepAlive = False, hass = hass):

1 Like