Compare two numbers with different length

Hi there,

I want to compare two sensors for changing icon colors for different conditions.

{{ ((states["sensor.1"].state) > (states["sensor.2"].state)) }}

Quite an easy job but I got wrong booleans back.

Here are a few examples.

3 > 5 = false (fine)
7 > 5 = true (also fine)
10 > 5 = false (noooooo! why?)
60 > 5 = true (fine again)
400 > 5 = false (don’t do that to me :frowning: )

It seems that the first number decied the whole thing. How can I check the complete number?
I already checked the result type of both sensors with the developer tools and they are both “number”.

What am I doing wrong?

You’re comparing strings, not numbers.

All states are strings in HA.

{{ ((states["sensor.1"].state | int) > (states["sensor.2"].state | int )) }}

Or float if that is what you need

Thanks for this usefull information. The feedback from the developer tools are confusing at this time.
I’m getting the correct booleans back now with adding | int at the end.

After some trouble because of the syntax inside the custom_button_card ( | int doesn’t work here) I finally make it. Here is the solution if someone else got similar problems.

- type: "custom:button-card"
  template: chip_navigate
  variables:
	ulm_chip_navigate_path: /ui-lovelace-minimalist/energy/
	ulm_chip_navigate_icon: mdi:home-lightning-bolt
  styles:
	icon:
	  - color: >
		  [[[
			if (parseInt(states["sensor.grid_power"].state) > parseInt(states["sensor.house_power_total"].state)) return '#43a047';
			else if (parseInt(states["sensor.grid_power"].state) > 0 ) return '#ffa600';
			else return '#db4437';
		  ]]]

Thank you!

1 Like