GithubHelp home page GithubHelp logo

warrior25 / ha-nysse Goto Github PK

View Code? Open in Web Editor NEW
7.0 4.0 0.0 298 KB

Home Assistant integration to fetch public transport data for Tampere, Finland

License: MIT License

Python 100.00%
home-assistant nysse public-transport tampere finland

ha-nysse's Introduction

HA-Nysse

hacs_badge

Introduction

Home Assistant integration to fetch public transport data for Tampere, Finland

Installation

Copy the files to your custom_components folder or install as a HACS custom repository. More info on HACS here.

Setup

Open your Home Assistant instance and start setting up a new integration.

The integration can be set up from the frontend by searching for Nysse.

Usage

Each station creates a sensor which contains data for departures from that station. Explanations for non self-explanatory attributes are listed below.

realtime - Indicates if the data is pulled from realtime vehicle monitoring or timetable data

Frontend examples

Simple frontend examples using custom:html-template-card

Example

type: custom:html-template-card
title: Keskustori D
ignore_line_breaks: true
content: >
  {% set departures = state_attr('sensor.keskustori_d_0015','departures')
  %} {% for i in range(0, departures | count, 1) %}

  <div style="display:grid; grid-template-columns: 2fr 1fr; font-size: 20px;
  padding: 10px 0px 0px 0px"> <div>{{ departures[i].line }} - {{
  departures[i].destination }}</div><div style="text-align: right">{% if
  departures[i].realtime %}<ha-icon style="color:green; padding: 0px 10px 0px
  0px" icon="mdi:signal-variant"></ha-icon>{% endif %} {% if
  departures[i].time_to_station | int < 21  %} {{departures[i].time_to_station}}
  min {% else %}{{departures[i].departure}}{% endif %}</div></div>

  {% endfor %}

Service Alerts

type: custom:html-template-card
ignore_line_breaks: true
title: Service Alerts
content: >
  {% set alerts = state_attr('sensor.nysse_service_alerts','alerts')
  %} {% for i in range(0, alerts | count, 1) %}

  <b>{{ alerts[i].start.strftime('%d.%m.%Y') }} - {{ alerts[i].end.strftime('%d.%m.%Y') }}</b><br>
  {{ alerts[i].description }}<br><br>

  {% endfor %}

Advanced usage

Combine data from multiple stops

 - platform: template
    sensors:
      combined_stops:
        value_template: "{{ states('sensor.stop1') }}"
        attribute_templates:
          departures: >-
            {% set combined_data = state_attr('sensor.stop1', 'departures') + state_attr('sensor.stop2', 'departures') %}
            {{ combined_data | sort(attribute='time_to_station') }}

Known issues / limitations

  • Nysse API sometimes functions incorrectly. Errors logged with Nysse API error can be resolved on their own over time.
  • Line icons are resolved from a hardcoded list of tram lines. If new tram lines are built, the list needs to be updated in const.py.

Troubleshooting

If the integration is not working correctly, follow these steps as a first measure:

  1. Remove all files from config/www/nysse
  2. Reload the integration

This forces the integration to fetch latest data from Nysse and recreates the database.

ha-nysse's People

Contributors

warrior25 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

ha-nysse's Issues

Example HTML to include n amount in display

Hi! not really an issue but more of an featurish request.
I currently have the following on my card:

type: custom:html-template-card
ignore_line_breaks: true
content: >
  <style>.grid-container { display: grid;

   padding: 10px; } </style>

  <div style="font-size:24px", class="grid-container"><br>STOP N
  (to centre)</div> {% set departures =
  state_attr('sensor.keskisenkatu_1_1234, 'departures') %} 

  {% for i in range(0, departures | count, 1) %} <div style="display:grid;
  grid-template-columns: 2fr 1fr; font-size: 20px; padding: 10px 0px 0px 0px">
  <div><span style="border-style:solid;background:blue;border:2px solid white
  ">&nbsp{% if departures[i].line | int < 10 %} {{departures[i].line}}&nbsp {%
  else %}{{departures[i].line}}&nbsp{% endif %}</span>&nbsp &nbsp&nbsp {{
  departures[i].destination }}</div><div style="text-align: right">{% if
  departures[i].realtime %}<ha-icon style="color:#4ba97b; padding: 0px 10px 0px
  0px" icon="mdi:signal-variant"></ha-icon>{% endif %} {% if
  departures[i].time_to_station | int < 21  %} {{departures[i].time_to_station}}
  min {% else %}{{departures[i].departure}}{% endif %}</div></div>
   
  {% endfor %}

