Why is the python_scripts environment so crippled?

I’m having a terrible time getting very basic stuff to work in python. For example:

File "color_temp.py", line 22, in calc_color
NameError: name '_inplacevar_' is not defined

All line 22 does is a -= 1, where a is just a float.

I’m being serious. As soon as I changed the line to a = a - 1 the script works. This is unacceptable by even the lowest standards! I also can’t use map, nor starred expressions in function calls. What am I doing wrong?

Nothing. The sandbox python does not accept a -= 1. You need to write it as a = a - 1 and it will work as you found out.

I was not aware of that limitation and will admit that one sounds unnecessarily harsh. But the reason for all of these restrictions is in the docs, the scripts are run in a sandboxed environment with limited capabilities. Imports for instance are strictly not allowed and it appears there are a lot of other restrictions as well.

If you want to do more advanced python scripting the answer is also in the docs, you should check out AppDaemon instead.

1 Like

I am sorry, but the “docs” are a one page brief description of python scripts. If Python is the main scripting language it would deserve a little more attention when it comes to documentation!?

  • what are the sandbox limits?
  • where is a documentation of the most import interface call ''hass.services.call" ? There is only one(!) example given for a switching a light, what about the syntax for calling other items?
1 Like

I agree, the documentation should be updated to include more details about what the specific restrictions are in the sandbox. I wasn’t trying to say that the docs covered everything, apologize if it came off that way. I was simply saying that the reason the author of this post was encountering that issue is because python scripts are sandboxed and pointing out that the documentation itself directs you to AppDaemon if you find yourself running up against the boundaries of the sandbox.

Unfortunately I can’t really help much with laying out the details of the sandbox for you. I don’t use the python scripts integration enough to know the boundaries of it. I did try and use it once and was quite frustrated when I discovered I could not import. Shortly after that I discovered the Node RED add-on and now do nearly all of my automation there.

The documentation is open source and editable by anyone however. So if you do continue to explore and discover the boundaries of the sandbox I’m sure users would appreciate it if you updated the documentation with your findings.

In terms of examples, the documentation advocates sharing examples in the scripts section of the forum. Now unfortunately the term scripts is a bit too generic and it looks like the posts in there are now a mix of python scripts and HA scripts whereas I think the original intention was for that to be the section for users to share their python scripts. It might be good to have a subsection of that forum specifically for python scripts but if you search you should be able to find some additional examples and info.