Closed neforce closed 4 years ago
Good work @neforce! I think it's usefull you shared this! However it's a lot of code within Home Assistant. Maybe this is some nice feature to implement in this component itself as sensors with attributes like "volgende week woensdag". In this case you don't have to add complex code to Home Assistant.
Anyway nice job! You could als share it in the community: https://community.home-assistant.io/t/garbage-pickup-date-mijnafvalwijzer-nl-custom-component/34631
@Mariusthvdb probably also likes this 😃
HI, must be missing the obvious here, but what would be the difference with:
other than the layout which is a simple frontend thing?
HI, must be missing the obvious here, but what would be the difference with:
other than the layout which is a simple frontend thing?
I think day names (donderdag over een week, woensdag, morgen, vandaag) are nicer and easier to read/calculate than; 'Over 12 dagen'.
The code can be probably much simplified, but hey, it works :)
ok, if you want a day name, use the attribute timestamp as on these template sensors I made:
afvalwijzer_restafval_next:
friendly_name: Restafval
entity_picture_template: /local/afvalwijzer/restafval_saver.png
value_template: >
{{state_attr('sensor.afvalwijzer_restafval','days_until_collection_date')}}
attribute_templates:
date: >
{{as_timestamp(strptime(states('sensor.afvalwijzer_restafval'),'%d-%m-%Y'))
|timestamp_custom('%a %d %b')}}
timestamp: >
{{as_timestamp(strptime(states('sensor.afvalwijzer_restafval'),'%d-%m-%Y'))}}
and recalculate the day from that, or, even easier use:
{{as_timestamp(strptime(states('sensor.afvalwijzer_restafval'),'%d-%m-%Y'))
|timestamp_custom('%a')}}
and a translate template if you want it in Dutch ;-) you are way overcomplicating things...
btw, here's one to play with and adapt if you'd like.
volgende_afval_ophaal_dag:
friendly_name: Volgende afval-ophaal dag
entity_id: sensor.afvalwijzer_first_next_date
value_template: >
{%- set tracker_timestamp = as_timestamp(strptime(states('sensor.afvalwijzer_first_next_date'),'%d-%m-%Y')) %}
{% set months = ['Januari','Februari','Maart','April','Mei','Juni','Juli','Augustus','September','Oktober','November','December'] %}
{%- set wdays = ['Zondag','Maandag','Dinsdag','Woensdag','Donderdag','Vrijdag','Zaterdag'] %}
{%- set wday = tracker_timestamp|timestamp_custom('%w')|int %}
{%- set month = tracker_timestamp |timestamp_custom('%m')|int %}
{{tracker_timestamp|timestamp_custom(wdays[wday] + ' ' '%d' ' ' + months[month-1])}}
add a 'phrase' for the 'over 1/2/3 we(e)ken' (don't like over 2 week but that's probably a typo, sorry...) and you're probably set?
@Mariusthvdb probably also likes this 😃
yeah, but I would prefer the code in the integration ;-) this is way too complex in jinja for the extra added value of the day phrase (because that's the only thing we don't have yet, in Dutch) imho.
@Mariusthvdb if i quickly scan your code, it's moreorless the same i think? What my code does:
there are 3 lovelace icons, 1, 2, 3.
number one calls the first available trash type to empty (so, sorted upcoming trash dates, and picks the first one)
number one calls the first available trash date for this type, and calculates the day into a phrase (e.g. volgende week woensdag).
number two calls the second available trash type to empty (so, sorted upcoming trash dates, and picks the second one)
number two calls the second available trash date for this type, and calculates the day into a phrase (e.g. volgende week donderdag).
and so on.
This was all build for version 4.X, maybe some things can be simplified now for version 5.X.
the hard part was;
'Let's say it's monday 12 october, and PMD will be emptied on friday 23', we'll need to check if friday 23 is in the current week, if so, is it on the first available friday, or is a week later? And more of these 'questions'.
It was quite hard and digged to deep into it i think that i lost sometimes overview.
But again, it does what i want :)
if you only need the representation in the frontend, have a look at this:
- type: custom:auto-entities
card:
type: entities
title: Volgende ophaaldata
filter:
include:
- entity_id: sensor.afvalwijzer_*_next
options:
type: custom:template-entity-row
state: >
{{state_attr(config.entity,'date')}}
secondary: >
{% set count = states(config.entity)|int %}
{% set unit = 'Dag' if count == 1 else 'Dagen' %}
{% if count == 0 %} Vandaag
{% else %} Over {{count}} {{unit}}
{% endif %}
cause that's all I use in the card. You could adjust the jinja here somewhat, to change the lines to what you like, without having to code that huge package of sensors, and triple that for all sensors.
this ones easier for the day translation:
{% set dagen =
{ 'Mon': 'Maandag',
'Tue': 'Dinsdag',
'Wed': 'Woensdag',
'Thu': 'Donderdag',
'Fri': 'Vrijdag',
'Sat': 'Zaterdag',
'Sun': 'Zondag'} %}
{% set state = state_attr('sensor.afvalwijzer_papier_next','date').split(' ')[0] %}
{% set dag = dagen[state] if state in dagen else state %}
{{dag}}
nog een beetje aangepast (some further refinements):
- type: custom:auto-entities
card:
type: entities
title: Volgende ophaaldata
filter:
include:
- entity_id: sensor.afvalwijzer_*_next
options:
type: custom:template-entity-row
state: >
{{state_attr(config.entity,'date')}}
secondary: >
{% set count = states(config.entity)|int %}
{% set day = state_attr(config.entity,'day') %}
{% set unit = 'Dag' if count == 1 else 'Dagen' %}
{% if count >= 14 %} {% set phrase = day + ' over 2 weken' %}
{% elif count >= 7 %} {% set phrase = 'Volgende week ' + day %}
{% elif count >= 3 %} {% set phrase = 'komende ' + day %}
{% elif count == 2 %} {% set phrase = day + ', overmorgen' %}
{% elif count == 1 %} {% set phrase = 'morgen, ' + day %}
{% else %} {% set phrase = 'Vandaag, ' + day %}
{% endif %}
{{phrase}} {% if count != 0 %} ({{count}} {{unit}}) {% endif %}
sort:
method: state
numeric: true
en het wordt al meer wat je zoekt denk ik (getting close to what you are looking for):
Of course, play around with this template.
I did add the attribute 'day' to the sensors, to make it easy in the Lovelace card
afvalwijzer_restafval_next:
friendly_name: Restafval
entity_picture_template: /local/afvalwijzer/restafval_saver.png
value_template: >
{{state_attr('sensor.afvalwijzer_restafval','days_until_collection_date')}}
attribute_templates:
day: >
{{as_timestamp(strptime(states('sensor.afvalwijzer_restafval'),'%d-%m-%Y'))
|timestamp_custom('%A')}}
date: >
{{as_timestamp(strptime(states('sensor.afvalwijzer_restafval'),'%d-%m-%Y'))
|timestamp_custom('%a %d %b')}}
timestamp: >
{{as_timestamp(strptime(states('sensor.afvalwijzer_restafval'),'%d-%m-%Y'))}}
Bedankt voor de aanvulling @Mariusthvdb! Ik denk dat het mooi is dat mensen dit kunnen vinden! Thanks voor jullie bijdrage!
here's the translated version:
- type: custom:auto-entities
card:
type: entities
title: Volgende ophaaldata
filter:
include:
- entity_id: sensor.afvalwijzer_*_next
options:
type: custom:template-entity-row
state: >
{{state_attr(config.entity,'date')}}
secondary: >
{% set count = states(config.entity)|int %}
{% set day = state_attr(config.entity,'day') %}
{% set dagen =
{'Monday': 'Maandag',
'Tuesday': 'Dinsdag',
'Wednesday': 'Woensdag',
'Thursday': 'Donderdag',
'Friday': 'Vrijdag',
'Saterday': 'Zaterdag',
'Sunday': 'Zondag'} %}
{% set dag = dagen[day] if day in dagen else day %}
{% set unit = 'Dag' if count == 1 else 'Dagen' %}
{% if count >= 14 %} {% set phrase = dag + ' over 2 weken' %}
{% elif count >= 7 %} {% set phrase = 'Volgende week ' + dag %}
{% elif count >= 3 %} {% set phrase = 'komende ' + dag %}
{% elif count == 2 %} {% set phrase = dag + ', overmorgen' %}
{% elif count == 1 %} {% set phrase = 'morgen, ' + dag %}
{% else %} {% set phrase = 'Vandaag, ' + dag %}
{% endif %}
{{phrase}} {% if count != 0%} ({{count}} {{unit}}) {% endif %}
sort:
method: state
numeric: true
of course, this is for the secondary line, if you want the state to be translated, you'd have to use another template for that. Or wait for the date to be localized some day in core HA.
Or have the CC have a localize option, which would be cool btw ;-)
here's the translated version:
- type: custom:auto-entities more code
of course, this is for the secondary line, if you want the state to be translated, you'd have to use another template for that. Or wait for the date to be localized some day in core HA.
Or have the CC have a localize option, which would be cool btw ;-)
Yeah, that's almost what i have now:
type: 'custom:auto-entities'
card:
type: entities
title: Container bij de weg
filter:
include:
- entity_id: sensor.afvalwijzer_*_next
options:
type: 'custom:template-entity-row'
state: >
{% set dagen =
{ 'Monday': 'Maandag',
'Tuesday': 'Dinsdag',
'Wednesday': 'Woensdag',
'Thursday': 'Donderdag',
'Friday': 'Vrijdag',
'Saturday': 'Zaterdag',
'Sunday': 'Zondag'} %}
{% set count = states(config.entity)|int %} {% set day =
state_attr(config.entity,'day') %} {% set day = dagen[day] %}
{% set unit = 'Dag' if count == 1 else 'dagen' %}
{% if count > 14 %}
{% set phrase = day + ' over twee weken' %}
{% elif count >= 7 and count <= 14 %}
{% set phrase = 'Volgende week ' + day|lower %}
{% elif count >= 3 and count <= 7 %}
{% set phrase = day %}
{% elif count == 2 %}
{% set phrase = 'Overmorgen' %}
{% elif count == 1 %}
{% set phrase = 'Morgen' %}
{% else %}
{% set phrase = 'Vandaag' %}
{% endif %}
{{phrase}}
sort:
method: state
numeric: true
count: 3
Great! Next step is to transform this into a different view (like the image in the OP) :)
Next step is to transform this into a different view (like the image in the OP)
this is a config for the secondary-row in a template entity row created by auto-entities.... cant really transform that into a set of picture-elements.
Unless I am misunderstanding what you're saying here. For me the combination is optimal having it all in 1 dashboard (and I really like to see the 3 days, always):
here's the translated version:
- type: custom:auto-entities card: type: entities title: Volgende ophaaldata filter: include: - entity_id: sensor.afvalwijzer_*_next options: type: custom:template-entity-row state: > {{state_attr(config.entity,'date')}} secondary: > {% set count = states(config.entity)|int %} {% set day = state_attr(config.entity,'day') %} {% set dagen = {'Monday': 'Maandag', 'Tuesday': 'Dinsdag', 'Wednesday': 'Woensdag', 'Thursday': 'Donderdag', 'Friday': 'Vrijdag', 'Saterday': 'Zaterdag', 'Sunday': 'Zondag'} %} {% set dag = dagen[day] if day in dagen else day %} {% set unit = 'Dag' if count == 1 else 'Dagen' %} {% if count >= 14 %} {% set phrase = dag + ' over 2 weken' %} {% elif count >= 7 %} {% set phrase = 'Volgende week ' + dag %} {% elif count >= 3 %} {% set phrase = 'komende ' + dag %} {% elif count == 2 %} {% set phrase = dag + ', overmorgen' %} {% elif count == 1 %} {% set phrase = 'morgen, ' + dag %} {% else %} {% set phrase = 'Vandaag, ' + dag %} {% endif %} {{phrase}} {% if count != 0%} ({{count}} {{unit}}) {% endif %} sort: method: state numeric: true
of course, this is for the secondary line, if you want the state to be translated, you'd have to use another template for that. Or wait for the date to be localized some day in core HA.
Or have the CC have a localize option, which would be cool btw ;-)
I was using the specific example from here. But it stopped working with the new version, probably because of the new timestamp class. Could anyone point me to the right direction what the breaking code is?
I believe it was already posted in the community, where these type of support questions belong, but here you go:
- type: custom:auto-entities
card:
type: entities
filter:
exclude:
- entity_id: sensor.afvalwijzer*next*
- entity_id: sensor.afvalwijzer_*orgen
- entity_id: sensor.afvalwijzer_v*
include:
- entity_id: sensor.afvalwijzer_*
options:
type: custom:template-entity-row
# set in customize
# image: >
# {% set type = config.entity.split('afvalwijzer_')[1] %}
# /local/afvalwijzer/{{type}}.png
state: >
{% set trash = config.entity %}
{% set today = state_attr(trash,'is_collection_date_today') %}
{% set future = state_attr(trash,'days_until_collection_date') %}
{% if future is none or
(is_state(trash,'geen') and future == 0) %} Tba
{% else %}
{% from 'easy_time.jinja' import month %}
{{as_datetime(states(trash)).day}}
{{month(as_datetime(states(trash)).month)}}
{% endif %}
secondary: >
{% set trash = config.entity %}
{% set today = state_attr(trash,'is_collection_date_today') %}
{% set future = state_attr(trash,'days_until_collection_date') %}
{% if future is none or
(is_state(trash,'geen') and future == 0) %} Nog niet gepubliceerd
{% else %}
{% set count =
state_attr(trash,'days_until_collection_date')|int(0) %}
{% set dagnummer = as_datetime(states(trash)).strftime('%w')%}
{% from 'easy_time.jinja' import weekday %}
{% set dag = weekday(dagnummer|int) %}
{% set unit = 'dag' if count == 1 else 'dagen' %}
{% if count >= 28 %} {% set phrase = dag + ' over 4 weken' %}
{% elif count >= 21 %} {% set phrase = dag + ' over 3 weken' %}
{% elif count >= 14 %} {% set phrase = dag + ' over 2 weken' %}
{% elif count >= 7 %} {% set phrase = 'Volgende week ' + dag %}
{% elif count >= 3 %} {% set phrase = 'Komende ' + dag %}
{% elif count == 2 %} {% set phrase = dag + ', overmorgen' %}
{% elif count == 1 %} {% set phrase = 'Morgen, ' + dag %}
{% else %} {% set phrase = 'Vandaag, ' + dag %}
{% endif %}
{{phrase}} {% if count != 0 %} ({{count}} {{unit}}) {% endif %}
{% endif %}
sort:
method: attribute
attribute: days_until_collection_date
numeric: true
note I am using the custom templates easytime which you can download via HACS
Hi Xirixiz,
I made a useful addition to this component. However, i don't know how to add this to the repo, but please take a look on this.
The addition only works (for me, for now) for papier, gft, pmd en rest.
Please see attached screenshot:
CODE SENSORS:
-- config/includes/garbage.yaml --
`- platform: template sensors: first_trash_date: value_template: >-
-- lovelace yaml --
`- cards:
If you need more info, or have any additions, please let me know.