How i can do Math operation in template?

How I can do math operation in a template like this?

  - platform: rest
    resource: http://192.168.1.79:1880/HASS/?entity=OWL_day
    name: OWL_day
    scan_interval: 60
    value_template: >-
      {{ value | int }}      

I need to divide the value and obtain a float… I guess with some like this {{ value / 1000 | float }} … but obviously this isn’t working…

2 Likes

That is exactly how you do math in a template.
To try it out, put this code into the template editor developer tool:

{%set value = 101%}
{{ value / 1000 }} 
{{ value/1000 | float }}

I’ve tryed …

  - platform: rest
    resource: http://192.168.1.79:1880/HASS/?entity=OWL_day
    name: OWL_day
    scan_interval: 60
    value_template: >-
      {{ value /  1000 | float }}      

… before, but I got a config error. Why? Where I’m wrong?

Templating in Home Assistant is powered by Jinja2 templating engine. You can read the templating page of Home Assistant here:

And Jinja2 documentation here:

http://jinja.pocoo.org/docs/2.10/

You can experiment with the formula in Templates on Developer Tools (inside the HA left menu, click the icon of a page with <> inside).

Hope this helps

And what is the error? What is “value” exactly?

Have you tried:

{{ (value / 1000) | float }}

Yes, I know… in the HA DEV tools it works, but not in the config! :thinking:

Try {{ (value / 1000) | float }}. It happened to me many times that the formula gives no error inside Dev Tools but gives conversion errors when inside an automation.

What’s the error your getting?

Tried also with {{ (value / 1000) | float }} … the sensor is skipped and no error in the logs.
With {{ value | float }} it work fine.
How I can debug? where to find the error?

P.S.: the value is about 11000

Tryed also with:

value_template: '{{ value /1000  | float }}'
value_template: '{{ (value/1000)  | float }}'

… no way! isn’t work! the sensor is skippe w/o errors!

My main question is what is value?? There are no variables with that name…

value is the default variable name for the output of a RESTful component (https://home-assistant.io/components/sensor.rest/).
About value I’m sure that is work and refer to a right output because if I use {{ value | float }} this visualize the correct number in the UI.

Hummm… Can you post what what appears in the States page of Dev Tools for that Entity?

Eureka! this is the correct way…

value_template: '{{ value | multiply(0.1) | float }}'

1 Like

Now the last question is … how to define the precision of the float? In the UI I’ve 3 decimals and I want to display only 2 decimals.
How I can achieve this?

Great!

value_template: '{{ value | multiply(0.1) | float(2) }}

Yes! simple! Thank @j.assuncao

1 Like

Mark your own answer as solution, so other know its solved.

If {{value | multiply(0.1)}} works and {{value/10}}does not work, that means that value is coming in as a string. You can’t do math on a string, you first have to convert it to a number. That’s why the filter works. You could also do {{value | float / 10}}

… this is plausible, I will inviestigate and I will try.

It seemed to work, but it was a casuality… My UI show randomic decimal points… some time 2 decimal, some time 3, some time 10 or more…
So, how I can show only 2 decimal point? float(2) seem to not work.