Integration of Real-time Israel Bus Data Retrieval with Home Assistant Assist
This README describes how to use Home Assistant's Assist feature to get information on bus arrival times at specific stations with a few simple steps.
Attached are some example images.
As shown in the images above, you can obtain bus line information by:
A reserved keyword
: "לעבודה" or "לבית".Providing the station number and the lines
: 'תחנה=XXX קווים=nn,nn' or 'ת=XXX ק=nn,nn' or 'תחנה XXX קווים nn,nn' or 'ת XXX ק nn,nn'Providing only the station number
: If you only provide the station number, you will receive information about all the lines at the station you provided.
Important: At the time of writing this guide, the core version is 2024.7.1. Note that things may work differently in other versions.
- Create a new folder named
python_scripts
under theconfig
directory. - Create a new file named
get_bus_times.py
. - Copy the code from here into the new file you created.
Using the Shell Command integration, you can run the script you just created. Add the following lines to the configuration.yaml
file:
shell_command:
get_bus_time: "/bin/bash -c '/config/python_scripts/get_bus_times.py {{ station }} {{ lines }}'"
After adding the script and Shell Command integration, restart Home Assistant.
A quick check to see that everything is working as expected:
If the response looks like this
stdout: "Usage: ./get_bus_times.py <station_number> <bus_lines>"
stderr: ""
returncode: 1
it's working correctly. if not, something might have been missed.
Let's create a Macros Templates File. This will allow us to define code that can be used in multiple places. For more information about the Macros Templates File, see here.
- Create a new folder named
custom_templates
under theconfig
directory. - Create a new file named
tools.jinja
. - Copy the code from here into the new file you created.
Create the automation that returns information given a station number and buses, or just a station number:
alias: Get bus time
trigger:
- platform: conversation
command:
- תחנה[=][ ]{station} קווים[=][ ]{lines}
- ת[=][ ]{station} ק[=][ ]{lines}
- תחנה[=][ ]{station}
- ת[=][ ]{station}
condition: []
action:
- service: shell_command.get_bus_time
response_variable: return_response
data:
station: "{{ trigger.slots.station }}"
lines: "{{ trigger.slots.lines | default('null') }}"
- set_conversation_response: |
{% from 'tools.jinja' import format_arrival %}
{{ format_arrival(return_response['stdout']) }}
mode: single
Automation for reserved words:
alias: Get bus time - Home
trigger:
- platform: conversation
command:
- לבית
- to home
condition: []
action:
- service: shell_command.get_bus_time
response_variable: return_response
data:
station: "5200"
lines: 62,31
- set_conversation_response: |
{% from 'tools.jinja' import format_arrival %} {{
format_arrival(return_response['stdout']) }}
mode: single
alias: Get bus time - Work
description: ""
trigger:
- platform: conversation
command:
- לעבודה
- to work
condition: []
action:
- service: shell_command.get_bus_time
response_variable: return_response
data:
station: "2262"
lines: 62,72
- set_conversation_response: |
{% from 'tools.jinja' import format_arrival %} {{
format_arrival(return_response['stdout']) }}
mode: single
Of course, change the values of station
and lines
as needed.
Now you can access Assist in Home Assistant, enter a station and bus lines, and get information about when those lines are supposed to arrive.