Add division math function to templates

I’ve seen in https://github.com/home-assistant/home-assistant/blob/master/homeassistant/helpers/template.py that templates support a multiply function which can be used to massage the units of a sensor to suit the user’s needs in the UI (such as converting Mb/s to Kb/s) but there isn’t a matching divide which was a little surprising.

I’m currently using a multiply operation of 0.001 to convert my disk free sensor from GB to approximate TB, but it’s a little crude. Would anyone else prefer to have an explicit divide function that they can use in the configuration YAML?

Ideally, I’d prefer this:

    disk_free_tb:
        value_template: '{{ states.sensor.disk_free_.state | divide(1024) }}'
        friendly_name: 'Disk Free'
        unit_of_measurement: 'TB'

Over this:

    disk_free_tb:
        value_template: '{{ states.sensor.disk_free_.state | multiply(0.001) }}' # close enough
        friendly_name: 'Disk Free'
        unit_of_measurement: 'TB'
1 Like

Voted! This would be handy indeed, converting division into multiplication isn’t always ideal.

E.g.: I have a template in which I have to divide a number by 11.25 so I have to use multiply(0.088888888888889)

Yes I vote for, I am busy to change wind direction in a compass terms and that needs divide too, so yes please add the option of DIVIDE

Just use /

http://jinja.pocoo.org/docs/dev/templates/#math

1 Like

Nordlead you are absolutely right, in jinja it is possible, thanks for poiting that out to me. So topic closed?!

I am trying to do a simply addition and subtraction - nothing works…

The template I am trying to do, is (for example) add a number like 100 to an output like this:

Output: ‘{{ states(“input_number.bedlight_temperature”) }}’

Addition: ‘{{ (states(“input_number.bedlight_temperature”) + 30 ) }}’

I have tried all sorts, nothing I try works… it doesn’t return any error, just goes blank :thinking:

States are strings so to apply math functions to them, you need to cast them to the right type. This should work for you:
Addition: ‘{{ (states(“input_number.bedlight_temperature”) | int) + 30 }}’

1 Like

Thanks man!

1 Like