Set lamp colour depending on train status

A simple script that checks the state of a train sensor (can be ‘ON TIME’, ‘LATE’ etc.) and sets the colour of a Hue lamp.

state = hass.states.get('sensor.next_waterloo_train_status')
train_status = state.state

if train_status == 'ON TIME':'light', 'turn_on', { "entity_id" : 'light.lamp', 'color_name': 'green' })
else:'light', 'turn_on', { "entity_id" : 'light.lamp', 'color_name': 'red' })

nice one Rob

i like the idea - will try to get this implemented (am using Yeelights myself at present)

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':
  'light', 'turn_on', { "entity_id" : 'light.lamp', 'color_name': 'green' })
  'light', 'turn_on', { "entity_id" : 'light.lamp', 'color_name': 'red' })

I have a similair setup for my morning commute (using Yeelight):

- alias: 'Commute Light'
    platform: time
    minutes: '/5'
    seconds: '0'
    condition: and
      - condition: time
          - mon
          - tue
          - wed
          - thu
          - fri
      - condition: time
        after: '06:35:00'
        before: '07:35:00'        
      - condition: state
        entity_id: device_tracker.bob_s8
        state: 'home'
    service: light.turn_on
    entity_id: light.pc
      color_name: >
            {% if states.sensor.google_travel_time__driving.state < "21" %} green
            {% elif states.sensor.google_travel_time__driving.state == "21" %} orange
            {% elif states.sensor.google_travel_time__driving.state == "22" %} orange
            {% elif states.sensor.google_travel_time__driving.state == "23" %} orange            
            {% elif states.sensor.google_travel_time__driving.state > "23" %} red
            {% endif %}
      brightness: 255

Checkout the Workaday sensor too

Will do, thanks for the tip.

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.

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

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

if status not in ['No data', 'ON TIME', 'EARLY']:'light', 'turn_on', { "entity_id" : 'light.lamp', 'color_name': 'red' })'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:

- action:
  - service: python_script.train_state
  alias: train_state_trigger
  condition: []
  id: '1503121451330'
  - entity_id: sensor.next_train_to_eus
    platform: state
- action:
  - service: python_script.my_7_54_train
  alias: my_7_54_automation
  condition: []
  id: '1503121509166'
  - entity_id: sensor.my_7_54_train
    platform: state

Let me know how you get on with this.


Hi Rob

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?


The automations editor created those ID, and the first script creates that Entity

But have you got a sensor of the 7.54 train i.e. sensor.my_7_54_train
Or was that created in the

It was created by the script, neat hey!

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 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 Line 1: SyntaxError: invalid syntax in on statement: entity_id = 'sensor.my_8_04_train' status =

any idea buddy?

Can’t tell from that, must be a typo

right I have thoroughly checked the code against yours and it gives me the same error, here is my code:

- action:
  - service: python_script.trains_state
  alias: train_state_trigger
  condition: []
  id: '1503121451330'
  - entity_id: sensor.next_train_to_eus
    platform: state
- action:
  - service: python_script.my_8_04_train
  alias: my_8_04_automation
  condition: []
  id: '1503121509166'
  - entity_id: sensor.my_8_04_train
    platform: state

Looks like an error from the script…?

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?

Thanks mate !

Glad that’s solved… :wink:
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 :smile:

so basically what you are saying is we can’t control the time we want the lights to turn on? as that is all controlled by the API.

Remove ‘ON TIME’? you mean from the script?

i.e. from this line: if status not in ['No data', 'ON TIME', 'EARLY']:

No more missing trains now :smile:

You could add conditions re timing. Your only limitation is your imagination :slight_smile:
And yes

1 Like

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