Help with ïf then else" automation

I need some help again.

Again something probably quite easy but I can’t seem to find the solution.

I am building a bin day indicator with an OLED screen. It’s supposed to show the icon of the bin type that has to be put out the next day.

I have an integration that shows the “next day type” as text.

The OLED screen i am able to show 5 different images according to a slider in home assistant.

I want “number.afval_display_container” to be set to 1 - 5 according to the state of “sensor.areareiniging_morgen”.
When sensor is “PMD” number needs to be “2”
When sensor is “papier” number needs to be “3”
When sensor is “restafval” number needs to be “4”
When sensor is “GFT” number needs to be “5”
When sensor is “else” number needs to be “1”

Image number 1 is just a blank image to have nothing on the screen.

The if then statments are the easy part. I just cannot finout how to get the states and set the number.

It would be a lot easier for us to give an answer tailored to your case if you would post the automation as you have it currently.

You use State conditions to check the current state of your entity. In the Automation editor, State conditions are under the “Entity” group.

image

Number entities only support one Action, i.e. number.set_value. If you are using the Automation editor, just scroll down until you see “Number”:

image

FWIW, since you have more than two possible results, you will be better off using a Choose Action instead of the If/Then action.

This is also possible to do with a bit of Templating and it would be a significantly more compact automation.

Or a lookup table.

- action: number.set_value
  target: 
    entity_id: number.afval_display_container
  data:
    value: >
      {% set b = states("sensor.areareiniging_morgen") %}
      {% set t = {"PMD": 2,"papier": 3,"restafval": 4,"GFT": 5} %}
      {{ t.get(b, 1) }}

b is your bin sensor value and t is a lookup table. {{ t.get(b, 1) }} looks up the number you want in the table t based on the sensor value b and defaults to 1 if it can’t find a match.

This seems to work indeed. Thank you very much.

1 Like