Openhab 3 Executing Python Scripts

  • Platform information:
    • Hardware: RPi Zero W
    • OS: Raspbian 10 (buster) Openhabian
    • Java Runtime Environment: Zulu11.43+88-CA (build 11.0.9+11-LTS)
    • openHAB version: 3.0.0
  • 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!

Cheers!

Joe

You can publish your data via mqtt or http and then use those bindings to create things in openhab.

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.

As I am used to the command line I use the exec binding for stuff I cannot find existing bindings.

Thanks! I am going to play with this more.

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.

Any advice or help would be greatly appreciated!

Thanks!

Joe

Why not use HABApp to run your python scripts.
It offers seamless integration with openhab so you can just post the values to your items.

That sounds like the ticket! I’ll check that out. I haven’t heard of the HABApp.

Thanks @Spaceman_Spiff!

Can you give me any insight on how to implement HABApp? I have it installed and am going through the documentation but it’s a bit over my novice head.

I see the “execute_subprocess” routines. But what do you do after that? How do I push those results to Items?

Do you have any examples you can share?

Thanks!!

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.

Awesome! Thanks so much!

#!/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()

I just typed it down but it should work.
Notice how I just moved the reading of the sensor into the rule, the rest stays as is.

import time
from datetime import timedelta

from grove.i2c import Bus

from HABApp import Rule
from HABApp.openhab.items import NumberItem


def CRC(data):
... 
# Code here


class TemperatureRule(Rule):
    def __init__(self):
        super().__init__()
        self.sensor = GroveTemperatureHumiditySensorSHT3x()
        
        self.item_temperature = NumberItem.get_item('ItemTemperature')
        self.item_humidity = NumberItem.get_item('ItemTemperature')
    
        self.run_every(None, interval=timedelta(seconds=5), self.read_sensor)

    def read_sensor(self):
        temperature, humidity = self.sensor.read()
        
        self.item_temperature.oh_post_update(f'{temperature:.2f}')
        self.item_humidity.oh_post_update(f'{humidity:.2f}')


TemperatureRule()

Thanks! I apprecaite that. That looks really quite simple. :smiley:

@Spaceman_Spiff

So I added that code to a test script and am getting the following error:

File “grove_temperature_humidity_sensor_sht3x.py”, line 95
self.run_every(None, interval=timedelta(seconds=5), self.read_sensor)
^
SyntaxError: positional argument follows keyword argument

Any ideas here? I can post the entire code if need to. Thanks!

self.run_every(None, interval=timedelta(seconds=5), callback=self.read_sensor)

I updated that but am getting this error:

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

Imho it is helpful if you skim over the getting started

Thanks I’ll check that out!

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.

I do appreciate all of the suggestions!

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! :wink: