Exec Binding script for 1wire temps on OpenHABianPi GPIO


(dijit) #1

I am attempting to get temperature readings from 1wire DS18b20 sensors connected to GPIO pin on my raspberry Pi running OpenHABianPi.
I have downloaded and installed the latest stable OpenHABianPi form here: openHABian hassle-free openHAB Setup

I did a lot of google research to figure out this problem, and ultimately followed this guide on using script to get temperature from 1wire device connected to GPIO:

My “Ger-glish” isn’t as gut as it used to be, but I think I got the basic concept.

So, in order to make my setup work in openHABianPi setup, here is what I did:

  1. Created the following script in “/etc/openhab2/scripts/”:
    “onewiretemp.sh”
#!/bin/bash

wert=`cat /sys/bus/w1/devices/$1/w1_slave | tail -n1 | cut -d '=' -f2`

wert2=`echo "scale=3; $wert/1000 * 9.0 / 5.0 + 32.0" | bc`
echo $wert2

2.Created 2 Items to run script (one for each of my 2 sensors):
“OneWireTemp1.items”

Number onewiretemp1ExitValue "Temp1 is [%.1f °F]" <temperature>  {channel="exec:command:onewiretemp1:exit"}

AND:
“OneWireTemp2.items”

Number onewiretemp2ExitValue "Temp2 is [%.1f °F]" <temperature>  {channel="exec:command:onewiretemp2:exit"}

3.Created 2 Things to run the script with the corresponding UID
"OneWireTemp.things"

exec:command:onewiretemp1 [command="bash /etc/openhab2/scripts/onewiretemp.sh@@28-blahblah"]

“OneWireTemp2.things”

exec:command:onewiretemp2 [command="bash /etc/openhab2/scripts/onewiretemp.sh@@28-blahblah2"]

OpenHab automagically adds the “Things” to the default sitemap, however, the values do not correspond with the actual values of the sensors.
No matter what the sensors are reporting at the terminal, the value of the thing in openHab is always “127”. I have messed around with the “Transform” functions (i.e. REGEX) , to no avail.
I’ve also created custom sitemaps and included:
item=onewiretemp1ExitValue
item=onewiretemp2ExitValue
Neither the default nor my custom sitemaps report the correct values for the temp sensors.

I have also attempted to create new “Things” from the exec binding in Paper and HABmin UI (instead of creating a things file), and then link to my custom items.
So far, I have not been able to get the temperature to read out correctly through OpenHab.

I also tested that I could run the original script as the openhab user (through ssh to raspberry pi box) and get a useable temperature. This was successful:

pi@openHABianPi:/etc/openhab2/scripts$ sudo -u openhab ./onewiretemp.sh 28-blahblah
70.250

Result is perfect, script runs under openhab user, DS18b20 value is 70.250 (Fahrenheit). Great! Now if only I could see this through OpenHab…

There is a nearly identical use case post:


This person relied on the same post I reference at the beginning of my post here, however does not explain how. And to address the issue of OWServer: It will not read from GPIO connected 1wire sensors, I have already been down this road.

So, what am I doing wrong in OpenHab? I feel like I am not implementing things and items correctly… Or maybe I need to use Output instead of ExitValue…?
Any help is greatly appreciated.


GPIO 1-Wire Temp sensors on Raspberry Pi with openHABian
( ) #2

Hey @dijit, thanks for the great tutorial in your first part of the message :wink:

You did most of the things I would have suggested. Please look at the /var/log/openhab2/events.log file to see when and to what value your items are updated. Hopefully this will give you some clarity.

It would be great if you could transform your posting into a Tutorial as soon as the problem is figured out!!


(dijit) #3

Thanks! I will certainly create a tutorial once I am able to successfully read and display temperatures (and can repeat the process and results). I also plan to work on some new bindings for OH, but I have a lot to learn before I can be at that level.

Sorry, a log would have been useful for diagnosis! Below are my events.log latest entries. The things created with random number ID’s are those created from the UI, after my custom things weren’t behaving. It didn’t matter if I created a thing file manually, or created a thing from the UI, both yield the same result: temp always says “127”. It is interesting to note that when a thing is created in the UI, no thing file is generated. So, Im not sure how to fully clear the references after removing the thing from UI.

Begin events.log:

