Comparing two sensors, output values from smallest, display in Lovelace UI

Hi there,

I’m using Waste Collection Schedule with two sources. From my understanding this means that I cannot create one “next_pickup” sensor within Waste Collection Schedule in the config.yaml file (as I can within only one source).

However in Lovelace I’d like to have a markup line read "next pickup is “waste type” on “date of pickup” (in “X days”). As long as I’m only looking at one of the sources, this works fine. But how can I reference two sources?

As of now I’ve created a “dummy” variable that only outputs the number of days as a number for each of my three types of waste. My thinking was, that I could somehow compare the value of these three numbers, determine the smallest number and then output the value of the relevant sensor for the smallest value (i.e. the next garbage to be picked up) and use that in the markup in Lovelace. But that’s where I am stuck (comparing the values/outputting the correct information).

Anybody have any ideas for how to do this? If my approach isn’t the best, let me know - happy for any alternative approaches.

Thanks!

Here is an example you can use to get started:

{% set date_1 = states.your_datetime_object_1.attributes.timestamp %}
{% set date_2 = states.your_datetime_object_2.attributes.timestamp %}
{% set date_3 = states.your_datetime_object_3.attributes.timestamp %}
{{([date_1, date_2, date_3]| sort)[0] | timestamp_custom('%Y-%m-%d, %H:%M:%S')}}

that will give you the date and time of the earliest value.

it will work as long as the datetime object has a timestamp attribute but you haven’t really given to many details on those sources/sensors.

Thanks!

My “setup” is as follows:
In waste collection schedule I generate a “next pickup” sensor. I use that sensor to display in the Dashboard a markdown card that says “next pickup is ‘garbage type’ on ‘day of week’, ‘month’, ‘day’, ‘year’ (‘in X days’)”.

The problem (from what I understand) is that this only works using one sensor. At least I haven’t figured out how to pull in data from two sensors. And within waste collection schedule I can’t generate a “next pickup” sensor using two sources of waste pickup (two companies, different data sources).

My YAML looks as follows (private information excluded using “XXX”)

config.yaml

waste_collection_schedule:
  sources:
    - name: bsr_de
      args:
        abf_strasse: "XXX"
        abf_hausnr: XXX
      customize:
        - type: "Abholung Hausmüll"
          alias: Hausabfall
          icon: mdi:trash-can
        - type: "Abholung Biogut"
          alias: Biogut
          icon: mdi:recycle
    - name: abfall_io
      args:
        key: "9583a2fa1df97ed95363382c73b41b1b"
        f_id_kommune: 3227
        f_id_strasse: XXX
        f_id_strasse_hnr: XXX
        f_abfallarten:
          - 127
      customize:
        - type: "127"
          alias: Wertstofftonne
          icon: mdi:recycle-variant
  fetch_time: "04:00"
  day_switch_time: "08:00"

sensor:
# the first three sensors ("XXX_next") only generate the number of days until the next pickup with this type of waste. Intention: Use this to figure out what waste is being picked up next.
  - platform: waste_collection_schedule
    source_index: 1
    name: Wertstoff_next
    details_format: "upcoming"
    value_template: "{{value.daysTo}}"
    types:
      - Wertstofftonne

  - platform: waste_collection_schedule
    source_index: 0
    name: Restmuell_next
    details_format: "upcoming"
    value_template: "{{value.daysTo}}"
    types:
      - Hausabfall

  - platform: waste_collection_schedule
    source_index: 0
    name: Biomuell_next
    details_format: "upcoming"
    value_template: "{{value.daysTo}}"
    types:
      - Biogut

  - platform: waste_collection_schedule
    source_index: 1
    name: Wertstoff
    details_format: "upcoming"
    value_template: >-
      {% if value.daysTo == 0 %}
      HEUTE
      {% elif value.daysTo == 1 %}
      MORGEN
      {% else %}
      in {{value.daysTo}} Tagen
      {% endif %}
    types:
      - Wertstofftonne

  - platform: waste_collection_schedule
    source_index: 0
    name: Restmuell
    details_format: "upcoming"
    value_template: >-
      {% if value.daysTo == 0 %}
      HEUTE
      {% elif value.daysTo == 1 %}
      MORGEN
      {% else %}
      in {{value.daysTo}} Tagen
      {% endif %}
    types:
      - Hausabfall

  - platform: waste_collection_schedule
    source_index: 0
    name: Biomuell
    details_format: "upcoming"
      {% if value.daysTo == 0 %}
      HEUTE
      {% elif value.daysTo == 1 %}
      MORGEN
      {% else %}
      in {{value.daysTo}} Tagen
      {% endif %}
    types:
      - Biogut

# This is the "next pickup" sensor. Right now it's pulling data from index source 0 (bsr).
  - platform: waste_collection_schedule
    name: abfallnaechster
    details_format: "upcoming"
    value_template: >-
      {% if value.daysTo == 0 %}
      HEUTE
      {% elif value.daysTo == 1 %}
      MORGEN
      {% else %}
      in {{value.daysTo}} Tagen
      {% endif %}

Markdown code in Lovelace/Dashboard

type: entities
entities:
  - type: custom:hui-element
    label: Muellabholung
    card_type: vertical-stack
    cards:
      - type: markdown
        content: >-
          ## <ha-icon icon="mdi:trash-can"></ha-icon> Müllabholung


          Die nächste Abholung ist der <b>{{
          states.sensor.abfallnaechster_2.attributes.values() | first |
          replace("Hausabfall", "Restmüll") | replace("Biomuell", "Biomüll") |
          replace("Biogut", "Biomüll") }}</b>, am <b>{{
          states.sensor.abfallnaechster_2.attributes | first | as_timestamp |
          timestamp_custom("%A</b>, den %d. %b. %Y") | replace("Monday",
          "Montag") | replace("Tuesday", "Dienstag") | replace("Wednesday",
          "Mittwoch") | replace("Thursday", "Donnerstag") | replace("Friday",
          "Freitag") | replace("Saturday", "Samstag") | replace("Sunday",
          "Sonntag") | replace("Mar", "Mrz") | replace("May", "Mai") |
          replace("Oct", "Okt") | replace("Dec", "Dez") }} ({{
          states("sensor.abfallnaechster_2")}}).
      - type: custom:collapsable-cards
        title: Weitere Abholungen
        cards:
          - type: entities
            entities:
              - entity: sensor.restmuell
              - entity: sensor.biomuell
              - entity: sensor.wertstoff
            show_header_toggle: false
            state_color: false

Here’s what that then looks like. In theory it works, just in a limited fashion. Specifically, the “Wertstoff”-waste type is only listed in the entity list but would not be included in the markdown text (the sentence) if it were the next waste type to be picked up.

I hope this makes sense? Thanks for your help!!

Can you provide me with a working config without the XXX values so I can test it to see what the sensors actually look like? It can be any valid address that will provide me with real sensor data. It doesn’t have to be your address. Your neighbors address down the street will work for my purposes. I promise I won’t tell them you used their address. :wink:

I just used your strings from the example, you need to replace them with states(’…’).

But if we remove the in and replace Tagen with a comma then we can split the string, make it integer and get the minimum value.

{{ (("in 7 Tagen" ~ "in 8 Tagen" ~ "in 12 Tagen") | replace("in ", "") | replace(" Tagen", ","))[:-1].split(",") |map('int') | min }}

A more direct method is to use regex:

{{ ("in 7 Tagen" ~ "in 8 Tagen" ~ "in 12 Tagen") | regex_findall('\d+') |map('int') | min }}

Sure thing.

waste_collection_schedule:
  sources:
    - name: bsr_de
      args:
        abf_strasse: "Glaskrautstr., 13503 Berlin (Reinickendorf)"
        abf_hausnr: 4
      customize:
        - type: "Abholung Hausmüll"
          alias: Hausabfall
          icon: mdi:trash-can
        - type: "Abholung Biogut"
          alias: Biogut
          icon: mdi:recycle
    - name: abfall_io
      args:
        key: "9583a2fa1df97ed95363382c73b41b1b"
        f_id_kommune: 3227
        f_id_strasse: 5801
        f_id_strasse_hnr: 183655
        f_abfallarten:
          - 127
      customize:
        - type: "127"
          alias: Wertstofftonne
          icon: mdi:recycle-variant
  fetch_time: "04:00"
  day_switch_time: "08:00"

