Lua script to calculate hot water tank percentage from temperature sensors

I would very much like some assistance with my project. I wish to calculate the percentage of hot water in my water tank based on the value of four temperature sensors which measure the tank temperature at different heights. I have a set up similar to the following:

My temperatures are measured with a fibaro universal sensor and I wish to ‘push’ the calculated percentage value to a multistring plugin.
Is anyone out there able to help and get me started as my coding abilities are limited at best!
Thanks.

I’m unsure of the actual purpose of this. If you have a sensor at the top and a sensor afew inches from the bottom and it’s a 40 gallon tank, you can basically figure you have 40 gallons of water at the average of the two temperatures. To be honest though, you’ll never use the “hot” water at the bottom as the tank draws from the top.

I can’t test it, but perhaps something like this would do what you want?

[code]local temp1 = luup.variable_get(“urn:upnp-org:serviceId:TemperatureSensor1”, “CurrentTemperature”, 5)
local temp2 = luup.variable_get(“urn:upnp-org:serviceId:TemperatureSensor1”, “CurrentTemperature”, 6)
local temp3 = luup.variable_get(“urn:upnp-org:serviceId:TemperatureSensor1”, “CurrentTemperature”, 7)
local temp4 = luup.variable_get(“urn:upnp-org:serviceId:TemperatureSensor1”, “CurrentTemperature”, 8)

local averageTemp = (temp1 + temp2 + temp3 + temp4) / 4

luup.variable_set(“urn:upnp-org:serviceId:VContainer1”, “newVariable1”, averageTemp, 9)
[/code]Replace 5, 6, 7, 8, 9 above with your actual device numbers. 9 is for the multistring plugin. This should set newVariable1 to the average temp. You can change it to any variable you wish.

Thanks for the help. I?m working away but will try it as son as I get home…

Sent from my iPhone using Tapatalk

I set to work based on input from jswim788. I managed to achieve everything I needed to (i.e. calculate an average tank temp based on 4 sensors and then a %age based on a useful empty and full (average) temperature. These can be adjusted based on experience. Hopefully this might be helpful to others:

local temp1 = luup.variable_get(“urn:upnp-org:serviceId:TemperatureSensor1”, “CurrentTemperature”, 847)
local temp2 = luup.variable_get(“urn:upnp-org:serviceId:TemperatureSensor1”, “CurrentTemperature”, 845)
local temp3 = luup.variable_get(“urn:upnp-org:serviceId:TemperatureSensor1”, “CurrentTemperature”, 846)
local temp4 = luup.variable_get(“urn:upnp-org:serviceId:TemperatureSensor1”, “CurrentTemperature”, 844)

local averageTemp = (temp1 + temp2 + temp3 + temp4) / 4

local tankEmpty = 40
local tankFull = 58

local percentTemp = ((averageTemp-tankEmpty) / (tankFull-tankEmpty)) * 100

luup.variable_set(“urn:upnp-org:serviceId:VContainer1”, “Variable1”, averageTemp, 851)

luup.variable_set(“urn:upnp-org:serviceId:VContainer1”, “Variable2”, percentTemp, 851)

Thanks everone taking the time to response.

Well, after feeling smug for a few days after getting vera to do what I wanted (see previous post), I realised that things were not quite as I would like them. The issue is that I am getting a ridiculous number of decimal places:

Tank Average = 48.3075
Tank % = 45.986111111111

I have tried a few different ways to limit the number to say two decimal places (XXX.XX), but I keep getting strange results.

Anyone out there got any suggestions?

Thanks,

The programming in Lua manual gives this example:print(string.format("pi = %.4f", PI)) --> pi = 3.1416What have you tried and what were the strange results?

So I have (with help!) got my script as I want it with the multistring plugin displaying my calculations to one decimal place using “%.1f”.

Thanks again all who contributed and made suggestions:

local temp1 = luup.variable_get(“urn:upnp-org:serviceId:TemperatureSensor1”, “CurrentTemperature”, 847)
local temp2 = luup.variable_get(“urn:upnp-org:serviceId:TemperatureSensor1”, “CurrentTemperature”, 845)
local temp3 = luup.variable_get(“urn:upnp-org:serviceId:TemperatureSensor1”, “CurrentTemperature”, 846)
local temp4 = luup.variable_get(“urn:upnp-org:serviceId:TemperatureSensor1”, “CurrentTemperature”, 844)

local averageTemp = (temp1 + temp2 + temp3 + temp4) / 4

local tankEmpty = 40
local tankFull = 60

local percentTemp = ((averageTemp-tankEmpty) / (tankFull-tankEmpty)) * 100

luup.variable_set(“urn:upnp-org:serviceId:VContainer1”,“Variable1”,string.format (“%.1f” ,averageTemp), 851)

luup.variable_set(“urn:upnp-org:serviceId:VContainer1”,“Variable2”,string.format (“%.1f” ,percentTemp), 851)