How i can do Math operation in template?

That’s odd… Try:

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

… mmm it’s equal to my code … where’s the difference?

The difference is in the parenthesis wrapping value | multiply(0.1). Using the parenthesis forces to multiply the value first, and only than, taking the result and appliyng float(2).

OMG, I’m sorry! here is about to dinner, and I’m hungry… maybe my brain need carbohydrate!

Now I’m trying with round(2) … at this time seem to work,

1 Like

{{ as_timestamp(states(‘sensor.echo_show_di_matteo_next_alarm’)) | timestamp_custom(’%H:%M’) }}

I have this value which returns a time. in this case 7.10. if I wanted to go back half an hour, then less than 30 minutes, which mathematical operator should I use and what mathematical formula?

@matteos1 Does this get what you need ?

{{ (as_timestamp(strptime('2019-12-05 08:00:00','%y-%m-%d %H:%M:%S')) - (30 * 60))   | timestamp_custom('%H:%M') }}

The trick was surrounding the as_timestamp function and math with ‘()’.
I just use the strptime() to emulate your string.

i have this with hour and minutes but when make binary sensor i’ve error

 - platform: template
    sensors:
      annuncio_allarme_notturno:
        friendly_name: "Annuncio Allarme Notturno"
        value_template: >
         {{ (as_timestamp(states('input_datetime.orario_allarme_notturno'))-10*60) | timestamp_custom('%H:%M') == states('sensor.time')}}

someone that can help me?

I have also something wrong with formula in math operation.
I.e. I have ‘PMS7003’ + ‘BME280’ sensors.
All data is displayed, except ‘pressure’.
1

The config file is following:

#Read data from PMS7003 and BME280 sensors (from http://IP_SENSOR/data.json)
  - platform: rest 
    name: PMS7003_P0 
    unit_of_measurement: "µg/m3" 
    value_template: '{{value_json ["sensordatavalues"] [0] ["value"]}}' 
    resource: 'http://192.168.1.227/data.json' 

  - platform: rest 
    name: PMS7003_P1 
    unit_of_measurement: "µg/m3" 
    value_template: '{{value_json ["sensordatavalues"] [1] ["value"]}}' 
    resource: 'http://192.168.1.227/data.json'
    
  - platform: rest 
    name: PMS7003_P2 
    unit_of_measurement: "µg/m3" 
    value_template: '{{value_json ["sensordatavalues"] [2] ["value"]}}' 
    resource: 'http://192.168.1.227/data.json'

  - platform: rest 
    name: PMS7003_BME280_temperature 
    unit_of_measurement: "°C" 
    value_template: '{{value_json ["sensordatavalues"] [3] ["value"]}}' 
    resource: 'http://192.168.1.227/data.json'

  - platform: rest 
    name: PMS7003_BME280_pressure 
    unit_of_measurement: "hPa" 
    value_template: '{{value_json ["sensordatavalues"] [4] [("value" | multiply(0.01)) | float(2)]}}' 
    resource: 'http://192.168.1.227/data.json'

  - platform: rest 
    name: PMS7003_BME280_humidity 
    unit_of_measurement: "%"
    value_template: '{{value_json ["sensordatavalues"] [5] ["value"]}}' 
    resource: 'http://192.168.1.227/data.json'

What is wrong in this math operation in this part?:

value_template: '{{value_json ["sensordatavalues"] [4] [("value" | multiply(0.01)) | float(2)]}}' 

The all data is coming from this:

{"software_version": "NRZ-2020-129", "age":"124", "sensordatavalues":[{"value_type":"PMS_P0","value":"22.00"},{"value_type":"PMS_P1","value":"37.00"},{"value_type":"PMS_P2","value":"31.75"},{"value_type":"BME280_temperature","value":"18.85"},{"value_type":"BME280_pressure","value":"99600.47"},{"value_type":"BME280_humidity","value":"40.70"},{"value_type":"samples","value":"4358879"},{"value_type":"min_micro","value":"32"},{"value_type":"max_micro","value":"11888"},{"value_type":"signal","value":"-56"}]}

As you can see - pressure is ‘99600.47’ so I wanted to make some operation to display in ‘hPa’.

Looked deeper - but still not expected results.
Why I’m getting precision of 4 digits after point? I want only 2.
The display value is:
value

From initial value: ‘99809.75’

value_template: '{{value_json ["sensordatavalues"] [4] ["value"] | float / 100 | round(2, ceil)}}'

you’re only rounding the 100.

value_template: ‘{{ (value_json [“sensordatavalues”] [4] [“value”] | float / 100) | round(2, ceil)}}’

Yes, indeed… But how then should look the command? I’ve tried ~20 examples; none was good…

Uh… I posted it in my response to you… here it is again

value_template: '{{ (value_json ["sensordatavalues"] [4] ["value"] | float / 100) | round(2, "ceil") }}'

Yes, sorry… I could not see the other part on phone.
Now O.K. works. Thx!

Hey Guys,

How can i round this template value operation? (round…doesnt work)

value_template: >- 
          {% set t = states('sensor.energie_total_api') | float %} 
          {% set u = states('sensor.einspeissung_sag_gesamt') | float %} 
          {{ (100/t)*(t-u) }} 

thanks for your help!

greetings from Austria

1 Like

What isn’t working with round in that case?

{% set t = 99.56 | float %} 
{% set u = 33.577 | float %} 

{{ ((100/t)*(t-u)) }} 

{{ ((100/t)*(t-u)) | round(2) }}
1 Like

WOW!!!
Thanks Dennis! Great Job!
Danke Dir! Eine Klammer um die ganze Rechnung und schon geht es! GENIAL!

After a lot of digging I finally found this post which solved my problem; calculating Nordpool spot price + net tariffs and VAT:

template:
  - sensor: {{ ( 
               states('sensor.skanska_energi_avgifter') | float()
               +
               states('sensor.energi_data_service') | float()
               ) 
               * 1.25 / 100
            }}

Thank you! :+1: