Divide sensor value by another sensor value

Hi

I have a Shelly EM that measures Voltage and Watts.

I’d like to calculate the amps draw by dividing watts by voltage.

Iv’e tried using adding this to my configuration.yaml but it registers as a non-numeric sensor.

value_template: "{{ ((states.sensor.shelly_shem_b9e7ba_2_current_consumption | float)  / (states.sensor.shelly_shem_b9e7ba_voltage | float)) | round(0) }}"

Any pointers as to where I am going wrong?

Thanks

This:

states.sensor.shelly_shem_b9e7ba_2_current_consumption

Should be:

states.sensor.shelly_shem_b9e7ba_2_current_consumption.state

Likewise for the voltage sensor.

However this form is better:

{{ ((states('sensor.shelly_shem_b9e7ba_2_current_consumption')|float) / (states('sensor.shelly_shem_b9e7ba_voltage')|float))|round(0) }}

It will not produce errors if the sensor is unavailable.

2 Likes

Thanks so much Tom.

Works perfectly :+1:

1 Like

Hello to all

I’m also trying to dived this value in the sensor in order to get the value as 19.32 ( 2 decimal cases)

image

Using this

value_template: "{{ (states.sensor.tuya_temp_sensor.state / 1000)|round(2) }}"

But the value still present to me as the 19320.0 …

Can you please help me in order to get this
Thank you

Your template needs to convert the sensor’s state value to a number (using either the float or int) before it can be properly divided by another number, like 1000.

value_template: "{{ (states('sensor.tuya_temp_sensor') | float(0) / 1000) | round(2) }}"
2 Likes

Great thank you it works :grinning:

Hello i want to make an entity from my pool monitor (PH and ORP) sensors with this math operation and get the number of Chlorine in PPM.

Free Chlorine (ppm) = (mV + pH - 600) / 10

I have the below working code but cannot divide with number 10. Im stuck here…

template:
  - sensor:
      - name: "ORP and PH to PPM"
        unit_of_measurement: "PPM"
        device_class: volatile_organic_compounds_parts
        state_class: measurement
        state: >- 
          {{ states('sensor.pool_monitor_orp') | float + states('sensor.pool_monitor_ph') | float - 600}}

Can anyone help me?

Thanks!

        state: >- 
          {{ (states('sensor.pool_monitor_orp') | float(0) + states('sensor.pool_monitor_ph') | float(0) - 600) / 10 }}
1 Like

Finaly i found it!

template:
  - sensor:
      - name: "Pool Monitor PPM"
        unit_of_measurement: "PPM"
        device_class: volatile_organic_compounds_parts
        state_class: measurement
        state: >- 
          {{ ((states('sensor.pool_monitor_orp') | float + states('sensor.pool_monitor_ph') | float - 600) / 10 ) |round(1)}}

If you look at the example I posted above, it provides each float filter with a default value. Your version doesn’t which will result in an error if the value returned by the states() function is ever none. I suggest you adopt the habit of providing Jinja2 functions, like float, with a default value.

Yes but in your version I’m getting for ex. 4.046000000000004 and on mine I’m getting 4.1 which is that I want.

Is that correct?

{{ ((states('sensor.pool_monitor_orp') | float(0) + states('sensor.pool_monitor_ph') | float(0) - 600) / 10 ) |round(1)}}

Your original template didn’t round the computed value and you didn’t ask for it to be rounded. You asked for help to divide the computed value by 10 and that’s what I demonstrated how to do.

Yes.


NOTE

You should consider using a Template Sensor’s availability option to guard against errors caused by unavailable entities.

Do you know how can make a sensor with this chart for example?

or this one?

PPM_Reading_vs_ORP_and_pH

