OK worked nicely for me this am. Here is what I have:
trains_state.py
entity_id = 'sensor.next_train_to_wim'
attributes = hass.states.get(entity_id).attributes
for train in attributes['next_trains']:
if train['estimated'] == '07:45':
hass.states.set('sensor.my_7_45_train', train['status'])
break
else:
hass.states.set('sensor.my_7_45_train', 'No data') # check no data
and my_7_45_train.py
entity_id = 'sensor.my_7_45_train'
status = hass.states.get(entity_id).state
if status not in ['No data', 'EARLY']:
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' })
hass.services.call('notify', 'ios_robins_iphone',
{'title':'7:45 train update', 'message':'Train status is {}'.format(status)})
This code that you gave me didn’t work this morning for me. I had the same issue. In fact I had 37 notifications on pushbullet in 35mins of runtime i.e. the runtime of the automations.
I will try your latest codes and see if it works for me tomorrow morning.
@bachoo786 you were correct, both scripts were being triggered, resulting in all the notifications. Turns out that even though the first script was repeatedly setting the state to ‘ON TIME’, this was enough to trigger the second script. I’ve updated trains_state.py to include an additional check to only update the state if it has changed. Note I have also changed the search field to ‘scheduled’ (forgot this was an option).
Thanks for sticking with this, can you let me know if the following resolves your problems?
trains_state.py [Note that sensor.my_7_45_train must exist]
entity_id = 'sensor.next_train_to_wim'
attributes = hass.states.get(entity_id).attributes
my_7_45_current_status = hass.states.get('sensor.my_7_45_train').state
for train in attributes['next_trains']:
if train['scheduled'] == '07:45':
if train['status'] != my_7_45_current_status:
hass.states.set('sensor.my_7_45_train', train['status'])
break
else:
hass.states.set('sensor.my_7_45_train', 'No data') # check no data
my_7_45_train.py
entity_id = 'sensor.my_7_45_train'
status = hass.states.get(entity_id).state
if status not in ['No data', 'EARLY', 'NO REPORT']:
if status == 'ON TIME':
hass.services.call('light', 'turn_on', { "entity_id" : 'light.lamp', 'color_name': 'green' })
elif status == 'LATE':
hass.services.call('light', 'turn_on', { "entity_id" : 'light.lamp', 'color_name': 'orange' })
elif status == 'CANCELLED':
hass.services.call('light', 'turn_on', { "entity_id" : 'light.lamp', 'color_name': 'red' })
hass.services.call('notify', 'ios_robins_iphone',
{'title':'7:45 train update', 'message':'Train status is {}'.format(status)})
The code that you provided yesterday didn’t work at all for me this morning. The lights nevery went off surprinsgly. I thought it could be due to my automation but I haven’t touched it.
I will try the code that you just provided. I assume the green lights turn on when the train is on time and the red light turns on if anything else and for both of these situations a notification is sent to your phone. Is this correct?
That break statement wasn’t working for me this morning. Edited to make foolproof:
trains_state.py
entity_id = 'sensor.next_train_to_wim'
attributes = hass.states.get(entity_id).attributes
my_early_train_current_status = hass.states.get('sensor.my_early_train').state
scheduled = False # Check if train scheduled
for train in attributes['next_trains']:
if train['scheduled'] == '07:15':
scheduled = True
if train['status'] != my_early_train_current_status:
hass.states.set('sensor.my_early_train', train['status'])
if not scheduled:
hass.states.set('sensor.my_early_train', 'No data') # check no data
I had fewer notifications this morning one every 3 minutes think it’s the updates from UK transport.
Have you changed the sensor to an earlier train whilst having your 7.56 train too in the new code for the trains_state.py above? It’s just that I thought of doing the same for my 8.04 train.
On another note how do you get the lights to turn off after the automation? I have tried switching my lights off right after the automation of the two python scripts but surpringly it doesn’t work. However it works for any other time of the day.
I changed the train, its actually my Girlfriend who catches the train and decided to get an earlier one :-/
Currently just manually turning off, could creathe an automation or scene to do it
My version for info / playing about with.
Has some code to prevent errors (try, if else)
Also covers unknown statuses etc and is covered by using one python script rather than 2
I’m thinking to wrap the script in a function that I can pass a list of trains to monitor. Also need to add the logic to crate sensors if they don’t exist
I had an idea to pass Traintimes into an input select with the ability to set a default and use the python script to monitor that particular train time