Python error when executing script via automation

I have a script in /config/python_scripts that works fine from terminal inside HA.

However if i call the same script via an automation I get the following error:

Error loading script getwatts.py: Line 28: "__init__" is an invalid attribute name because it starts with "_"., Line 29: "_stop" is an invalid attribute name because it starts with "_"., Line 53: "_stop" is an invalid attribute name because it starts with "_"., Line 56: "_stop" is an invalid attribute name because it starts with "_".
any ideas hive mind?

Show how the automation is executing getwatts.py.

  • id: ‘1628595771120’
    alias: Refresh Power Check Python
    description: ‘’
    trigger:
    • platform: time_pattern
      hours: ‘
      minutes: '

      condition: []
      action:
    • service: python_script.getwatts
      mode: single

As suspected, you are using the python_script integration which runs in a sandboxed environment with several constraints (like inability to use import). Basically, your python code is incompatible with what the python_script integration permits.

I believe the workaround may be to execute getwatts.py as a shell command.

Solved. Works perfectly. Many thanks!!! Everyday is school day.

1 Like

You’re welcome!

Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been resolved. This helps users find answers to similar questions.