Notice: This forum has been recovered from an old backup, so some content, links, and dates may be outdated. The forum is currently read-only while we restore sign-in and registration functionality. Details

If you find this forum valuable and would like to help keep it online, donations to help cover hosting and domain costs are greatly appreciated, but never expected. You can support the forum through Buy Me a Coffee or Ko-fi. Thank you for helping preserve the EventGhost community.

How to calculate in Python to compensate for sensor errors?

If you have a question or need help, this is the place to be.
Post Reply
Pallantir
Posts: 41
Joined: Mon May 14, 2012 6:41 pm

How to calculate in Python to compensate for sensor errors?

Post by Pallantir »

I have a bunch of temperature sensors in the house, and I know that a couple of them are a bit off. One of them, in the aquarium, is one degree off, and I would like that to reflect on the web server page I have made. I tried the way it would be done in LUA, but that's of course not working. Can somebody please tell me how this is done? I'm sure it's dead simple if you know it...

Code: Select all

import time
eg.event.payload_copy = eg.event.payload
my_data = eg.event.payload_copy.split(' ')

td = my_data[2]-1 #temperature and then subtracting one

timeStamp = str(
time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
)
eg.globals.Akvariet_time = timeStamp
eg.globals.Akvariet_temp = td
That gives me this error:

Code: Select all

11:51:10            Traceback (most recent call last):
11:51:10              Python script "37", line 5, in <module>
11:51:10                td = my_data[2]-1 #temperature and then subtracting one
11:51:10            TypeError: unsupported operand type(s) for -: 'str' and 'int'
I have tried moving the -1 around the script and even doing td2 = td-1, but that doesn't help.
krambriw
Plugin Developer
Posts: 2570
Joined: Sat Jun 30, 2007 2:51 pm
Location: Stockholm, Sweden
Contact:

Re: How to calculate in Python to compensate for sensor erro

Post by krambriw »

Code: Select all

td = float(my_data[2])-1.0 #temperature and then subtracting one
RichPyke
Posts: 11
Joined: Wed Sep 12, 2012 9:04 pm
Location: Cheltenham, UK
Contact:

Re: How to calculate in Python to compensate for sensor erro

Post by RichPyke »

Change td = my_data[2]-1 to
td = float(my_data[2])-1

It should work then.

From my understanding (basic understanding) td was a string, not a number. You can't -1 from a string. Float changes it to a number... (Please, correct me if I'm wrong)

Edit: ^^ Beat me to it.
Pallantir
Posts: 41
Joined: Mon May 14, 2012 6:41 pm

Re: How to calculate in Python to compensate for sensor erro

Post by Pallantir »

Synchronized posting should be a part of the olympics, just like syncrhnoized swimming! :mrgreen: Thank you very much for the help, both of you! Works like a charm!
krambriw
Plugin Developer
Posts: 2570
Joined: Sat Jun 30, 2007 2:51 pm
Location: Stockholm, Sweden
Contact:

Re: How to calculate in Python to compensate for sensor erro

Post by krambriw »

If you rather like to have td as a string with a specified number of decimals, you can do like this:

Code: Select all

td = str( "%.2f" % (float(my_data[2])-1.0))
Pallantir
Posts: 41
Joined: Mon May 14, 2012 6:41 pm

Re: How to calculate in Python to compensate for sensor erro

Post by Pallantir »

Thanks again! So that's nice to have if I want to calclate a bit more on it, right?
Post Reply