Set lamp colour depending on train status

Perhaps update chrome?

It’s updated. Do you mind explaining what your automation does please?

On another note, this morning I noticed the UK transport apI gives you an update of your train 2 hours before the scheduled time of your interest. (8.04 train is of my interrst and the lights went off at 6.05). The funny thing is the whole train journey is less than 2 hours from start to finish. Does that seem right?

Hi, il write this up in full on Hackster very soon, just waiting for some trains (Waterloo repairs)! In the meantime you can read the source code on GitHub :+1:

Rob is it possible to change the the python script of my_7_54_train.py? I wanted to introduce time condition in the for loop because I have noticed that the state data sent by the api is every 2 mins which sends notifications to the phone every 2 mins and triggers the light to be turned on every 2mins.

I understand I can introduce time condition in the automation script but this would still send notifications and trigger the light to be turned on every 2mins. Just my 2 pence bud.

The second script is only triggered by state changes, I.e from on time to delayed for example

hmm I was wondering what does if not mean? i.e. if status not in ['No data', 'ON TIME', 'EARLY’] what does this do?

Can we not just use simple if statements? E.g. if train is on time then turn on green lights.

If it’s not on time or in other words delayed then turn on the red lights?

It means IF the status is not in that list THEN DO the loop.
Yes you could also write statements for each possible status, sounds like this is what you want so have a go :slight_smile:

Will have a go as I am a noob and still learning

1 Like

right here is my piece, what do you think buddy?

entity_id = 'sensor.my_8_04_train'
status = hass.states.get(entity_id).state

    if status in ['ON TIME']:
        hass.services.call('switch', 'turn_on', { "entity_id" : 'switch.bedroom_lights' })
        hass.services.call('notify', 'ahmed',
                           {'title':'8:04 train update', 'message':'Train status is {}'.format(status)})
    elif status in ['LATE', 'CANCELLED', 'NO REPORT']:
          hass.services.call('switch', 'turn_on', { "entity_id" : 'switch.bedroom_lights' })
          hass.services.call('notify', 'ahmed',
                             {'title':'8:04 train update', 'message':'Train status is {}'.format(status)})

I wasnt sure if I needed the in after if status ??

On another note Rob, I am not getting data from UK Transport today on my HA. Do you experience the same?

I get errors saying:

ERROR (MainThread) [homeassistant.components.sensor.template] Could not render template Next train estimated: UndefinedError: 'mappingproxy object' has no attribute 'next_trains'
ERROR (MainThread) [homeassistant.components.sensor.template] Could not render template Next train scheduled: UndefinedError: 'mappingproxy object' has no attribute 'next_trains'
ERROR (MainThread) [homeassistant.components.sensor.template] Could not render template Next train origin: UndefinedError: 'mappingproxy object' has no attribute 'next_trains'
ERROR (MainThread) [homeassistant.components.sensor.template] Could not render template Next train status: UndefinedError: 'mappingproxy object' has no attribute 'next_trains'
ERROR (MainThread) [homeassistant.components.sensor.template] Could not render template Next train platform: UndefinedError: 'mappingproxy object' has no attribute ‘next_trains'

and when I execute trains_state.py I get the following error:

ERROR (Thread-5) [homeassistant.components.python_script.trains_state.py] Error executing script: 'next_trains'
Traceback (most recent call last):
  File "/srv/homeassistant/lib/python3.4/site-packages/homeassistant/components/python_script.py", line 115, in execute
    exec(compiled.code, restricted_globals, local)
  File "trains_state.py", line 5, in <module>
  File "/srv/homeassistant/lib/python3.4/site-packages/RestrictedPython/Eval.py", line 35, in default_guarded_getitem
    return ob[index]
KeyError: 'next_trains'

There are trains on your line? Lots of rail repairs this weekend, and Euston is closed if I recall

1 Like

You could have IF status == ON TIME.

In is only required for a list.

1 Like

Well I have set up UK transport from my station to Euston. Euston is closed I believe but I have just had an email from UK transport saying my limit usage is above 80%. I wonder why it’s doing that?

That’s by design, to make maximum use of the 1000 daily requests, I would forward those warning emails to junk :grimacing:

Hi Rob

The for loop from the trains_state.py is causing the notifications to be sent to push bullet every min. Is there an alternative to this?

I know you may not experience this issue because you have set your code for only when the trains are delayed.

Cheers mate.

HI again, trains_state.py does not send any notifications, unless you have changed it…?

The second script (my_7_54_train.py) is only triggered by state changes

I suggest you check your automations and scripts, an error must have crept in, unless the status of the train really is changing frequently…

Cheers

Hi

I can confirm my scripts and automation are correct and mirror yours. Here they are:

trains_state.py:

entity_id = 'sensor.next_train_to_eus'
attributes = hass.states.get(entity_id).attributes
hass.states.set('sensor.my_8_04_train', 'No data')  # check no data

for train in attributes['next_trains']:
    if train['estimated'] == '08:04':
        hass.states.set('sensor.my_8_04_train', train['status'])

my_8_04_train.py:

entity_id = 'sensor.my_8_04_train'
status = hass.states.get(entity_id).state

if status == 'ON TIME':
    hass.services.call('script', 'turn_on', { "entity_id" : 'script.bedroom_light_green' })
    hass.services.call('notify', 'ahmed',
                       {'title':'8:04 train update', 'message':'Train status is {}'.format(status)})
elif status in ['LATE', 'CANCELLED', 'NO REPORT']:
      hass.services.call('script', 'turn_on', { "entity_id" : 'script.bedroom_light_red' })
      hass.services.call('notify', 'ahmed',
                         {'title':'8:04 train update', 'message':'Train status is {}'.format(status)})

automation:

- alias: 'Train State'
  trigger:
    - entity_id: sensor.next_train_to_eus
      platform: state
  condition:
    - condition: time
      after: '07:15:00'
      before: '07:50:00'
      weekday:
        - mon
        - tue
        - wed
        - thu
        - fri
  action:
    - service: python_script.trains_state


- alias: '8 04 Train Status'
  trigger:
    - entity_id: sensor.my_8_04_train
      platform: state
  condition:
    - condition: time
      after: '07:15:00'
      before: '07:50:00'
      weekday:
        - mon
        - tue
        - wed
        - thu
        - fri
  action:
    - service: python_script.my_8_04_train

I strongly feel the status of the train is changing frequently. Do you have any solution to this?

Cheers!!

OK could be a bug in trains_state.py since it is setting sensor.my_8_04_train status to ‘No data’ on every call.
Try changing to

entity_id = 'sensor.next_train_to_eus'
attributes = hass.states.get(entity_id).attributes

for train in attributes['next_trains']:
    if train['estimated'] == '07:54':
        hass.states.set('sensor.my_7_54_train', train['status'])
        break
    else:
        hass.states.set('sensor.my_7_54_train', 'No data')  # check no data

Right, I will try and see if this command hass.states.set('sensor.my_7_54_train', train['status’]) does the trick.

Quick question does the if statement still continue to carry out the else command if there is a break in between? In my case the else statement is irrelevant because I haven’t added the option of “No Data” in my_8_04.py script.

Break exits the loop