integration icon indicating copy to clipboard operation
integration copied to clipboard

Simple work-arond

Open Johan-Claesson opened this issue 1 year ago • 6 comments

A simple suggestion, but wanted to share if it could help someone while we wait for an updated HASL integration.

Since the new API is now open, (dont know of any limitations yet?) one way forward is to create a rest-sensor in configuration.yaml:

sensor:
  - platform: rest
    resource: https://transport.integration.sl.se/v1/sites/9504/departures?transport=TRAIN&direction=1&forecast=60
    scan_interval: 300
    name: "Next Train"
    value_template: "{{value_json['departures'][0]['expected']}}"

and then we can create a template with something like this (i use HA-Fusion):

#### Nästa tåg:
{% set date_data = states('sensor.next_train') | as_datetime %}
{% set next_train = time_until(date_data,1) | replace("minutes","min") | replace ("hours","h") %}
{{next_train}} 

Dont know if it is best to have the formatting in the template, or to have the sensor know exactly how many minutes it is to the next train - but please take this and make it yours, and share if you know a better way of doing it :)

Johan-Claesson avatar Sep 18 '24 21:09 Johan-Claesson

My integration based on SL Realtidsinformation 4 worked until 1 October. I don't fully understand the information at Trafiklab website but it seems Resrobot 2.1 should still be working? So maybe it's just me making some mistake setting it up.

I really liked hasl departure lovelace card and with your rest sensor I only get time until next departure :/

Matss0n avatar Oct 02 '24 08:10 Matss0n

