Code to identify which sensor has the lowest value

So I have three sensors, that identify how many days it is till each bin day (three separate days)

sensor.black_bin
sensor.green_bin
sensor.brown_bin

What I would like to do is create a custom button that shows the bin icon, with it the color of the next bin, with a state (or label) of the number of days till then.

I’m thinking maybe I could configure another sensor that looks at the three above sensors, and sets it’s state to indicate which one has the lowest value… such as ‘black’ or ‘green’ or ‘brown’. I could then use that state to help with logic to configure the icon colour and state value on a card such as custom-button.

Could anyone assist with what code/logic to use to create such a sensor? Or have any other/better ideas to achieve what I’m attempting?

Thanks for any help or ideas!

you need to create a template sensor and use the following template for the state:

{{ [states('sensor.black_bin') | float(0), states('sensor.green_bin') | float(0), states('sensor.brown_bin') | float(0)] | min }}

You could use a Min/Max helper, which will let you set up everything in the UI

Or you could do that too…

I tend to not use the UI. :wink:

Yeah, I’ve been using HA for a while, certainly before a lot of the UI tools were avaiable, and usually default to the yaml approach as well. I have just recently update a whole heap of climate sensors in rooms to use multiple sensors and average them, and just found the UI way easier.

1 Like

Thanks @finity and @dgaust, appreciate the support.

The above would def be useful to create a sensor to see how many days till the next bin collection.

However, and I probably didn’t explain it very well, my main sticking point is how to identify which bin is next… so that I can set the color of the bin icon on my card to match.

Don’t suppose you guys have any thoughts?

Thanks again!

You are entirely correct :wink:

You could test something like this, to see if it works, I just don’t have those types of sensors, so just mocked this up in yaml (fingers crossed it’s correct).

There are likely cleaner ways to do this though, but its a start.

{% if states('sensor.black_bin') | int < states('sensor.green_bin') | int  and states('sensor.black_bin') | int  < states('sensor.brown_bin') | int %}
   Black Bin Out
{% elif states('sensor.green_bin') | int < states('sensor.black_bin') | int  and states('sensor.green_bin') | int  < states('sensor.brown_bin') | int %}
   Green Bin Out
{% elif states('sensor.brown_bin') | int < states('sensor.black_bin') | int  and states('sensor.brown_bin') | int  < states('sensor.green_bin') | int %}
   Brown Bin Out
{% else %}
   Something went wrong
{% endif %}

Thanks @dgaust, thats great.