Different from your card example it greates "nysse" themed box around the line number (tried with icons but numeric icons stop at "9". It's not perfect but does the job, kind of. shows the linenumber-destination-time(and live time). But I've really been trying to get n amount of stops into one single card but seems like its impossible? I'm no pro in HTML so this is why I'm opening issue since you got HTML knowledge among this projject! lets say keskustori A and keskustori B into same card somehow.

Seems like fetching data doesn't work at all for me

Seems like the nysse schedules do not work anymore and if does randomly show schedules it works for a day max.... I assume the problem is at nysses end? does the integration work on your (or someone elses system?)
in the latest patch what was the modification you did on fetching the data and if i understood correctly it was supposed to work like "trains toilet" compared to the another way you tried for a short while.

image
image

Sensor creation fails for some stops

Works fine with stops such as Aakkula, but when trying with stops having higher number (approx > 5400), then sensor creation fails. See log snippet below.

Working:
2022-11-04 23:02:24.250 INFO (MainThread) [homeassistant.components.sensor] Setting up sensor.nysse
2022-11-04 23:02:24.259 INFO (MainThread) [custom_components.nysse.sensor] Fectching stop points
2022-11-04 23:02:24.421 INFO (MainThread) [custom_components.nysse.sensor] Fectching arrivals from https://data.itsfactory.fi/journeys/api/1/stop-monitoring?stops=4014
2022-11-04 23:02:24.504 INFO (MainThread) [custom_components.nysse.sensor] Not enough timetable data
2022-11-04 23:02:24.504 INFO (MainThread) [custom_components.nysse.sensor] Fetching timetable data for Friday, from http://data.itsfactory.fi/journeys/api/1/journeys?stopPointId=4014&dayTypes=Friday&startIndex=0
2022-11-04 23:02:24.897 INFO (MainThread) [custom_components.nysse.sensor] Fetching timetable data for Friday, from http://data.itsfactory.fi/journeys/api/1/journeys?stopPointId=4014&dayTypes=Friday&startIndex=100
2022-11-04 23:02:25.283 INFO (MainThread) [custom_components.nysse.sensor] Fetching timetable data for Friday, from http://data.itsfactory.fi/journeys/api/1/journeys?stopPointId=4014&dayTypes=Friday&startIndex=200
2022-11-04 23:02:25.685 INFO (MainThread) [custom_components.nysse.sensor] Fetching timetable data for Friday, from http://data.itsfactory.fi/journeys/api/1/journeys?stopPointId=4014&dayTypes=Friday&startIndex=300
2022-11-04 23:02:26.292 INFO (MainThread) [custom_components.nysse.nysse_data] Aakkula
2022-11-04 23:02:26.293 INFO (MainThread) [homeassistant.helpers.entity_registry] Registered new sensor.nysse entity: sensor.nysse_aakkula_4014

Failing:
2022-11-04 23:03:35.526 INFO (MainThread) [homeassistant.components.sensor] Setting up sensor.nysse
2022-11-04 23:03:35.532 INFO (MainThread) [custom_components.nysse.sensor] Fectching stop points
2022-11-04 23:03:35.691 INFO (MainThread) [custom_components.nysse.sensor] Fectching arrivals from https://data.itsfactory.fi/journeys/api/1/stop-monitoring?stops=9648
2022-11-04 23:03:35.765 INFO (MainThread) [custom_components.nysse.sensor] Not enough timetable data
2022-11-04 23:03:35.766 INFO (MainThread) [custom_components.nysse.sensor] Fetching timetable data for Friday, from http://data.itsfactory.fi/journeys/api/1/journeys?stopPointId=9648&dayTypes=Friday&startIndex=0
2022-11-04 23:03:35.854 INFO (MainThread) [custom_components.nysse.sensor] Not enough timetable data remaining for today
2022-11-04 23:03:35.855 INFO (MainThread) [custom_components.nysse.sensor] Fetching timetable data for Saturday, from http://data.itsfactory.fi/journeys/api/1/journeys?stopPointId=9648&dayTypes=Saturday&startIndex=0
2022-11-04 23:03:35.883 WARNING (MainThread) [custom_components.nysse.sensor] Not enough timetable data was found. Reduce the amount of requested departures
2022-11-04 23:03:35.888 ERROR (MainThread) [homeassistant.components.sensor] Error adding entities for domain sensor with platform nysse
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 428, in async_add_entities
await asyncio.gather(*tasks)
File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 588, in _async_add_entity
suggested_object_id = entity.name
File "/config/custom_components/nysse/sensor.py", line 163, in name
station = self._nysse_data.get_station_name()
File "/config/custom_components/nysse/nysse_data.py", line 101, in get_station_name
self._station_name = self._stops[self._station]
TypeError: list indices must be integers or slices, not str
2022-11-04 23:03:35.896 ERROR (MainThread) [homeassistant.components.sensor] Error while setting up nysse platform for sensor
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 289, in _async_setup_platform
await asyncio.gather(*pending)
File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 428, in async_add_entities
await asyncio.gather(*tasks)
File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 588, in _async_add_entity
suggested_object_id = entity.name
File "/config/custom_components/nysse/sensor.py", line 163, in name
station = self._nysse_data.get_station_name()
File "/config/custom_components/nysse/nysse_data.py", line 101, in get_station_name
self._station_name = self._stops[self._station]
TypeError: list indices must be integers or slices, not str

Hide departures by time

It would be nice if you could hide departures which will be arrive before you can get to the stop. In example, if it takes 5 min walking, it will only show vehicles that are more than 5 min away.

Filter bus routes

Can we have a new feature where departure bus lines can be filtered. For example, Hervantakeskus F 3699, it would be nice to have only bus number 19 departures.

Error 500 occured 20.1. ~11:30-->

Hey,
On 20.1. around 11:30...12:00 the HA stopped receiving any nysse data (2 stops) with error:

Error
Config flow could not be loaded: 500 Internal Server Error Server got itself in trouble

Idk what happened nor why. Restarting HA and/or entities/integration had no effect.

Failed to configurate v1.1.0

Something broke when I installed the latest update of Nysse integration v1.1.0. The integration got "failed to unload" -status. After rebooting the integration started working again but failed to unload again in the middle of night. I tried to reinstall the integration but now I cannot configure it. I have no logs of the failed to unload error but the here's the configuration error and its log.

Error
Config flow could not be loaded: 400: Bad Request

Logger: custom_components.nysse.fetch_api
Source: custom_components/nysse/fetch_api.py:37
Integration: Nysse ([documentation](https://github.com/warrior25/HA-Nysse), [issues](https://github.com/warrior25/HA-Nysse/issues))
First occurred: 10:27:46 (1 occurrences)
Last logged: 10:27:46

Failed to fetch stops: 'body'

"Feature" request

That Nysse alerts was cool addition, thanks for the update!
Whenever you have extra time, could you create an example for a card how to pull data out of the alert (attributes) and show it in clean format (alert #, description, effective from dd.mm.-dd.mm.yyyy) I've been trying to use markdown card and implementing the same method as we discussed back with the "Example HTML to include n amount in display" -issue but i have no enough knowledge to get it working.

...I'd show the alert data on a wall tablet but atm the presentation is uglyish:
image

type: entity
entity: sensor.nysse_service_alerts
name: nysse alert
attribute: alerts

extra: Maybe add some sort of filtering on what alerts to show, i.e. pick the lines to follow (idk if this is possible):
alert 1: Event in nokia arena, buses re-routed
alert2: bus stop nnnn out of order (line yy stop) due to whatever reason.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.