Author Topic: Lua script to calculate hot water tank percentage from temperature sensors  (Read 333 times)

Offline Talisker

  • Newbie
  • *
  • Posts: 7
  • Karma: +0/-0
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:
http://www.dreamgreenhouse.com/reviews/2012/fibaro_us/
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.


Offline Mike Yeager

  • Hero Member
  • *****
  • Posts: 571
  • Karma: +9/-12
Re: Lua script to calculate hot water tank percentage from temperature sensors
« Reply #1 on: September 10, 2018, 06:26:49 pm »
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.

Offline jswim788

  • Hero Member
  • *****
  • Posts: 789
  • Karma: +57/-2
Re: Lua script to calculate hot water tank percentage from temperature sensors
« Reply #2 on: September 10, 2018, 06:48:28 pm »
I can't test it, but perhaps something like this would do what you want?
Code: [Select]
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)
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.

Offline Talisker

  • Newbie
  • *
  • Posts: 7
  • Karma: +0/-0
Lua script to calculate hot water tank percentage from temperature sensors
« Reply #3 on: September 11, 2018, 04:55:20 pm »
Thanks for the help. I?m working away but will try it as son as I get home......


Sent from my iPhone using Tapatalk

Offline Talisker

  • Newbie
  • *
  • Posts: 7
  • Karma: +0/-0
Re: Lua script to calculate hot water tank percentage from temperature sensors
« Reply #4 on: September 16, 2018, 07:46:43 am »
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.




Offline Talisker

  • Newbie
  • *
  • Posts: 7
  • Karma: +0/-0
Re: Lua script to calculate hot water tank percentage from temperature sensors
« Reply #5 on: September 17, 2018, 01:25:03 pm »
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,

Offline jswim788

  • Hero Member
  • *****
  • Posts: 789
  • Karma: +57/-2
Re: Lua script to calculate hot water tank percentage from temperature sensors
« Reply #6 on: September 17, 2018, 02:19:51 pm »
The programming in Lua manual gives this example:
Code: [Select]
print(string.format("pi = %.4f", PI))     --> pi = 3.1416What have you tried and what were the strange results?

Offline Talisker

  • Newbie
  • *
  • Posts: 7
  • Karma: +0/-0
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)