2017-01-08 20:39:47.044 [ThingAddedEvent           ] - Thing 'exec:command:bc4dc6d6' has been added.
2017-01-08 20:39:47.347 [hingStatusInfoChangedEvent] - 'exec:command:bc4dc6d6' changed from UNINITIALIZED to INITIALIZING
2017-01-08 20:39:47.496 [hingStatusInfoChangedEvent] - 'exec:command:bc4dc6d6' changed from INITIALIZING to ONLINE
2017-01-08 20:40:16.338 [ThingAddedEvent           ] - Thing 'exec:command:284e0903' has been added.
2017-01-08 20:40:16.375 [hingStatusInfoChangedEvent] - 'exec:command:284e0903' changed from UNINITIALIZED to INITIALIZING
2017-01-08 20:40:16.489 [hingStatusInfoChangedEvent] - 'exec:command:284e0903' changed from INITIALIZING to ONLINE
2017-01-08 20:41:38.426 [ItemChannelLinkAddedEvent ] - Link 'onewiretemp1ExitValue-exec:command:bc4dc6d6:output' has been added.
2017-01-08 20:41:51.694 [ItemChannelLinkAddedEvent ] - Link 'onewiretemp2ExitValue-exec:command:284e0903:output' has been added.
2017-01-08 21:20:43.343 [ThingUpdatedEvent         ] - Thing 'exec:command:bc4dc6d6' has been updated.
2017-01-08 21:20:53.014 [ThingUpdatedEvent         ] - Thing 'exec:command:284e0903' has been updated.
2017-01-08 21:21:03.292 [ThingUpdatedEvent         ] - Thing 'exec:command:284e0903' has been updated.
2017-01-08 21:21:15.870 [ThingUpdatedEvent         ] - Thing 'exec:command:bc4dc6d6' has been updated.
2017-01-08 21:28:57.309 [ItemChannelLinkAddedEvent ] - Link 'onewiretemp2ExitValue-exec:command:284e0903:exit' has been added.
2017-01-08 21:29:04.546 [ItemStateChangedEvent     ] - onewiretemp2ExitValue changed from NULL to 127
2017-01-08 21:29:30.764 [ItemChannelLinkAddedEvent ] - Link 'onewiretemp1ExitValue-exec:command:bc4dc6d6:exit' has been added.
2017-01-08 21:29:34.551 [ItemStateChangedEvent     ] - onewiretemp2ExitValue changed from NULL to 127
2017-01-08 21:29:40.015 [ItemRemovedEvent          ] - Item 'onewiretemp2ExitValue' has been removed.
2017-01-08 21:29:40.064 [ItemRemovedEvent          ] - Item 'onewiretemp1ExitValue' has been removed.
2017-01-08 21:29:47.011 [ItemStateChangedEvent     ] - onewiretemp1ExitValue changed from NULL to 127
2017-01-08 21:30:04.604 [ItemStateChangedEvent     ] - onewiretemp2ExitValue changed from NULL to 127

( ) #4

This might be interesting to you: https://github.com/openhab/openhab/issues/4976

Glad to hear that. Both should behave the same.

That’s by design. One of the biggest misconceptions with openHAB 2. Have a look here: Recommended way to backup/restore OH2 configurations and things? - You can use config files or PaperUI or whatever else might be available to configure the internal database. These methods can also be mixed. openHAB will never change your config files. That’s a feature :wink:

Regarding your log, I’d be interested in the readings in the last few lines. I suspect they are continued like that on a regular basis? Something seems to be wrong with the way the script is working or executed by openhab (despite your previous findings…).

My advice: Move the execution (temporarily) into a rule. A rule gives you more flexibility and the full result of your execution. This way you can see what’s actually going on. Check the rule here: Speedtest-cli Internet Up-/Downlink Measurement Integration --> var String result = executeCommandLine("your command", 120*1000)


(dijit) #5

Thanks @ThomDietrich for taking the time to explain all of that. Like I said, I have a lot to learn, but I’m very excited for the possibilities of OH.

Also, great use of “That’s a feature, not a bug” :smile:

I will test my setup with a rule and report back my results.

Thanks again!


(dijit) #6

OK, I learned 2 new things today:

  1. Do not use “@@” in place of spaces in the command, it is not needed and
  2. For my particular application I should have used the output of the command (and not the exit value).

Now I get the correct reading in OpenHab, as verified in HAbmin, but I still cannot get the value to display properly in a sitemap:


This is Basic UI with sitemap: _default, so there is no file I can edit (that I am aware of). My custom sitemaps also do not report the Temperature reading.

I experimented with creating a Rule, as suggested, however I can now get valid readings and the script errors have been resolved, so I am back to using items and things.

Any guidance on getting the data to display in sitemaps?


( ) #7

Great you got that far. The result in BasicUI is normally shown when there is no or no valid value available. Please build your own sitemap and as always check the openhab logs for errors and value updates… sorry I can’t be of more help than that.


(dijit) #8

No apology necessary, you have been extremely helpful and I appreciate all the advice and guidance. I will continue working on this until I can get a working model and then make a tutorial.

Thanks again!


(dijit) #9

Tutorial Created:

I had some trouble with formatting though, as the “Heading” and “Bold” tags refused to work on a few sections. I will attempt to fix this with an edit.

Thanks again for the guidance @ThomDietrich


(Alexandr) #10

Why is the temperature value as String type?
It is not possible to operate whit it!
How to convert String type to Number type?


(dijit) #11

Replied over on the tutorial thread…