Closed rkl70 closed 2 years ago
The only way as far as I can tell is to use a template sensor like this:
template:
- sensor:
- name: "Bio Müll"
state: >
{% set days = ["Montag", "Dienstag", "Mittwoch", "Donnestag", "Freitag", "Samstag", "Sonntag"] %}
{% set date = strptime(states.sensor.bio_tonne.state, "%A, %d.%m.%Y") %}
{{days[date.weekday()]}} {{date.strftime("%d.%m.%Y")}}
I've not tested that but it should work
Thanks!
I've tested this code in developer-tools/template
and there it works like a charm.
Now, I'm just struggling on where to put that:
does it go into configuration.yaml
, in a special file, or into the code of the card itself?
to tidy things up a bit, i have included several .yaml-files in my configuration.yaml, like this:
# Includes
group: !include groups.yaml
automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml
sensor: !include sensor.yaml
shell_command: !include shell_commands.yaml
template: !include templates.yaml
So, do I have to put it in sensor.yaml
, templates.yaml
, or where?
(totally confused right now ...
can you post your current sensor.yaml
please?
sure:
- platform: abfallplus
name: Restmüll
key: 248deacbb49b06e868d29cb53c8ef034
municipality: 2339
street: 2002
trash_ids: "27,28,17,48,67,33,101,42"
pattern: Restabfall
- platform: abfallplus
name: Altpapier
key: 248deacbb49b06e868d29cb53c8ef034
municipality: 2339
street: 2002
trash_ids: "27,28,17,48,67,33,101,42"
pattern: Altpapier
- platform: abfallplus
name: Gelbe Tonne
key: 248deacbb49b06e868d29cb53c8ef034
municipality: 2339
street: 2002
trash_ids: "27,28,17,48,67,33,101,42"
pattern: Gelber Sack
- platform: abfallplus
name: Biomüll
key: 248deacbb49b06e868d29cb53c8ef034
municipality: 2339
street: 2002
trash_ids: "27,28,17,48,67,33,101,42"
pattern: Bioabfall
- platform: abfallplus
name: Grünschnitt
key: 248deacbb49b06e868d29cb53c8ef034
municipality: 2339
street: 2002
trash_ids: "27,28,17,48,67,33,101,42"
pattern: Grünabfall
- platform: abfallplus
name: Sperrmüll
key: 248deacbb49b06e868d29cb53c8ef034
municipality: 2339
street: 2002
trash_ids: "27,28,17,48,67,33,101,42"
pattern: Sperrmüll
- platform: abfallplus
name: Problemabfall
key: 248deacbb49b06e868d29cb53c8ef034
municipality: 2339
street: 2002
trash_ids: "27,28,17,48,67,33,101,42"
pattern: Problemabfall
I tried putting it as a template sensor (in template.yaml
):
#template:
- sensor:
- name: "Restmüll"
state: >
{% set days = ["Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag", "Sonntag"] %}
{% set date = strptime(states("sensor.restmull"), "%A, %d.%m.%Y") %}
{{days[date.weekday()]}}, {{date.strftime("%d.%m.%Y")}}
which only gives me a second sensor sensor.restmull_2
, which has only the (at least correct) date as a state:
when I try to add the state >
part of the template to the sensors in sensor.yaml
, it throws an error:
- platform: abfallplus
name: Restmüll
key: 248deacbb49b06e868d29cb53c8ef034
municipality: 2339
street: 2002
trash_ids: "27,28,17,48,67,33,101,42"
pattern: Restabfall
state: >
{% set days = ["Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag", "Sonntag"] %}
{% set date = strptime(states("sensor.restmull"), "%A, %d.%m.%Y") %}
{{days[date.weekday()]}}, {{date.strftime("%d.%m.%Y")}}
What I'm aiming at is a card that shows me the upcoming Garbage collection:
Here is the card's code:
type: entity-filter
entities:
- entity: sensor.restmull
icon: mdi:trash-can-outline
icon_color: gray
- entity: sensor.biomull
icon: mdi:biohazard
icon_color: brown
- entity: sensor.gelbe_tonne
icon: mdi:recycle
icon_color: yellow
- entity: sensor.altpapier
icon: mdi:recycle-variant
icon_color: green
- entity: sensor.sperrmull
icon: mdi:barn
icon_color: grey
- entity: sensor.grunschnitt
icon: mdi:barley
icon_color: green
- entity: sensor.problemabfall
icon: mdi:alert-outline
icon_color: red
state_filter:
- attribute: remaining
operator: <
value: 8
card:
type: entities
title: Müllabfuhr
Adding this to my configuration.yaml works for me:
template:
- sensor:
- name: Restmüll Deutsch
state: >
{% set days = ["Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag", "Sonntag"] %}
{% set state = states("sensor.restmull") %}
{% set date = strptime(state, "%A, %d.%m.%Y") %}
{{ days[date.weekday()] }}, {{ date.strftime("%d.%m.%Y") }}
Gives me:
Feel free to give it a better name than restmull_deutsch 😉
ok, but is there a way to add the attribute "remaining" to the new sensor?
Sure!
template:
- sensor:
- name: Restmüll Deutsch
state: >
{% set days = ["Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag", "Sonntag"] %}
{% set state = states("sensor.restmull") %}
{% set date = strptime(state, "%A, %d.%m.%Y") %}
{{ days[date.weekday()] }}, {{ date.strftime("%d.%m.%Y") }}
attributes:
remaining: >
{{ state_attr("sensor.restmull", 'remaining') }}
YESS!!
Had some issues with the correct syntax for adding the attributes. Thank you so much!
First of all this thing is great!
But is there any way to translate the weekday to german?