If else structure doesn't work

                  - type: template
                    entity: sensor.energy_socket_5c2faf0313f6_active_power
                    icon: >
                      [[[
                      if (states['sensor.energy_socket_5c2faf0313f6_active_power'].state > 2) {
                        mdi:dishwasher-alert
                      } else {
                        mdi:dishwasher
                      }
                      ]]]

I don’t get errors on the code, but it does not work. Where did I go wrong ? (again :pensive:)

icon: >
  {{ "mdi:dishwasher-alert" if states("sensor.energy_socket_5c2faf0313f6_active_power") | int > 2 else "mdi:dishwasher" }}
1 Like

just for info. what is the difference between float and int ?

float is for when you want decimal numbers

Use Template from Developer Tools to make your tests.

Also, you used JavaScript. That’s typically used by some third-party UI components. The backend uses Jinja.

Where did you get that code to begin with?

In this case, there wouldn’t have been any errors: there just wouldn’t be an icon. Basically, all the JS would be text in the icon field, but not in the mdi:... format, so it just got ignored.

I did. Result was string, and no error. but I dont know what to do when the result is string

Ok. Thanks. I just dont know the difference between the languages. So I copy paste here and there and hope it works…

I meant the other question about float vs int

https://jinja.palletsprojects.com/en/latest/templates/#jinja-filters.float
https://jinja.palletsprojects.com/en/latest/templates/#jinja-filters.int