############################################
# Enter your your sensor name here. #
############################################
entity_id = 'sensor.next_train_to_eus'
scheduled = False # Check if train scheduled
train_time = '08:04'
if hass.states.get(entity_id).state == "No departures":
logger.warn('No Departures')
else:
attributes = hass.states.get(entity_id).attributes
morning_train_status = hass.states.get('sensor.my_8_04_train').state
try:
for train in attributes['next_trains']:
if train['scheduled'] == train_time:
scheduled = True # Check if train scheduled
if train['status'] != my_8_04_train_status:
hass.states.set('sensor.my_8_04_train', train['status'])
if train['status'] == 'ON TIME':
hass.services.call('script', 'turn_on', { "entity_id" : 'script.bedroom_light_green' })
hass.services.call('notify', 'ahmed', { "title": "Train Status Update", "message": "The {} is scheduled to be on time.".format(train_time)})
elif train['status'] == 'LATE':
hass.services.call('script', 'turn_on', { "entity_id" : 'script.bedroom_light_orange' })
hass.services.call('notify', 'ahmed', { "title": "Train Status Update", "message": "The {} will be late and is now sheduled at".format(train_time) + train$
elif train['status'] == 'CANCELLED':
hass.services.call('script', 'turn_on', { "entity_id" : 'script.bedroom_light_red' })
hass.services.call('notify', 'ahmed', { "title": "Train Status Update", "message": "The {} has been cancelled.".format(train_time)})
break
except:
logger.warn('Failed to get next trains')
if not scheduled:
hass.states.set('sensor.my_8_04_train', 'No data') # check no data
#logger.warn('Test complete')
I think if it worked yesterday then its an issue with the data source, prob reached requests limit or network issue.
Give it another shot tomorrow.
Cheers
So the script has been working all fine but now I get an error and my lights do not change colour anymore.
The error I get is:
Traceback (most recent call last):
File "trains_state.py", line 16, in <module>
if hass.states.get(MAIN_ENTITY_ID).state == "No departures":
NameError: name 'hass' is not defined
Hmm strange… I tried a different python script and it worked fine.
I suppose it’s something to do with HA and/or the location of the python scripts folder has been changed? Its definitely something to do with HA in relation to running python scripts.
I downgraded to 0.59.2 and have the same issue. I will try an older version of HA and see if it works like it did in the past.
I’m also playing around with another pair that run when the train sensor changes between a time window to notify every couple of minutes if any trains are running late. The only problem is that if trains were late but become on time again, I wont know about it: