Hi folks,
Having a blast again coming back to my home assistant setup.
I cooked up a quick rest integration sensor that checks on my local country’s upcoming trains from cityA to cityB in a given date/time and it creates 3 sensors out of it. Here’s how I’m doing it:
# /config/configuration.yml
rest:
- resource: "https://israelrail.azurefd.net/rjpa-prod/api/v1/timetable/searchTrainLuzForDateTime"
method: GET
params:
fromStation: 4200
toStation: 4600
date: >
{{ now().strftime('%Y') ~ '-' ~ now().strftime('%m') ~ '-' ~ now().strftime('%d') }}
hour: >
{{ now().strftime('%H') ~ ':' ~ now().strftime('%M') }}
headers:
Accept: "application/json"
Content-Type: "application/json"
sensor:
- name: "Next Train 1"
unique_id: "next_train_1"
value_template: >-
{%- set indexStart = value_json["result"]["startFromIndex"] + 1 -%}
{%- if ((indexStart) < (value_json["result"]["travels"] | length )) -%}
{{ strptime(value_json["result"]["travels"][indexStart]["departureTime"], '%Y-%m-%dT%H:%M:%S') }}
{%- else -%}
Not available
{%- endif -%}
- name: "Next Train 2"
unique_id: "next_train_2"
value_template: >-
{%- set indexStart = value_json["result"]["startFromIndex"] + 2 -%}
{%- if ((indexStart ) < (value_json["result"]["travels"] | length )) -%}
{{ strptime(value_json["result"]["travels"][indexStart]["departureTime"], '%Y-%m-%dT%H:%M:%S') }}
{%- else -%}
Not available
{%- endif -%}
- name: "Next Train 3"
unique_id: "next_train_3"
value_template: >-
{%- set indexStart = value_json["result"]["startFromIndex"] + 3 -%}
{%- if ((indexStart ) < (value_json["result"]["travels"] | length )) -%}
{{ strptime(value_json["result"]["travels"][indexStart]["departureTime"], '%Y-%m-%dT%H:%M:%S') }}
{%- else -%}
Not available
{%- endif -%}
scan_interval: 300
What I’d love to do is create a custom component out of it so I can share it with the community and other home assistant users can easily install it and add these sensors to their dashboards.
Is there a simple example of doing this which I can find on GitHub and copy over to my own repo with minimal changes required? I’m not versed on the internals of creating custom components but I can work out the Python stuff fine
Thanks!