vingerha / gtfs2

Support GTFS in Home Assistant GUI-only
https://github.com/vingerha/gtfs2
MIT License
65 stars 4 forks source link

[FEATURE]: sensor state should provide clear information #66

Closed vingerha closed 1 week ago

vingerha commented 2 months ago

Describe the solution you'd like At present, when the config leads to a no-data situation, after a while the sensor goes to unknown, which can mean multiple things and vagueness shuld be avoided

Describe alternatives you've considered Static start/end: currently shows first/next departure => unknown seems pretty accurate as it is not known when a next dpearture takes place, could change to something more descriptive as "next-departure-unknown" Local-stop sensor: currently shows stop name => stop name in a state is not really a good location, possibly change to number of collected departures

FabienD74 commented 1 month ago

Hello, The following exampleis working fine. I'm not using state to find out the "Stop name", but the departures, and it works fine. The following example search for gtfs2 entities having "phone" in the name. ( mine is something like "Bus around my phone") This example works in "Markdown Card"... at least for me ... :-) :-;

Regards


{%- set keyword = "phone" %}

{%- set unique_entities = states  |  list %}
{%- set ns = namespace(integrations = []) %}

{%- set ns.result = [] %}

{%- set ns.count_unique_entities= 0 %}
{%- set ns.processed_entities= 0 %}

{%- for entity in unique_entities %}

  {%- set ns.count_unique_entities = ns.count_unique_entities + 1 %} 
  {%- set l_indentifiers = device_attr(entity.entity_id, "identifiers") %}
  {%- set ns.l_indent1 = "" %}
  {%- set ns.l_indent2 = "" %}
  {%- if (l_indentifiers | string) != "None" %}
  {%- set ids = l_indentifiers | list | first| default %}
  {%- if ids and ids | length == 2 %}
  {%- set ns.l_indent1 = ids[0] %}
  {%- set ns.l_indent2 = ids[1] %}
  {%- endif %}
  {%- endif %}

  {%- if 1  == 2  %}
-
1 {{ entity.entity_id }} 
2 {{ entity.name }}
3 {{ entity.last_updated }}
4 {{ entity.domain }}
5 {{ entity.state }}
6 {{ entity.attributes }}
7 {{ entity.object_id }}
8 {{ entity.context }}
9 {{ l_indentifiers }}
  - 9a={{ ns.l_indent1 }}
  - 9b={{ ns.l_indent2 }}
  {%- endif %}

  {%- if ( entity.domain  == "sensor"  ) and ( ns.l_indent1 == "gtfs2")  %}
  {%- if ( keyword in ns.l_indent2 )  %}
  {%- set ns.processed_entities = ns.processed_entities + 1 %}
  {%- for entry_departure in  entity.attributes['next_departures_lines']  %}
  {%- set stop_name = entry_departure['stop_name'] %}
  {%- set departure = entry_departure['departure'] %}
  {%- set icon      = entry_departure['icon'] %} 
  {%- set direction = entry_departure['direction_id'] %} 
  {%- set route     = entry_departure['route'] %} 
  {%- set route_long = entry_departure['route_long'] %}
  {%- if direction == 1 %}
  {%- set terminus = route_long.split(" - ")[0] %}
  {%- else %}
  {%- set terminus = route_long.split(" - ")[-1] %}
  {%- endif %}

  {% set raw_string = stop_name + " -> " + terminus %}

  {% set item = { 'sort_key1': departure , 'sort_key2':'', 'icon': icon , 'route':route, 'departure':departure, 'stop_name':stop_name,'terminus': terminus } %}

  {% set ns.result = ns.result + [item] %}

  {%- endfor -%}
  {%- endif %}
  {%- endif %}
{%- endfor -%}

{%- set sorted = ns.result | sort(attribute='sort_key1')%}

{%- for item in sorted %}
  {{ item['departure'] + ' : '  + item['stop_name'] + ' -> ' + item['terminus']  + ' ('+ item['icon']+','+item['route'] + ')'}}
{%- endfor %}

`

vingerha commented 1 month ago

Thanks for this...in a few cases it is indeed related to the card and not refreshing. What I seemed to see is that a sensor where the state = stop name...and the state does not change ... it goes stale. The challenge I have is that I cannot pinpoint a 'when'. Current ideas are to change the state from stop_name to number of stops (from the attributes) so it changes along the day. HA behavior is not 100% known to me, I ma be able to develop bits/pieces...does not mean I am a devloper :)

FabienD74 commented 1 month ago

Well i have seen a strange behavior a couple of weeks (months?) ago. I was checking if state was valid or not... and if i remember at the end HA claimed it was valid, but the content was "unknown" or "unavailable" or something like that... :-( I sould try to reproduce it... I think i was comparing 2 timestamps to retreive "Next Bus In XX minutes"

vingerha commented 1 month ago

It is quite tricky to repoduce things as I often donot have the same source or the time to look at things that are not 'my data of interest'...anything you can provide may help me to understand where the code may go pearshaped :)

FabienD74 commented 1 month ago

Hi ,

I just found this https://community.home-assistant.io/t/how-to-make-template-sensor-with-as-timestamp-update-regularly/106084/2

In short it says "It is possible to force update even if sensor value doesn't change...."... may be it cound help with your update/refresh ....

On my side the issue is related to invalid timestamp when processing: {{as_timestamp (states('sensor.tec_place_renard')) | timestamp_custom("%H:%M") }} Fabien

vingerha commented 1 month ago

Thanks and I was indeed thinking about a state-value that changes, which is not the stop-name. ...still pondering EDIT: as adding a timestamp makes little sense uness it means something usable. But also the count of leaving buses is not good ...does not say a thing as to which bus and whereto

vingerha commented 1 week ago

@FabienD74 To keep the state usable I am thinking about this

vingerha commented 1 week ago

Tried it out but the 'dash' as separator is not nice so changed to underscore, then found out that I cannot easily count the #departures without going through a loop, so removed that

Liege Gare des Guillemins - Quai C _ 10:24
vingerha commented 1 week ago

Addressed with 0.4.8