Surplus regulation with python script in Home Assistant

Hi there,

before I’m using Home Assistant I used an Rasperry Pi to regulate the solar surplus for my wallbox for my car. This works perfectly. I’ve used this python script:

# Import some modules
import json, requests, time
from decimal import Decimal

# Variable declaration

url_pv     = 'http://PV/solar_api/v1/GetPowerFlowRealtimeData.fcgi'
url_wb_amp = 'http://charger/mqtt?payload=amx=' #Ampere (e.g. 16)
url_wb_alw = 'http://charger/mqtt?payload=alw=' #OnOff (0 or 1)

# Functions

def get_pv_data():
  return json.loads(requests.get(url_pv).text)

def load():
	P_Load = (data['Body']['Data']['Site']['P_Load'])
	if P_Load != None:
  		return int(round(Decimal(P_Load)))
	else:
		return 0

def grid():
        P_Grid = (data['Body']['Data']['Site']['P_Grid'])
        if P_Grid != None:
                return int(round(Decimal(P_Grid)))
        else:
                return 0

def battery():
        P_Battery = (data['Body']['Data']['Site']['P_Akku'])
        if P_Battery != None:
                return int(round(Decimal(P_Battery)))
        else:
                return 0

def pv():
        P_PV = (data['Body']['Data']['Site']['P_PV'])
        if P_PV != None:
                return int(round(Decimal(P_PV)))
        else:
                return 0

def soc():
        SOC = (data['Body']['Data']['Inverters']['1']['SOC'])
        if SOC != None:
                return int(SOC)
        else:
                return 0

#main

data = get_pv_data()
charge_power = 6

while True:
        while ((grid() + battery()) <= -1380) and (charge_power <= 20):
                charge_power = charge_power + 1
                requests.post(url_wb_alw+"1")
                requests.post(url_wb_amp+str(charge_power))
                data = get_pv_data()
                time.sleep(5)

        while ((grid() + battery()) >= -100) and (charge_power >= 6):
                if (charge_power - 1) < 6:
                        time.sleep(120)
                        data = get_pv_data()
                        if ((grid() + battery()) >= -100) and (charge_power >=6):
                                requests.post(url_wb_alw+"0")
                else:
                        charge_power = charge_power - 1
                        requests.post(url_wb_alw+"1")
                        requests.post(url_wb_amp+str(charge_power))
                data = get_pv_data()
                time.sleep(5)
        data = get_pv_data()
        time.sleep(5)

Then I’ve used this script in Home Assistant Core on an Ubuntu Client over the services option to start this script directly in the bash shell for about 1 year (this was a first test to gain experience with Homeassistant). In lovelace I had a switch at my charger card to activate or deactivate this script. This works also fine.

Today I did a complete new installation of Homeassistant on a VMWare ESXi server and used the HomeAssistant OS image. Now I search for a solution to use this script but I’m getting errors. I can’t use the “import” in my script.

What can I do to use my script for the regulation? Or are there other methods to do that?
Thanks in advance

Regards