Need help with python_scripts

You need to read data from the I2C bus and assign the data to the sensor. On the basis of the file ( wrote the scripts.

#import struct
#import smbus
# import sys
# import time

#bus = smbus.SMBus(DEVICE_BUS)

#read = bus.read_word_data(DEVICE_ADDRESS, 2)
#swapped = struct.unpack("<H", struct.pack(">H", read))[0]
#voltage = swapped * 1.25 /1000/16

inputAttributesObject = {}
inputState = 0
# inputState = voltage
hass.states.set('sensor.x728_voltage', inputState, inputAttributesObject)

But it doesn't work. If you apply it as it is written, the sensor.x728_voltage is created equal to 0. if you remove any # sensor, sensor.x728_voltage is not created.
What is the problem?

You can’t import anything in the python_script environment. That’s ultimately your problem.

So you also have the Geekworm X728 UPS Hat, did you find a way to get this to work?

I just started my HA on my RPi and this is my first mission.

No, I couldn’t.