Skip to content

Commit

Permalink
add time offset to skånetrafiken PT sensor
Browse files Browse the repository at this point in the history
  • Loading branch information
matt8707 committed Nov 3, 2021
1 parent 8252dca commit af5284a
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 13 deletions.
17 changes: 17 additions & 0 deletions include/automation.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,23 @@ automation:
target:
entity_id: sensor.synology_dsm_latest

- alias: skanetrafiken_scan_interval
id: '0745322571221'
initial_state: true
trigger:
- platform: time_pattern
minutes: /5
condition:
condition: not
conditions:
- condition: time
after: '01:00:00'
before: '04:00:00'
action:
- service: homeassistant.update_entity
target:
entity_id: sensor.skanetrafiken

####################################################
# #
# MONITORS #
Expand Down
25 changes: 18 additions & 7 deletions include/sensor.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,24 @@ sensor:
name: skanetrafiken
resource: !secret skanetrafiken
value_template: >
{{ value_json.Trip[0].LegList.Leg[1].Product.num }}
json_attributes_path: $.Trip[0].LegList.Leg[1].Origin
json_attributes:
- name
- time
- date
scan_interval: 268
{% if value_json is defined %}
{% set ns = namespace(found=false) %}
{% for i in range(value_json.Trip | length) %}
{% set json = value_json.Trip[i].LegList.Leg[1].Origin %}
{% set name = json.name.split()[1] %}
{% set timestamp = json.date + 'T' + json.time %}
{% set offset = 60 * 15 %}
{% if (as_timestamp(timestamp) - as_timestamp(now())) > offset and not ns.found %}
{% set ns.found = true %}
{% set ns.timestamp = as_timestamp(timestamp) | timestamp_custom('%H:%M') %}
{% set ns.name = name %}
{% endif %}
{% endfor %}
{% if ns.timestamp != null and ns.name != null %}
Nästa buss går {{ ns.timestamp }} från {{ ns.name }}
{% endif %}
{% endif %}
scan_interval: 86400

- platform: rest
name: hass_release_notes
Expand Down
7 changes: 1 addition & 6 deletions include/template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -205,12 +205,7 @@ template:
Kan inte läsa in väderinformation...
{%- endif %}
transport: |
{% set entity_id = 'sensor.skanetrafiken' %}
{% if states(entity_id) != 'unknown' %}
{%- set date = state_attr(entity_id, 'date') + ' ' + state_attr(entity_id, 'time') %}
{%- set time = as_timestamp(date) | timestamp_custom('%H:%M') -%}
Nästa buss går {{ time }} från {{ state_attr(entity_id, 'name').split()[1] }}
{% endif %}
{{ states('sensor.skanetrafiken') }}
laundry: |
{%- set start_time = state_attr('calendar.tvatta', 'start_time') %}
{% if start_time != None %}
Expand Down

0 comments on commit af5284a

Please sign in to comment.