Is there a way to decide if Total clean count entity is even or odd?

My Roborock S7 has a entity called “Total clean count”. My idea was to send the roboter after every second cleaning directly to the bin to easily clean him.

Is there a way to get if the Total clean count is even or odd?

Maybe something like this:

  • Get float of Total clean count
  • Divide by 2
  • Check if it has no remainder
  • If yes, even. If not, odd

Many thanks!

you can use the modulus operator in a template:

{{ (states('sensor.your_clean_count') | float % 2) == 0 }} 

if true then the number is even. if false it’s odd

1 Like

You can also use


      {{ states('sensor.…')|int(0) is even }}

2 Likes

Perfect that works! Some follow-up questions abput this code.

I guess that I could replace / 2 to / x to change it to do something every x days right?

Also what does the % in float % 2 do exactly? I tried googling it but couldnt find a answer.

And what does int(0) do?

The Jinja documentation says:

%

Calculate the remainder of an integer division. {{ 11 % 7 }} is 4.

This is based on the Euclidean algorithm. The number 11 can be divided by 7 only once, the remainder is 4.

Another examples:


{{ 456 % 321 }} = 135

{{ 18 % 4 }} = 2

The number 456 can be divided by 321 once, rest/remainder is 135.

The number 18 can be divided by 4 four times, rest/remainder is 2.

Even is true, if you can divide a number and the result is a whole number (= remainder is zero):
Is it true, that the number 18 is even?


{{ 18 % 2 }} = 0 ## Yeah!
{{ 18 / 2 }} = 9, rest: 0

Is it true, that the number 17 is even?


{{ 17 % 2 }} = 1 ## Nope
{{ 17 / 2 }} = 8, rest: 1

1 Like

The filter |int converts a value into an integer, whereas |float converts a value into a floating point number.

By using int(0) or float(0) you ensure that the template will be rendered without any log warnings. Details see here: Templating changes in 2021.10.0

it’s easier if you break it up a bit.

`{{ (states('sensor.your_clean_count') | float.......

“float” is a filter that converts the output of “states(‘sensor.your_clean_count’)” to a floating point number - it has decimals in it (an integer is a whole number)

all states in HA are strings. Even if the value looks like a number it’s actually a string.

you need to convert the string into a number (either a float or integer) in order to do math on the values.

..... % 2).......

takes the modulus of the converted number.

so there are three independent functions going on in that template…

first convert "states(‘sensor.your_clean_count’) " to a float using the | float filter. then take the modulus of the resulting floating point number using % 2. then compare the result to 0.

1 Like