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.

Revisions

  1. gtrabanco renamed this gist Apr 21, 2024. 1 changed file with 36 additions and 4 deletions.
    40 changes: 36 additions & 4 deletions configuration.yaml → tua.yaml
    Original file line number Diff line number Diff line change
    @@ -10,17 +10,18 @@
    input_text:
    my_bus_line:
    name: "Mi línea de autobús"
    initial: "A"
    initial: "J"
    my_bus_stop:
    name: "Mi parada de autobús"
    initial: 1218
    initial: 4112

    rest:
    - resource_template: https://www.tua.es/rest/estimaciones/{{ states('input_text.my_bus_stop') }}
    verify_ssl: false
    scan_interval: 60
    sensor:
    - name: "Current Bus"
    - name: "Current Bus Estimated seconds"
    unique_id: "current_bus_eta"
    json_attributes_path: $.estimaciones.value[?(@.line == states('input_text.my_bus_line'))].vh_first
    json_attributes:
    - meters
    @@ -39,7 +40,23 @@ rest:
    {% else %}
    None
    {% endif %}
    - name: "Next Bus"
    - name: "Current Bus Estimated Time"
    unique_id: "current_bus_eta_hhmmss"
    json_attributes_path: $.estimaciones.value[?(@.line == states('input_text.my_bus_line'))].vh_first
    json_attributes:
    - meters
    - seconds
    value_template: >-
    {% set from_time = int(as_timestamp(states.sensor.current_bus_estimated_seconds.last_changed), 0) %}
    {% if from_time > 0 %}
    {% set eta = int(states('sensor.current_bus_estimated_seconds')) %}
    {% set eta_time = from_time + eta %}
    {{ eta_time | timestamp_custom("%H:%M:%S") }}
    {% else %}
    {{ None }}
    {% endif %}
    - name: "Next Bus Estimated Seconds"
    unique_id: "next_bus_eta"
    json_attributes_path: $.estimaciones.value[?(@.line == states('input_text.my_bus_line'))].vh_second
    json_attributes:
    - meters
    @@ -58,3 +75,18 @@ rest:
    {% else %}
    None
    {% endif %}
    - name: "Next Bus Estimated Time"
    unique_id: "next_bus_eta_hhmmss"
    json_attributes_path: $.estimaciones.value[?(@.line == states('input_text.my_bus_line'))].vh_second
    json_attributes:
    - meters
    - seconds
    value_template: >-
    {% set from_time = int(as_timestamp(states.sensor.next_bus_estimated_seconds.last_changed), 0) %}
    {% if from_time > 0 %}
    {% set eta = int(states('sensor.current_bus_estimated_seconds')) %}
    {% set eta_time = from_time + eta %}
    {{ eta_time | timestamp_custom("%H:%M:%S") }}
    {% else %}
    {{ None }}
    {% endif %}
  2. gtrabanco revised this gist Mar 22, 2024. 1 changed file with 6 additions and 3 deletions.
    9 changes: 6 additions & 3 deletions configuration.yaml
    Original file line number Diff line number Diff line change
    @@ -11,9 +11,12 @@ input_text:
    my_bus_line:
    name: "Mi línea de autobús"
    initial: "A"
    my_bus_stop:
    name: "Mi parada de autobús"
    initial: 1218

    rest:
    - resource: https://www.tua.es/rest/estimaciones/1218
    - resource_template: https://www.tua.es/rest/estimaciones/{{ states('input_text.my_bus_stop') }}
    verify_ssl: false
    scan_interval: 60
    sensor:
    @@ -42,13 +45,13 @@ rest:
    - meters
    - seconds
    value_template: >-
    {% set my_line = "A" %}
    {% set my_bus_line = states('input_text.my_bus_line') %}
    {% 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 %}
    {% if bus_line == my_bus_line %}
    {{ eta }}
    {% endif %}
    {% endfor %}
  3. gtrabanco revised this gist Mar 22, 2024. 1 changed file with 14 additions and 4 deletions.
    18 changes: 14 additions & 4 deletions configuration.yaml
    Original file line number Diff line number Diff line change
    @@ -2,32 +2,42 @@
    # 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 == "A")].vh_first
    json_attributes_path: $.estimaciones.value[?(@.line == states('input_text.my_bus_line'))].vh_first
    json_attributes:
    - meters
    - seconds
    value_template: >-
    {% set my_line = "A" %}
    {% 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_line %}
    {% if bus_line == my_bus_line %}
    {{ eta }}
    {% endif %}
    {% endfor %}
    {% else %}
    None
    {% endif %}
    - name: "Next Bus"
    json_attributes_path: $.estimaciones.value[?(@.line == "A")].vh_second
    json_attributes_path: $.estimaciones.value[?(@.line == states('input_text.my_bus_line'))].vh_second
    json_attributes:
    - meters
    - seconds
  4. gtrabanco created this gist Mar 22, 2024.
    47 changes: 47 additions & 0 deletions configuration.yaml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,47 @@
    # 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
    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 == "A")].vh_first
    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_first']['seconds'], 0) %}
    {% if bus_line == my_line %}
    {{ eta }}
    {% endif %}
    {% endfor %}
    {% else %}
    None
    {% endif %}
    - name: "Next Bus"
    json_attributes_path: $.estimaciones.value[?(@.line == "A")].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 %}