bachoo786
(Bachoo786)
December 27, 2017, 7:05pm
1
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 ?
fabaff
(Fabian Affolter)
December 27, 2017, 9:17pm
2
You can’t run the script manually as the environment is sandboxed. You can only execute it as service call.
bachoo786
(Bachoo786)
December 27, 2017, 9:37pm
3
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.
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.
bachoo786
(Bachoo786)
December 28, 2017, 2:20pm
6
@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.
bachoo786
(Bachoo786)
December 28, 2017, 5:43pm
9
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?
cjacunad
(Carlos J. Acuna)
July 29, 2019, 1:51am
10
Hi,
Maybe you are using the hass object inside a function, phyton scripts in HA restrict global variables , 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