Template sensor not returning integer value

Here is my yaml. The template editor and the UI both display a float instead of an integer. Why is this?

{%- if (is_state('light.desk_lamp_dimmer', 'on')) -%}
        {{ state_attr('light.desk_lamp_dimmer', 'brightness') | int
              / 255 * 100 | int }}{{'%' }}
        {% else %}
        {%- endif %}

I’ve also tried piping through round(0), round(1), round(0, “floor”) in place of the final pipe through int, but always a float like this:

56.470588235294116%

Because you are converting the number 100 to an integer. Put some brackets around the whole expression.

{% if (is_state('light.desk_lamp_dimmer', 'on')) %}
        {{ ( state_attr('light.desk_lamp_dimmer', 'brightness') | int
              / 255 * 100 ) | int }} {{ '%' }}
{% else %}
{% endif %}
1 Like

That’s it! Thanks much!

1 Like

Hi all!
I have the same problem but something is not working for me… can u help me?

myscript:
  variables:
    az_min: 10
    az_max: 55
    az_tot: "{{ ( cos ( az_min + az_max ) * 100 ) | int }}" # I need an integer number
  sequence:
    - service: notify.persistent_notification
      data:
        message: "COS of the sum is {{ az_tot }}" 

I continuously obtain -56.2453851238172

Nothing is wrong with your code. Tested and working on my end. Are you sure you reloaded the script after making the changes?

1 Like

It worked. After a full server restart.
Thank you

You just need to reload scripts, you don’t have to do a full restart