Skip to content

Instantly share code, notes, and snippets.

@gtrabanco
Last active April 21, 2024 20:59
Show Gist options
  • Select an option

  • Save gtrabanco/6af76cdcd6f5bd1b54ba40694c302b0d to your computer and use it in GitHub Desktop.

Select an option

Save gtrabanco/6af76cdcd6f5bd1b54ba40694c302b0d to your computer and use it in GitHub Desktop.
Rest sensor for Oviedo local Buses
# You need to change last param of the resource url for your stop number
# You need to change "A" for whatever your line it is
# You can view all default params information stop here:
#. https://www.tua.es/es/lineas-y-horarios/paradas/uria-centro-1218.html?idLinea=6#paradasIda
# homeassistant:
# packages:
# rest: !include tua.yaml
input_text:
my_bus_line:
name: "Mi línea de autobús"
initial: "A"
rest:
- resource: https://www.tua.es/rest/estimaciones/1218
verify_ssl: false
scan_interval: 60
sensor:
- name: "Current Bus"
json_attributes_path: $.estimaciones.value[?(@.line == states('input_text.my_bus_line'))].vh_first
json_attributes:
- meters
- seconds
value_template: >-
{% set my_bus_line = states('input_text.my_bus_line') | string %}
{% set buses = value_json['estimaciones']['value'] | default([]) %}
{% if buses %}
{% for bus in value_json['estimaciones']['value'] %}
{% set bus_line = bus['line'] | default("None") %}
{% set eta = int(bus['vh_first']['seconds'], 0) %}
{% if bus_line == my_bus_line %}
{{ eta }}
{% endif %}
{% endfor %}
{% else %}
None
{% endif %}
- name: "Next Bus"
json_attributes_path: $.estimaciones.value[?(@.line == states('input_text.my_bus_line'))].vh_second
json_attributes:
- meters
- seconds
value_template: >-
{% set my_line = "A" %}
{% set buses = value_json['estimaciones']['value'] | default([]) %}
{% if buses %}
{% for bus in value_json['estimaciones']['value'] %}
{% set bus_line = bus['line'] | default("None") %}
{% set eta = int(bus['vh_second']['seconds'], 0) %}
{% if bus_line == my_line %}
{{ eta }}
{% endif %}
{% endfor %}
{% else %}
None
{% endif %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment