Combining state_filter in Lovelace badges

Hi there.

I’m trying to display a badge at the top of my default view in Lovelace that shows what bins are being collected this week (recycling or refuse). I feel like I’m close to getting it working how I want, but I can’t quite figure it out.

I use Node-RED to scrape the bin collection info from my local council website and publish it to an MQTT topic:

{
  "type": "recycling",
  "attributes": {
    "date": "2020-10-26",
    "day": "Monday",
    "countdown": 1
  }
}

I then have an MQTT sensor in Home Assistant with the state set to either recycling or refuse, and three attributes that I was hoping to use with setting up the badge:

sensor:
  - platform: mqtt
    state_topic: "bins/home"
    name: "Bin Collection"
    qos: 0
    value_template: '{{ value_json.type }}'
    json_attributes_topic: "bins/home"
    json_attributes_template: "{{ value_json.attributes|to_json }}"
    force_update: true

This creates a sensor that looks like this:

image

To create the badge, I thought I’d use the entity-filter badge (Badges docs) two switch between two different badges depending on the state of sensor.bin_collection (either recycling or refuse).

image

badges:
  - type: entity-filter
    entities:
      - entity: sensor.bin_collection
        name: Recycling
        image: /local/bins_recycling.png?v=1
        state_filter:
          - recycling
      - entity: sensor.bin_collection
        name: Refuse
        image: /local/bins_refuse.png?v=1
        state_filter:
          - refuse

This works perfectly fine, and the badges change depending on the sensor state. However, I would only like the bin colleciton badge to display when there are fewer than three days to go until bin collection day.

I tried adding the state_filter part to the badge definition, but it seems to be ignored and the bin badge displays all the time.

badges:
  - type: entity-filter
    state_filter:
      - attribute: countdown
        operator: <
        value: 3
    entities:
      - entity: sensor.bin_collection
        name: Recycling
        image: /local/bins_recycling.png?v=1
        state_filter:
          - recycling
      - entity: sensor.bin_collection
        name: Refuse
        image: /local/bins_refuse.png?v=1
        state_filter:
          - refuse

In the Examples section of the docs, it shows a state_filter for the badge, as well as separate state_filters for individual entities.

What am I doing wrong? It seems as if you can’t combine state_filters as “AND” conditions, and can only combine them as “OR” conditions.

Thanks for reading,

Chris

OK, figured out a way round it. I realised I could just create two template sensors - one for recycling and one for refuse - that output true if the countdown until bin day is less than 3. These are then shown/hidden by an entity-filter badge:

Template sensor configuration:

- platform: template
  sensors:
    bins_home_recycling:
      value_template: >-
        {% set type = states("sensor.bin_collection") %}
        {% set countdown = state_attr("sensor.bin_collection", "countdown") %}
        {% if countdown < 3 %}
          {% if type == "recycling" %}
            true
          {% else %}
            false
          {% endif %}
        {% endif %}
    bins_home_refuse:
      value_template: >-
        {% set type = states("sensor.bin_collection") %}
        {% set countdown = state_attr("sensor.bin_collection", "countdown") %}
        {% if countdown < 3 %}
          {% if type == "refuse" %}
            true
          {% else %}
            false
          {% endif %}
        {% endif %}

… and the badge configuration:

badges:
  - type: entity-filter
    state_filter:
      - 'true'
    entities:
      - entity: sensor.bins_home_recycling
        name: Recycling
        image: /local/bins_recycling.png?v=1
      - entity: sensor.bins_home_refuse
        name: Refuse
        image: /local/bins_refuse.png?v=1

It’s not answered my question about combining state_filters but it has solved my problem of how to display a variable badge for a set amount of time.

Chris

1 Like