sensor:
# the first three sensors ("XXX_next") only generate the number of days until the next pickup with this type of waste. Intention: Use this to figure out what waste is being picked up next.
  - platform: waste_collection_schedule
    source_index: 1
    name: Wertstoff_next
    details_format: "upcoming"
    value_template: "{{value.daysTo}}"
    types:
      - Wertstofftonne

  - platform: waste_collection_schedule
    source_index: 0
    name: Restmuell_next
    details_format: "upcoming"
    value_template: "{{value.daysTo}}"
    types:
      - Hausabfall

  - platform: waste_collection_schedule
    source_index: 0
    name: Biomuell_next
    details_format: "upcoming"
    value_template: "{{value.daysTo}}"
    types:
      - Biogut

  - platform: waste_collection_schedule
    source_index: 1
    name: Wertstoff
    details_format: "upcoming"
    value_template: >-
      {% if value.daysTo == 0 %}
      HEUTE
      {% elif value.daysTo == 1 %}
      MORGEN
      {% else %}
      in {{value.daysTo}} Tagen
      {% endif %}
    types:
      - Wertstofftonne

  - platform: waste_collection_schedule
    source_index: 0
    name: Restmuell
    details_format: "upcoming"
    value_template: >-
      {% if value.daysTo == 0 %}
      HEUTE
      {% elif value.daysTo == 1 %}
      MORGEN
      {% else %}
      in {{value.daysTo}} Tagen
      {% endif %}
    types:
      - Hausabfall

  - platform: waste_collection_schedule
    source_index: 0
    name: Biomuell
    details_format: "upcoming"
      {% if value.daysTo == 0 %}
      HEUTE
      {% elif value.daysTo == 1 %}
      MORGEN
      {% else %}
      in {{value.daysTo}} Tagen
      {% endif %}
    types:
      - Biogut

# This is the "next pickup" sensor. Right now it's pulling data from index source 0 (bsr).
  - platform: waste_collection_schedule
    name: abfallnaechster
    details_format: "upcoming"
    value_template: >-
      {% if value.daysTo == 0 %}
      HEUTE
      {% elif value.daysTo == 1 %}
      MORGEN
      {% else %}
      in {{value.daysTo}} Tagen
      {% endif %}

Unfortunately, this isn’t the actual issue. :frowning: This is why I implemented the “wastetype_next” sensors. Yes, your way is definitely more elegant, but I think mine achieves the same thing. Problem is, that I can’t pull data from both waste sources (BSR and abfall_io) into the “nächste abholung” (next pickup) sensor. It’s either one, or the other.

In that case I don’t understand the issue.
Both the title and the third paragraph is about comparing integers

So I’m pulling in data from two sources: BSR and abfall_io.
BSR automatically is defined as source “0” and abfall_io as source “1”.

In the sensor section I can pull in data from source “0” (bsr) OR source “1” (abfall_io).

This means that in the markdown part (in the dashboard), I can’t have it say “the next pickup is waste X on …” and have it consider all types of waste (both sources).

What I need is a way to basically create a dummy variable/sensor that pulls data from both sources and creates an overarching sensor that includes all three waste types (data from both sources).

So it is about comparing integers. But I would need more than just figuring out the smallest value (x days), rather I then need to have it pull the remaining data about the smallest sensor (the next garbage to be picked up).

Does that help?

Thanks!!

But your image shows you get both (?).
Abfall_io is the wertstoffe? and bsr has hausabfall and biogutt.
So you have entities for all three? or… ??

yes. but the “abfallnaechster” sensor only can pull data from one of the sources at the time. So I could use the “xxx_next” sensors to compare and find the smallest number, but then I still don’t know which type of waste sits behind the smallest integer (or what date it is). Or do I? Sorry. Thanks!

