Set lamp colour depending on train status

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)})

Hi @robmarkcole

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?

Thanks.

Correct :slight_smile:

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

Hi @robmarkcole

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

@robmarkcole Right so with your latest code everything worked as expected this morning flawlessly. Thanks!

Ok great!
Let me know if any issues come up, otherwise I will write this up as a project on Hackster soon :slight_smile:

1 Like

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

https://github.com/Pow377/UK_Transport_State_Card/tree/master/Lights_based_on_status

1 Like

I see you’ve skipped:

if not scheduled:
    hass.states.set('sensor.my_early_train', 'No data')  # check no data

Put my version on https://github.com/robmarkcole/TrasportAPI-HASS/blob/master/lights_based_on_status/train_state.py

Just amended mine to reset the status each day. otherwise wouldnt get the intial ontime notification / colour.

@robmarkcole just looked at your train_state.py on github and seems like you havent changed much rather have merged the 2 python files together?

how does your automation look like now?

Yes only the first automation is required.
I’m sure the script can be improved further

Your new version is nice. Will take a proper look later ended up with beers and pizza so had to put it on hold :joy:

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

Right the new version never worked for me this morning I followed what you done and I had a warning saying : failed to get next trains

Did you experience the same?

Hi, were there trains?