I have a shell scrip that checks to see if the date in an input_text matches the current date. If the dates do not match I want to stop the automation’s sequence of actions. The python script below does correctly text the date, but when dates do not match, calling sys.exit(1) does not stop the automation.
matchDate.py
#!/usr/bin/python3.12
import sys
from datetime import date
logfile = open("/var/snap/home-assistant-snap/current/shellScript.log","a")
debug = True
if debug:
logfile.write('Running {0} {1}\n'.format(sys.argv[0], sys.argv[1]))
nextStr = sys.argv[1]
nextLst = nextStr.split('-')
testDay = date.today()
testLst = testDay.timetuple()
matches = False
error = 1
if debug:
logfile.write('Test: Year {0} Month {1} Day {2}\n'.format(testLst[0], testLst[1], testLst[2]))
logfile.write('Next: Year {0} Month {1} Day {2}\n'.format(nextLst[0], nextLst[1], nextLst[2]))
if (int(testLst[0]) == int(nextLst[0])) and (int(testLst[1]) == int(nextLst[1])) and (int(testLst[2]) == int(nextLst[2])):
matches = True
error = 0
if debug:
logfile.write('Dates Match: {0}\n'.format(matches))
print('{0}\n'.format(matches))
sys.exit(error)
This is a check on turning up the heat in my vacation home which is controlled by home assistant. If shell_command.match_date returns an error the heat should be left alone.
I’m not seeing anything in the documentation for shell_command that says a non-zero exit code would do anything. Why do you think it should stop the automation?
On the other-hand, there is an example of checking the return code in an automation. Have you tried that?
First, thanks for the link to an example. Looks like it will work.
Second, why I thought that an error exit would stop the automation sequence:
This is from https://www.home-assistant.io/docs/scripts/
Continuing on error
By default, a sequence of actions will be halted when one of the actions in that sequence encounters an error. The automation or script will be halted, an error is logged, and the automation or script run is marked as errored.
Ok, I can see how it might be interpreted that way, especially in the context of a shell script.
Based on the docs I would assume that a non-zero exit code is not an error for shell_command. But this does give you more flexibility to handle different return codes rather than binary error/no-error.