But what does the bsr sensor have in developer tools.
Doesn’t it have both number of days/dates or whatever?
Can’t you create two bsr sesors, something like:

waste_collection_schedule:
  sources:
    - name: bsr_de_biogut
      args:
        abf_strasse: "Glaskrautstr., 13503 Berlin (Reinickendorf)"
        abf_hausnr: 4
      customize:
        - type: "Abholung Biogut"
          alias: Biogut
          icon: mdi:recycle
    - name: bsr_de_haumüll
      args:
        abf_strasse: "Glaskrautstr., 13503 Berlin (Reinickendorf)"
        abf_hausnr: 4
      customize:
        - type: "Abholung Hausmüll"
          alias: Hausabfall
          icon: mdi:trash-can
    - name: abfall_io
      args:
        key: "9583a2fa1df97ed95363382c73b41b1b"
        f_id_kommune: 3227
        f_id_strasse: 5801
        f_id_strasse_hnr: 183655
        f_abfallarten:
          - 127
      customize:
        - type: "127"
          alias: Wertstofftonne
          icon: mdi:recycle-variant
  fetch_time: "04:00"
  day_switch_time: "08:00"

I’m just guessing. I don’t have a waste collection sensor so this could be terribly wrong.

That is possible.
The date is now() + timedelta(days= [min days]).
The type is finding the index of the list, so with [7,8,12] you would get 0 as the return. And since you know that 0 is the first sensor in the template that is known.
You just use the index and get the name from [ “Hausmüll”, “Biogut”, “Wertstofftonne”]

waste collection schedule generates entities that have the dates of the next couple of pick ups. So the values are something like this

the sensors I generate in the config.yaml file basically - I think - do what you’re looking at doing. So for example the dev. output of the biomuell_next sensor is:


(note how now the value is only the number of days, not the “in X Tagen”.)

So I already have entities / sensors that have the integer. I don’t need two sources (in fact I think that would make it harder). It’s about figuring out the smallest of the three values (that you’ve shown - I think) and then pulling that sensor’s remaining info into the markdown.

Honestly, it might be super easy and I’m just missing something. I’m not really super firm on YAML or coding - more a “learn by doing and learn by watching YouTube / reading things in forums” person. So it might be something where you say “duh” and I say “omg why didn’t I think of that”. :wink:

If the states are just numbers that you can access one by one, then try this:

{% set list = [states('sensor.xxxx') | int, states('sensor.yyyy') | int, states('sensor.zzzz') | int] %} 
{% set i =   list.index(list |min) %}
{{ i }} 
{{  [ "Hausmüll", "Biogut", "Wertstofftonne"][i] }} 
{{ as_timestamp(now() + timedelta(days=list|min)) | timestamp_custom("%A</b>, den %d. %b. %Y") }}

I don’t think it’s working.
In my implementation it’s pulling in the “Wertstofftonne” as being on Wed, August 24th. That’s not the case and that’s not listed in the relevant sensor. I fear that the date portion that you wrote does pull in the correct date (in this case Aug. 24th) but it’s not associating that date with the correct type of waste. Or am I missing something?

You have the sensors in the wrong order.
They need to match the list on line 4

got it, sorry.

But could I here still pull out the number of days until the pickup in markdown? I.e. in brackets after the “pickup is on day X” put “(in X days)”?

and now for me being a total noob (sorry)… how would I implement this for use in the dashboard?

Give me a few minutes, i need to get the kids in bed.
But I believe this can more or less be posted in the markdown card.
Some editing will be needed, like the translation of the day name and such.

take your time :slight_smile: thanks for your help!
I tried posting this in the markdown card. It didn’t do anything. Just kept the grey wheel turning so to speak. But I might well be doing something wrong.

Probably the </b> tag doing that if you don’t have an opening tag then it could do that.
I just copied your custom date format.

Another way is to create a custom sensor that has the string you want in the markdown card.
Then just output the state of this sensor in the markdown card.