OK updated this script to search through the attributes for a scheduled train at a particular time. Note there are 13 upcoming trains in the attributes, and these can have a status ‘ON TIME’, ‘LATE’ etc.
entity_id = 'sensor.next_train_to_eus'
attributes = hass.states.get(entity_id).attributes # attributes is a dict
next_trains = attributes['next_trains'] # next_trains is a list of upcoming trains, must access dict in this way
logger.warning("Number of upcoming trains is {}".format(len(next_trains)))
for train in next_trains:
estimated = train['estimated']
status = train['status']
if estimated == '07:56':
logger.warning("Estimated departure at {} identified".format(estimated))
logger.warning("Status is {}".format(status))
if status == 'ON TIME':
hass.services.call('light', 'turn_on', { "entity_id" : 'light.lamp', 'color_name': 'green' })
else:
hass.services.call('light', 'turn_on', { "entity_id" : 'light.lamp', 'color_name': 'red' })
OK got the complete alert system working. Consists of 2 python scripts, and a sensor per train to monitor.
The first script is a variation on my last post, and checks the attributes of my uk_transport sensor (sensor.next_train_to_eus) for an instance of my train of interest at 7:54. On detecting this train, it sets the state of a sensor for that train (sensor.my_7_54_train) which acts as a global variable. This script is run by an automation which is triggered on the state change of sensor.next_train_to_eus, i.e. every time it is updated with fresh data.
trains_state.py
entity_id = 'sensor.next_train_to_eus'
attributes = hass.states.get(entity_id).attributes
hass.states.set('sensor.my_7_54_train', 'No data') # check no data
for train in attributes['next_trains']:
if train['estimated'] == '07:54':
hass.states.set('sensor.my_7_54_train', train['status'])
The second python script is run by an automation that is triggered on a state change of sensor.my_7_54_train
my_7_54_train.py
entity_id = 'sensor.my_7_54_train'
status = hass.states.get(entity_id).state
if status not in ['No data', 'ON TIME', 'EARLY']:
hass.services.call('light', 'turn_on', { "entity_id" : 'light.lamp', 'color_name': 'red' })
hass.services.call('notify', 'ios_robins_iphone',
{'title':'7:54 train update', 'message':'Train status is {}'.format(status)})
And the automations, which I generated using the Automations editor in the front end:
Just a wabted to know about the id in the automation. What are these numbers and where can I get mine from?
Also on your python script you got “sensor.my_7_54_train” I don’t have that, all I have is “sensor.next_train_to_eus” do I need to specify the 7.54 train as a sensor? if yes how can I do that?
I have followed your method and get the following errors when I trigger my automation:
ERROR (Thread-11) [homeassistant.components.python_script] Error loading script trains_state.py: Line 1: SyntaxError: invalid syntax in on statement: entity_id = 'sensor.next_train_to_eus' attributes =
ERROR (Thread-8) [homeassistant.components.python_script] Error loading script my_8_04_train.py: Line 1: SyntaxError: invalid syntax in on statement: entity_id = 'sensor.my_8_04_train' status =
Thats correct there was an error in the script and I have resolved it I think.
I hit my 8_04_train automation trigger and my lights turn on and I get a push bullet notification. The notification on the phone says “Train status is None” which is understandable as there is no update for the 8.04 train yet.
However, my question is will the automation work even if the train is not delayed? or does it just works when the train is delayed? because I wanted the lights to turn on regardless if its delayed or on time.
And finally, at what time would the lights turn on in the morning?
Glad that’s solved…
Automation triggered on state changes so when the train appears in the return by the API or when the status of that train changes.
I assume that the Estimated time is the scheduled time and that should be the same daily.
Remove the ‘ON TIME’ from the list to be notified of that state.
No excuses for missing a train again now
yeah my only issue is your automation code is confusing me as you have got it from automations editor on chrome. I cannot find my automations editor on chrome for some reason.
So I have to re-wrtite the automation and I cannot really make a head or tail out of your generated automation, hence I just copied yours and changed a few bits to match my script name etc