# Templating math power function help

Does HA have a “raise to the power” function?

I’m trying to implement the following function

`{{ 400 * e^( -0.045 * states('sensor.aeotec_lounge_light_level') | float ) | int }}`

For Lux values of 70 to 10 it should output 17 to 255 in a nice exponential curve.

I’ve been messing with the template editor but it does not recognise " ^ ".

What is the point of having the constant `e` defined if there is no power function?

Do I have to write a python script and use `math.exp(x)` ?

1 Like

I am no python/jinja expert but I believe `**` does it.

Try
`{{ 400 * e**( -0.045 * states(‘sensor.aeotec_lounge_light_level’) | float ) | int }}`

1 Like

Thanks but I tried that. It always returns 400.0 which is weird considering the | int

What’s weirder is that

`{{ 400 * e**(-0.045*70) | int }}`

returns 19.914827347145582 which is wrong and not an integer. Output should be 17.

Aha! this works:

{{ (400 * (e**(-0.045*70))) | int }}

This does too:

`{{ (400 * (e**(-0.045 * states('sensor.aeotec_lounge_light_level') | float))) | int }}`

or it would if my lux value wasn’t currently == 0

I just have to make sure I use a condition so that it is only calculated for values of 70 to 10.

The code fails the config check though:

``````Error loading /config/configuration.yaml: invalid key: "OrderedDict([("(400 * (e**(-0.045 * states('sensor.aeotec_lounge_light_level') | float))) | int", None)])
``````
``````    - condition: template
value_template: "{{ 70.1 > states('sensor.aeotec_lounge_light_level') | float > 9.9 }}"
- service: light.turn_on
data_template:
entity_id:  light.lifx_lounge_entrance_lamp
brightness: {{ (400 * (e**(-0.045 * states('sensor.aeotec_lounge_light_level') | float))) | int }}
``````

EDIT: It’s because I forgot to put the template in quotes.

``````    brightness: "{{ (400 * (e**(-0.045 * states('sensor.aeotec_lounge_light_level') | float))) | int }}"
``````

Which i dont think is going to work as brightness expects a number not a string.

Back to the python script?

1 Like

All single line templates require quotes. Your template is returning an int so it should work.

The template looks correct and should work. My only concern is the fact that your equation can produce a result higher than 255. Brightness only allows values from 0 to 255.

If your sensor has a value between 0 and 10, it will break the brightness attribute.

0 produces a result of 400, and 9.9 produces a result of 256 and 10 produces 255. You should account for that.

Thanks petro. Ok I’ll give it a go and see what happens tomorrow night.

I am sanitising the data sent to the brightness template. This appears before the light turn on service:

``````- condition: template
value_template: "{{ 70.1 > states('sensor.aeotec_lounge_light_level') | float > 9.9 }}"
``````

The sensor returns 0.1 Lux increments.So the brightness template should only see values of 10 to 70 Lux.

Ah ok, good. Might want to change your bottom check for 10.0 instead of 9.9 though.

``````value_template: "{{ 70.1 > states('sensor.aeotec_lounge_light_level') | float >= 10.0 }}"
``````

Are they not equivalent?

`> 9.9` with a 0.1 step is 10.0

Well they are but… interpolation from a string to a float could cause a value to be 9.90000000000001. Python should handle it but it could cause an issue.

Good to know. Thanks.

I’ll use this then:

``````"{{ 70.0 >= states('sensor.aeotec_lounge_light_level') | float >= 10.0 }}"
``````

It’s more for safety than anything. I doubt you’d run into the floating point issue. I think HA is pretty good about removing epsilon error when converting from string to floats.

Better safe than sorry.

It works exactly as I wanted. Though after sunset it gets dark really quickly. The lux value can go from the low teens straight to below 10. So the brightness never actually gets set to 255 but it’s close enough so as to not make any difference. Also I will be replacing the zwave lux sensor with a nodemcu and I’ll be able to get more frequent updates.