It works good. But sometimes my lamps are at 100, sometimes at 50 brightness. It would be nice if I could find a way to adjust them simply half of the current value. What I am trying to do is something like this:
That isn’t necessary, for two reasons. First, the attribute is already an int, otherwise you couldn’t divide it by 2. Second, the result of any template is always going to be a string, even if it has to be converted to a string. So adding the int filter does nothing but add extra processing time.
The fact that the brightness value of the service call being a string representation of a number is not a problem because the service code is going to convert it to a int internally. Otherwise you could never use a template (because, again, the output from every template is a string.)
Ah, ok, I didn’t think of that scenario. So it’s not that you’re trying to convert a string to an int, but trying to convert a float to an int before it gets converted to a string representation due to the fact that the template output is always a string.
It comes down to the Python int() function - int('6') is ok, and int(6.5) is ok, but int('6.5') isn’t. You’re ending up in that last case without the int filter because the template takes the float and turns it into a string representation of it (i.e., converts 6.5 to '6.5'.)
@Mariusthvdb, at the very least, I’m pretty sure it’s not needed here. Brightness is an int, so converting an int to an int definitely doesn’t do anything.
This is a different case since an input_number’s state can be a string representation of a float. So to avoid the situation described above, it should be converted to an int before it automatically gets converted to a string.
But I suppose in the end adding a potentially unnecessary |int filter doesn’t hurt. It’s probably just best to be consistent.
I needed it here, since when the light is not on, brightness would be 0. And 0 is not an int, so to keep things simple and not add all kinds of extra rules in the template, adding |int makes it work.
Ah, ok, another scenario I didn’t consider. lol! Thanks for setting me straight. FWIW, when the light is not on the brightness attribute doesn’t exist, so the state_attr function returns None. The int filter (by default) returns 0 when it can’t convert the input (such as None) to a number.
exactly.
where I mentioned 0, I was referring to my input_number being slided to 0 which is a valid value for the slider, and hence, input for the lights template.