Bouni / abfallplus

AbfallPlus component for Home Assistant
MIT License
14 stars 1 forks source link

Weekdays translation #4

Closed rkl70 closed 2 years ago

rkl70 commented 2 years ago

First of all this thing is great!

But is there any way to translate the weekday to german?

Bouni commented 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

rkl70 commented 2 years ago

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 ...

Bouni commented 2 years ago

can you post your current sensor.yaml please?

rkl70 commented 2 years ago

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: image

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")}}

image

image

rkl70 commented 2 years ago

What I'm aiming at is a card that shows me the upcoming Garbage collection: image

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
Bouni commented 2 years ago

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:

image

Feel free to give it a better name than restmull_deutsch 😉

rkl70 commented 2 years ago

ok, but is there a way to add the attribute "remaining" to the new sensor?

Bouni commented 2 years ago

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') }}

image

rkl70 commented 2 years ago

YESS!!

image

Had some issues with the correct syntax for adding the attributes. Thank you so much!