Issue of the topic:
I have several sensors that I can read via python scripts but do not have any bindings associated with them. I would like to display those readings from as seperate Things.
Where might I find any applicaple posts or information about doing this? I did a quick search and didn’t get further than finding the Wiki for the exec binding. I’m hoping someone might have an example of using the exec binding to run a python script for a sensor. Just not tyring to re-invent the wheel.
Thanks for the suggestion! I had thought about posting to MQTT and doing it that way as well. That may be the simplest way. I’m curious what others think as well.
I’ve been working on this all evening and struggling to get anywhere with it.
The command I will be running is:
grove_temperature_humidity_sht31
The values that are returned when you run this command are:
Temperature in Celsius is 17.73 C
Relative Humidity is 49.62 %
I would like to snip the degrees in C, transform it to F, and display that in a seperate Temperature and Humidity Thing. This seems like it should be something that could easily be accomplished with the exec binding.
I’m going to try and play with things and see if I can figure out how to publish to a MQTT topic within the original python script.
Instead of executing the pythonscripts through execute_subprocess you run your python code in a HABApp rule and read the temperature and humidity there.
If you copy paste your script I’ll show you how.
#!/usr/bin/env python
#
# Library for Grove - Temperature & Humidity Sensor (SHT31)
# (https://www.seeedstudio.com/Grove-Temperature-Humidity-Sensor-SHT3-p-2655.html)
#
'''
## License
The MIT License (MIT)
Copyright (C) 2018 Seeed Technology Co.,Ltd.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
'''
import time
from grove.i2c import Bus
def CRC(data):
crc = 0xff
for s in data:
crc ^= s
for _ in range(8):
if crc & 0x80:
crc <<= 1
crc ^= 0x131
else:
crc <<= 1
return crc
class GroveTemperatureHumiditySensorSHT3x(object):
def __init__(self, address=0x44, bus=None):
self.address = address
# I2C bus
self.bus = Bus(bus)
def read(self):
# high repeatability, clock stretching disabled
self.bus.write_i2c_block_data(self.address, 0x24, [0x00])
# measurement duration < 16 ms
time.sleep(0.016)
# read 6 bytes back
# Temp MSB, Temp LSB, Temp CRC, Humididty MSB, Humidity LSB, Humidity CRC
data = self.bus.read_i2c_block_data(self.address, 0x00, 6)
if data[2] != CRC(data[:2]):
raise ValueError("temperature CRC mismatch")
if data[5] != CRC(data[3:5]):
raise ValueError("humidity CRC mismatch")
temperature = data[0] * 256 + data[1]
celsius = -45 + (175 * temperature / 65535.0)
humidity = 100 * (data[3] * 256 + data[4]) / 65535.0
return celsius, humidity
Grove = GroveTemperatureHumiditySensorSHT3x
def main():
sensor = GroveTemperatureHumiditySensorSHT3x()
while True:
temperature, humidity = sensor.read()
print('Temperature in Celsius is {:.2f} C'.format(temperature))
print('Relative Humidity is {:.2f} %'.format(humidity))
time.sleep(1)
if __name__ == "__main__":
main()
Traceback (most recent call last):
File “grove_temperature_humidity_sensor_sht3x.py”, line 104, in
TemperatureRule()
File “grove_temperature_humidity_sensor_sht3x.py”, line 89, in init
super().init()
File “/usr/local/lib/python3.7/dist-packages/HABApp/rule/rule.py”, line 43, in init
__vars = sys._getframe(depth).f_globals
ValueError: call stack is not deep enough
I ended up modifying my python scripts to post results to MQTT and am using MQTT to create and update my items. HABApp was a bit over my head. Great concept and I’m sure it works well. I just couldn’t wrap my head around the functions and get it to work.
All you had to do was to put the file in the configured rule folder (of HABApp) and enter the openhab information in the configuration file.
HABApp could have also doubled as a rule engine where you can write all your rule in python3.
But I guess you just want to do it the hard way!