Yes its to bad it isnt working anymore :(

Yeah, what do you need? Also I did some tweaking to have it create additional sensors for the next 4 departures:

rest:
  - resource: https://transport.integration.sl.se/v1/sites/9504/departures?transport=TRAIN&direction=1&forecast=60
    scan_interval: 300 # the default is 30 seconds if you leave this line out, or change it to what you need.
    sensor:
      - name: "Train 1"
        value_template: "{{value_json['departures'][0]['expected'] | as_datetime | as_local()}}"
        unique_id: sensor.train_1
      - name: "Train 2"
        value_template: "{{value_json['departures'][1]['expected'] | as_datetime | as_local()}}"
        unique_id: sensor.train_2
      - name: "Train 3"
        value_template: "{{value_json['departures'][2]['expected'] | as_datetime | as_local()}}"
        unique_id: sensor.train_3
      - name: "Train 4"
        value_template: "{{value_json['departures'][3]['expected'] | as_datetime | as_local()}}"
        unique_id: sensor.train_4

And then I added this to my frontend so that I can see whenever the next train is leaving, and the one after that:

{% set t1 = states('sensor.train_1') | as_datetime %}
{% set t2 = states('sensor.train_2') | as_datetime %}
{% set t3 = states('sensor.train_3') | as_datetime %}
{% set t4 = states('sensor.train_4') | as_datetime %}

{% if t1 == 'Unknown'%}  
  {% set next_train = 'Okänt' %}
{% elif  t_now < t1%}  
  {% set next_train = t1 %}
  {% set next_next_train = t2 %}
{% elif  t_now < t2%}  
  {% set next_train = t2 %}
  {% set next_next_train = t3 %}
{% elif  t_now < t3%}  
  {% set next_train = t3 %}  
  {% set next_next_train = t4 %}
{%else%}
  {% set next_train = t4 %}  
{% endif %}


{% if ((today_at((next_train.strftime('%H:%M'))) - now()).seconds / 60) |int < 15 %}
  {% set next_train_min = (((today_at((next_train.strftime('%H:%M'))) - now()).seconds / 60) |int) %}
  {% set next_train_text = "om " + next_train_min | string +" minuter"%}

{%else%}
  {% set next_train_text = next_train.strftime('%H:%M') %}   
{% endif %}

<b> Pendeltåg:</b> <br>
{{ next_train_text }} <br>
& {{ next_next_train.strftime('%H:%M') }} <br>

Here is a scree shot from my current frontend (work in progress): https://imgur.com/DrJcMRt

Johan-Claesson avatar Oct 02 '24 18:10 Johan-Claesson

I did a similar one, and got some inspiration from Johan above (thanks!).

rest:
  - scan_interval: 180 
    resource: https://transport.integration.sl.se/v1/sites/<sitenr>/departures?transport=BUS&line=<line>&direction=2&forecast=60
    sensor:
      - name: "SL #1"
        value_template: "{{ as_timestamp(value_json['departures'][0]['expected']) | timestamp_custom('%H:%M') }}"
        unique_id: sl_bus_1
      - name: "SL #2"
        value_template: "{{ as_timestamp(value_json['departures'][1]['expected']) | timestamp_custom('%H:%M') }}"
        unique_id: sl_bus_2
      - name: "SL #3"
        value_template: "{{ as_timestamp(value_json['departures'][2]['expected']) | timestamp_custom('%H:%M') }}"
        unique_id: sl_bus_3
      - name: "SL #4"
        value_template: "{{ as_timestamp(value_json['departures'][3]['expected']) | timestamp_custom('%H:%M') }}"
        unique_id: sl_bus_4

I then used the mushroom template card to display this in lovelace:

type: custom:mushroom-template-card
primary: Bus
secondary: |-
  {% if not is_state('sensor.sl_bus_1', 'unknown') -%}
  - {{ states('sensor.sl_bus_1') }} 
  {% endif %}
  {%- if not is_state('sensor.sl_bus_2', 'unknown') -%}
  - {{ states('sensor.sl_bus_2') }} 
  {% endif %}
  {%- if not is_state('sensor.sl_bus_3', 'unknown') -%}
  - {{ states('sensor.sl_bus_3') }} 
  {% endif %}
  {%- if not is_state('sensor.sl_bus_4', 'unknown') -%}
  - {{ states('sensor.sl_bus_4') }} 
  {% endif %}
icon: mdi:bus
entity: sensor.sl_bus_1
multiline_secondary: true

Giving me this result: image

rasssta avatar Oct 05 '24 07:10 rasssta

I am using a similar method combined with OpenEPaperLink display to show the next buses at the entrance.

image

henrikbla avatar Oct 05 '24 07:10 henrikbla

My integration based on SL Realtidsinformation 4 worked until 1 October. I don't fully understand the information at Trafiklab website but it seems Resrobot 2.1 should still be working? So maybe it's just me making some mistake setting it up.

I really liked hasl departure lovelace card and with your rest sensor I only get time until next departure :/

Reserobot is working but I can't seem to configure it with direction 1 or 2 but 0 is fine

superture avatar Oct 08 '24 11:10 superture

I did a similar one, and got some inspiration from Johan above (thanks!).

rest:
  - scan_interval: 180 
    resource: https://transport.integration.sl.se/v1/sites/<sitenr>/departures?transport=BUS&line=<line>&direction=2&forecast=60
    sensor:
      - name: "SL #1"
        value_template: "{{ as_timestamp(value_json['departures'][0]['expected']) | timestamp_custom('%H:%M') }}"
        unique_id: sl_bus_1
      - name: "SL #2"
        value_template: "{{ as_timestamp(value_json['departures'][1]['expected']) | timestamp_custom('%H:%M') }}"
        unique_id: sl_bus_2
      - name: "SL #3"
        value_template: "{{ as_timestamp(value_json['departures'][2]['expected']) | timestamp_custom('%H:%M') }}"
        unique_id: sl_bus_3
      - name: "SL #4"
        value_template: "{{ as_timestamp(value_json['departures'][3]['expected']) | timestamp_custom('%H:%M') }}"
        unique_id: sl_bus_4

I then used the mushroom template card to display this in lovelace:

type: custom:mushroom-template-card
primary: Bus
secondary: |-
  {% if not is_state('sensor.sl_bus_1', 'unknown') -%}
  - {{ states('sensor.sl_bus_1') }} 
  {% endif %}
  {%- if not is_state('sensor.sl_bus_2', 'unknown') -%}
  - {{ states('sensor.sl_bus_2') }} 
  {% endif %}
  {%- if not is_state('sensor.sl_bus_3', 'unknown') -%}
  - {{ states('sensor.sl_bus_3') }} 
  {% endif %}
  {%- if not is_state('sensor.sl_bus_4', 'unknown') -%}
  - {{ states('sensor.sl_bus_4') }} 
  {% endif %}
icon: mdi:bus
entity: sensor.sl_bus_1
multiline_secondary: true

Giving me this result: image

Thanks for this! I can't write this by myself but have some experience in copy and pasting and editing and managed to work this out for myself.

image

superture avatar Oct 09 '24 08:10 superture

Using this as well, thanks a lot!

image

Question. Anyone know why I do only get maximum of 3 sensors (times) to work though?

I've added multiple in my configuration.yaml:

rest:

  • resource: https://transport.integration.sl.se/v1/sites/9183/departures?transport=METRO&direction=1 scan_interval: 30 # the default is 30 seconds if you leave this line out, or change it to what you need. sensor:
    • name: "Train 1" value_template: "{{ as_timestamp(value_json['departures'][0]['expected']) | timestamp_custom('%H:%M') }}" unique_id: sensor.train_1
    • name: "Train 2" value_template: "{{ as_timestamp(value_json['departures'][1]['expected']) | timestamp_custom('%H:%M') }}" unique_id: sensor.train_2
    • name: "Train 3" value_template: "{{ as_timestamp(value_json['departures'][2]['expected']) | timestamp_custom('%H:%M') }}" unique_id: sensor.train_3
    • name: "Train 4" value_template: "{{ as_timestamp(value_json['departures'][3]['expected']) | timestamp_custom('%H:%M') }}" unique_id: sensor.train_4
    • name: "Train 5" value_template: "{{ as_timestamp(value_json['departures'][4]['expected']) | timestamp_custom('%H:%M') }}" unique_id: sensor.train_5

But looking at sensor.train_4 and sensor.train_5 in Dev Tools they are always unknown:

image

fhanel avatar Oct 27 '24 22:10 fhanel

Using this as well, thanks a lot!

image

Question. Anyone know why I do only get maximum of 3 sensors (times) to work though?

I've added multiple in my configuration.yaml:

rest:

  • resource: https://transport.integration.sl.se/v1/sites/9183/departures?transport=METRO&direction=1 scan_interval: 30 # the default is 30 seconds if you leave this line out, or change it to what you need. sensor:

    • name: "Train 1" value_template: "{{ as_timestamp(value_json['departures'][0]['expected']) | timestamp_custom('%H:%M') }}" unique_id: sensor.train_1
    • name: "Train 2" value_template: "{{ as_timestamp(value_json['departures'][1]['expected']) | timestamp_custom('%H:%M') }}" unique_id: sensor.train_2
    • name: "Train 3" value_template: "{{ as_timestamp(value_json['departures'][2]['expected']) | timestamp_custom('%H:%M') }}" unique_id: sensor.train_3
    • name: "Train 4" value_template: "{{ as_timestamp(value_json['departures'][3]['expected']) | timestamp_custom('%H:%M') }}" unique_id: sensor.train_4
    • name: "Train 5" value_template: "{{ as_timestamp(value_json['departures'][4]['expected']) | timestamp_custom('%H:%M') }}" unique_id: sensor.train_5

But looking at sensor.train_4 and sensor.train_5 in Dev Tools they are always unknown:

image

The API only gives 3 departures unfortunately image

superture avatar Oct 28 '24 07:10 superture

The API only gives 3 departures unfortunately image

Ah, I see. I missed that part, that's unfortunate.

I will begin looking into creating a sensor and card for disturbances/delays on my specific metro-line. If anyone has made anything similar I'd be happy to get some inspo.

fhanel avatar Oct 28 '24 09:10 fhanel

Hey everyone, thanks for sharing this! The new SL sensors are available in the pre-release version of this integration, go check it out!

NecroKote avatar Dec 17 '24 11:12 NecroKote