Bonjour,
Voilà après quelque jours de prompting GPT j’ai réussis à récupérer et afficher les horaires des trains dans HA. Je sais qu’il y a une integration dédié mais j’ai préféré utiliser directement une api.
Voilà ce qu’il vous faut pour le faire:
-
S’enregistrer pour obtenir un token API sur Demandez votre clé d'accès gratuite - SNCF NUMÉRIQUE
-
Recupérer le code de la gare ( Nice pour moi ) depuis Fréquentation en gares — SNCF Open Data
-
Créer un sensor dans rest.yaml :
# SNCF Departures from Nice
- resource: https://<votre token ici>@api.sncf.com/v1/coverage/sncf/stop_areas/stop_area:SNCF:87756056/departures?data_freshness=realtime
scan_interval: 60
sensor:
- name: SNCF Next Departure Nice
value_template: >
{% set departures = value_json['departures'] %}
{% if departures | length >= 1 %}
{% set dep1 = departures[0]['stop_date_time']['departure_date_time'] %}
{{ strptime(dep1, '%Y%m%dT%H%M%S').strftime('%Y-%m-%d %H:%M:%S') }}
{% else %}
No upcoming departures
{% endif %}
json_attributes:
- departures
- Créer une carte:
4.1:
type: markdown
title: Trains départs Nice
content: >-
{% set departures = state_attr('sensor.sncf_next_departure_nice',
'departures') %}
{% if departures and departures | length >= 1 %}
{% for next_train in departures[:10] %}
{% set dep_time = next_train['stop_date_time']['departure_date_time'] %}
{% set base_dep_time =
next_train['stop_date_time']['base_departure_date_time'] %}
{% set delay_minutes = ((strptime(dep_time, '%Y%m%dT%H%M%S') -
strptime(base_dep_time, '%Y%m%dT%H%M%S')).seconds / 60) | int %}
{% set dep_time_formatted = strptime(dep_time,
'%Y%m%dT%H%M%S').strftime('%H:%M') %}
{% set base_dep_time_formatted = strptime(base_dep_time,
'%Y%m%dT%H%M%S').strftime('%H:%M') %}
{% if delay_minutes > 0 %}
{% set display_time = "<s>" ~ base_dep_time_formatted ~ "</s> " ~
dep_time_formatted %}
{% else %}
{% set display_time = dep_time_formatted %}
{% endif %}
{% if 'canceled' in next_train.get('additional_informations', []) %}
{% set status = "🚫 Annulé" %}
{% elif delay_minutes > 0 %}
{% set status = "🚨 Retard: " ~ delay_minutes ~ " min" %}
{% else %}
{% set status = "✅ À l'heure" %}
{% endif %}
{% set destination =
next_train['display_informations']['direction'].split('(')[0].strip() %}
{% if destination in ['Monaco', 'Menton', 'Ventimiglia'] %}
**{{ display_time }} {{ destination.upper() }}, {{
next_train['display_informations']['trip_short_name'] }} - {{ status }}**
{% else %}
{{ display_time }} {{ destination }}, {{
next_train['display_informations']['trip_short_name'] }} - {{ status }}
{% endif %}
{% endfor %}
{% else %}
🚫 Aucun départ imminent
{% endif %}
4.2:
type: markdown
title: Trains départs Nice2
content: >-
{%- set departures = state_attr('sensor.sncf_next_departure_nice',
'departures') -%} {%- if departures and departures | length >= 1 %}
{%- for next_train in departures[:10] %}
{%- set stop_date_time = next_train.get('stop_date_time', {}) -%}
{%- set dep_time_str = stop_date_time.get('departure_date_time') -%}
{%- set base_dep_time_str = stop_date_time.get('base_departure_date_time') -%}
{%- if dep_time_str and base_dep_time_str %}
{%- set dep_time = strptime(dep_time_str, '%Y%m%dT%H%M%S') -%}
{%- set base_dep_time = strptime(base_dep_time_str, '%Y%m%dT%H%M%S') -%}
{%- if dep_time and base_dep_time %}
{%- set delay_minutes = ((dep_time - base_dep_time).total_seconds() / 60) | int -%}
{%- set dep_time_formatted = dep_time.strftime('%H:%M') -%}
{%- set base_dep_time_formatted = base_dep_time.strftime('%H:%M') -%}
{%- if delay_minutes > 0 %}
{%- set display_time = "<s>" ~ base_dep_time_formatted ~ "</s> " ~ dep_time_formatted -%}
{%- set status = "🚨 Retard: " ~ delay_minutes ~ " min" -%}
{%- else %}
{%- set display_time = dep_time_formatted -%}
{%- set status = "" -%}
{%- endif %}
{%- set display_informations = next_train.get('display_informations', {}) -%}
{%- set direction = display_informations.get('direction', '') -%}
{%- set destination = direction.split('(')[0].strip() -%}
{%- set trip_short_name = display_informations.get('trip_short_name', '') -%}
{%- set commercial_mode = display_informations.get('commercial_mode', '') -%}
{%- set train_type_and_number = commercial_mode ~ '-' ~ trip_short_name -%}
{%- if destination in ['Monaco', 'Menton', 'Ventimiglia'] %}
{%- set output_line = "**" ~ display_time ~ "** **" ~ destination | upper ~ "**, " ~ train_type_and_number -%}
{%- else %}
{%- set output_line = "**" ~ display_time ~ "** " ~ destination ~ ", " ~ train_type_and_number -%}
{%- endif %}
{%- if status %}
{%- set output_line = output_line ~ " - " ~ status -%}
{%- endif %}
{{ output_line }}
{%- endif %}
{%- endif %}
{%- endfor %}
{%- else %}
'🚫 Aucun départ imminent'
{%- endif %}
- Admirer votre carte et suivre les départs (j’ai mis en gras/majuscule les destinations qui m’itéresse le plus )
5.1:
5.2: