Hi,
I have this python script, I want to start in a sandbox, at the same time Home-Assistant starts.
The program is very simple, and should just run in the background, when Home-Assistants run, and stop when Home-Assistant stops.
The program prepares som files, which later is read by Home-Assistant, in order to update some sensors.
I tried to make it run in script mode, but I can simply not figure out how to start it on Home-Assistant start.
This is the program, and it is absolutely disconnected from Home-Assistant, and it works perfectly, if I start it outside home-assistant, but that is not very smart.
import paho.mqtt.client as mqtt
import json
# Init a new asyncio event loop
# Create MQTT client
mqttClient = mqtt.Client()
# MQTT Event - on connect
def on_connect(client, userdata, flags, rc):
print("Connected")
client.subscribe("Tgrow_HIGrow/#")
# MQTT Event - on incomming message
def on_message(client, userdata, msg):
print(3)
global _future
payload = (msg.payload.decode("utf-8", "ignore"))
print(payload)
mqtt_data = json.loads(payload)
print("per")
MACid = mqtt_data["Tgrow_HIGrow"]["Tgrow_HIGrow"]
print("MACid", MACid)
filename = "Tgrow_Higrow" + MACid + ".json"
file = open('custom_components/TIGO-HIGrow/Tgrow_Higrow.conf', 'r')
found = False
for x in file:
if x.strip() == filename:
print (x)
found = True
print(found)
file.close()
with open('custom_components/TIGO-HIGrow/Tgrow_Higrow.conf', 'a') as conffile:
if found == False:
conffile.write(filename+ "\n")
with open(filename, 'w') as outfile:
outfile.write(payload)
print(1)
mqttClient.on_connect = on_connect
mqttClient.on_message = on_message
print(2)
print("start")
# Start the broker and loop^forever in the main thread
mqttClient.connect("192.168.1.64")
mqttClient.loop_forever()