Domoticz crashes because of my script
Posted: Monday 03 May 2021 17:29
Hello,
i'm teaching myself python and thought it would be a good learning experience to create a domoticz event script. The idea is when i update my phone's alarm settings, it automatically changes my Hue sunrise routine and change the timer on my bedroom shutter. My phone updates a uservariable (AlarmTime) in domoticz via tasker as epoch(linux time) or if i cancel the alarm it updates it to a string 'next'. the script is triggered on updated uservariables. I convert the epoch time to the time format the hue api understands and to the timeformat domoticz understands. basically HUE sunrise routine needs to start 30 min before the alarm and the shutter should open 10 min after the alarm.
My issue is that as soon as my alarm of today has rang, it updates the uservariable to tomorrows alarm and the shutter timer is overwritten with tomorrows timer. basically it never opens automatically. to get around this issue i thought it would be a good idea to save today's alam time in another uservariable (lastalarmtime) and compare the two. if the new alarm is in 24 hours don't delete the old one. this all seems to work as i would expect but as soon as i update the lastalarmtime uservariable domoticz crashes. i honestly dont see where the issue is. any ideas or suggestions are more than welcome.
i'm teaching myself python and thought it would be a good learning experience to create a domoticz event script. The idea is when i update my phone's alarm settings, it automatically changes my Hue sunrise routine and change the timer on my bedroom shutter. My phone updates a uservariable (AlarmTime) in domoticz via tasker as epoch(linux time) or if i cancel the alarm it updates it to a string 'next'. the script is triggered on updated uservariables. I convert the epoch time to the time format the hue api understands and to the timeformat domoticz understands. basically HUE sunrise routine needs to start 30 min before the alarm and the shutter should open 10 min after the alarm.
My issue is that as soon as my alarm of today has rang, it updates the uservariable to tomorrows alarm and the shutter timer is overwritten with tomorrows timer. basically it never opens automatically. to get around this issue i thought it would be a good idea to save today's alam time in another uservariable (lastalarmtime) and compare the two. if the new alarm is in 24 hours don't delete the old one. this all seems to work as i would expect but as soon as i update the lastalarmtime uservariable domoticz crashes. i honestly dont see where the issue is. any ideas or suggestions are more than welcome.
Code: Select all
import Domoticz , DomoticzEvents as DEVS
if '/home/domoticz/.local/lib/python3.7/site-packages' not in sys.path :
DEVS.Log('Site packages path not found')
sys.path.append( '/home/domoticz/.local/lib/python3.7/site-packages' )
from datetime import datetime , requests, json, urllib
def isint(value):
try:
int(value)
return True
except ValueError:
return False
DomoticzIP = '192.168.10.51'
ShutterIDX = '151'
HueIP = '192.168.10.52'
HueUsername = '0000000000000-000000000-0000000000000000000000000000'
for name, value in DEVS.user_variables.items():
res = isinstance(value, str)
if name == 'LastAlarmTime':
LastAlarm = value
if name == 'AlarmTime':
# Get Hue schedules
response = requests.get(f"http://{HueIP}/api/{HueUsername}/schedules")
data = json.loads(response.text)
# Get the ID of the Schedule called Wake Up Sunrise
for i in data.keys():
if data[i].get('name') == 'Wake up Sunrise':
Schedule = i
if isint(value) :
DEVS.Log('New Alarm')
# Take 30 min of for Hue sunrise
HueAlarm = int(value) - 1800
# add 10 min for opening shutter
ShutterAlarm = int(value) + 600
# Create JSON Post for Hue schedule
TimeAlarm = datetime.utcfromtimestamp(HueAlarm).strftime('/T%H:%M:%S')
DayAlarm = datetime.utcfromtimestamp(HueAlarm).weekday()
TimeFormated = {0 : f'W064{TimeAlarm}',1 : f'W032{TimeAlarm}',2 : f'W016{TimeAlarm}',3 : f'W008{TimeAlarm}',4 : f'W004{TimeAlarm}',5 : f'W002{TimeAlarm}',6 : f'W001{TimeAlarm}',}
JsonPutAlarm = { "time": TimeFormated[DayAlarm], "status": "enabled"}
# Create url elements for shutter opening
HourAlarm = datetime.fromtimestamp(ShutterAlarm).strftime('%H')
MinuteAlarm = datetime.fromtimestamp(ShutterAlarm).strftime('%M')
DateAlarm = datetime.fromtimestamp(ShutterAlarm).strftime('%Y-%m-%d')
if LastAlarm == value :
break
# calculate if the new alarm is in 24 hours
TwentyFourHours = int(LastAlarm) + 86400 - int(value)
if TwentyFourHours != 0 :
DEVS.Log('not 24 hours difference')
# remove shutter timers if alarm is not in 24 hours
ClearShutter = requests.get(f'http://{DomoticzIP}/json.htm?type=command¶m=cleartimers&idx={ShutterIDX}')
# set shutter opening
SetShutter = requests.get(f'http://{DomoticzIP}/json.htm?active=true&command=1&date={DateAlarm}&days=0&hour={HourAlarm}&idx={ShutterIDX}&level=1&min={MinuteAlarm}¶m=addtimer&randomness=false&timertype=5&type=command')
# save alarm to LastAlarm
SetLastAlarm = requests.get(f'http://{DomoticzIP}/json.htm?type=command¶m=updateuservariable&vname=LastAlarmTime&vtype=0&vvalue={value}')
elif str(res):
DEVS.Log('Cancel Alarm')
# Create JSON Post for Hue schedule
JsonPutAlarm = { "status": "disabled"}
# remove shutter timers if alarm is not in 24 hours
requestShutter = requests.get(f'http://{DomoticzIP}/json.htm?type=command¶m=cleartimers&idx={ShutterIDX}')
# post sunrise
putHue = requests.put(f"http://{HueIP}/api/{HueUsername}/schedules/{Schedule}", json=JsonPutAlarm)