I have a pool with a PH-W2839 (Wireless WiFi Online Control Detector Water Quality Detector 3-in-1 pH/ORP/TEMP Controller Water Quality Tester BNC Type Probe). It’s working wonderfull with Home Assistant but i want to know the free chlorine PPM. I found a math equation from an AI (poe.com and it’s (ORP+PH-600) / 10 but according to other convertion charts for example:

it’s not even close to this. For examble with ORP 800 and pH 7.5 (800+7.5-600)/10 we have 20.75ppm. As you see above it has 4ppm. The gap is HUGE! When i ask it why it give the 600 and what is it I’m getting this:

The value of 600 in the formula is a constant and represents the theoretical redox potential of a solution containing both chlorine and hypochlorous acid (HOCl) at a pH of 7. This value is often used as a reference point for redox measurements in swimming pools, where the predominant form of chlorine is HOCl.

The value of 600 is subtracted from the sum of the mV and pH values in the formula to account for the contribution of hypochlorous acid to the redox potential. This leaves only the contribution of free chlorine to the redox potential, which can then be used to estimate the free chlorine concentration in the pool water.

It’s worth noting that the value of 600 is an approximation, and the actual redox potential of a solution containing chlorine and HOCl will depend on factors such as temperature, pressure, and the concentration of other ions in the solution. However, for practical purposes, the value of 600 is a reasonable approximation that is widely used in the swimming pool industry.

The question is how can I have a sensor which calculate with the data from the above chart in Home Assistant?

Easy, if you hire one for the project cause who wants to work for free for you ?

I guess you will need and want an expert so be prepared for a nice price and do not start counting beans that the parts are so cheap and the price is ridiculous.
It is not - only the expectations are ususally too high.

And due to the behaviour to not really honor the effort other have invested and wrote replies and ideas I doubt that any one will want to work on such a project cause so many replies of others had not been rewarded by you so there is not much hope that you will get anything except you will pay for it.

I did it with Chat GPT. :slight_smile:

Usually people tend to present their results when they have asked a question and let others think for them. But I doubt chat gpt is able to deliver anything usefull based on what you had asked for .

But who knows as long as chat gpt is not taking over and also replying here.

Me! Me!

Would be genuinely fascinated to see the results. Here’s my attempt (luckily, the the table was searchable and the text copyable!) — paste this into Developer Tools / Template to play:

{% set orp = [
    [735,725,720,712,704,695,687,680,675,668,662],
    [760,750,742,734,725,716,709,703,695,689,684],
    [772,765,756,748,738,731,722,715,708,702,697],
    [780,772,763,756,748,740,732,724,717,712,705],
    [788,780,772,763,754,746,738,732,725,718,714],
    [794,785,778,769,761,753,745,737,731,724,719],
    [800,792,782,774,765,758,750,742,735,729,724],
    [  0,798,786,778,770,762,754,746,739,734,728],
    [  0,  0,792,782,774,766,757,751,743,736,733],
    [  0,  0,798,785,778,769,761,754,746,741,736],
    [  0,  0,  0,789,781,772,764,756,750,744,738],
    [  0,  0,  0,793,783,775,767,760,753,746,742],
    [  0,  0,  0,796,786,777,770,762,756,749,744],
    [  0,  0,  0,  0,788,780,772,765,758,752,746],
    [  0,  0,  0,  0,792,782,774,767,760,754,749],
    [  0,  0,  0,  0,  0,784,776,769,762,756,751],
    [  0,  0,  0,  0,  0,786,778,771,764,758,753],
    [  0,  0,  0,  0,  0,788,780,773,766,760,755],
    [  0,  0,  0,  0,  0,790,782,775,767,762,756],
    [  0,  0,  0,  0,  0,792,784,777,769,763,758]
  ]
%}
{% set ph = 7.4 %}   # this is your pH sensor
{% set cl = 1.5 %}   # this is your Cl sensor
{% set col = ((ph - 7) * 10)|int %}
{% set row = ((cl - 0.5) * 2)|int %}

{{ orp[row][col] }}

That’d be a good application for a macro / reusable template:

1 Like