Template: convert negative to positive

I’m trying to get Alexa to set the volume on my Denon receiver. Problem is that the receiver is showing negative dB values as display value and the component works with a positive value. Also it is scaled. I found out how to convert it, but after conversion I’m having a negative float, which should just be converted to a positive float.
So I tried it with abs() and multiplying with -1, but both return not the expected result. Was hoping someone here sees my mistake.

  service: media_player.volume_set
    entity_id: media_player.denon_livingroom
    volume_level: '{{ Volume |float / 100 + -0.8 | round(2) }}'
  type: plaintext
  text: Ok

Volume is the var coming in through Alexa, confirmed working. Say in this example the outcome is -0.50, and it needs to be 0.50. Shouldn’t be a problem;

{{ Volume |float / 100 + -0.8 | round(2) | abs }}

And instead of the expected 0.50 it returns 1.1.
If I multiply with -1, it also returns 1.1

I think it has something to do with order, but using parentheses isn’t possible in Jinja2, right?

You are partially right. This is order of operations.
In this case, you are sending -.08 to the Round(2) filter.
BUT you are wrong that parenthesis are not allowed. You absolutely can do something like :

{{ (Volume |float / 100 + -0.8) | round(2) | abs }}

I can’t make out from your problem statement if this is the right way to structure your formula, but the point is you can change the order of operations with parens.

1 Like

You’re the man! :smile:
I didn’t open the parenthesis including the variable but starting at float and that’s what got me the error.

This works as expected!