Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
vingerha opened this issue Apr 29, 2024 · 6 comments
Open

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

vingerha opened this issue Apr 29, 2024 · 6 comments

Comments

@vingerha
Copy link
Owner

vingerha commented Apr 29, 2024

Describe the solution you'd like
At present, when tje config leads to a no-data situation, afetr a while the sensor goes to unknown, which can mean multiple things

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
Copy link
Contributor

FabienD74 commented May 15, 2024

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
Copy link
Owner Author

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
Copy link
Contributor

FabienD74 commented May 15, 2024

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
Copy link
Owner Author

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
Copy link
Contributor

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
Copy link
Owner Author

vingerha commented May 17, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants