Install OpenHAB2 on emonSD / emonPi

I have done, yes. Though I find that openhab2 can be a little temperamental if it’s not got everything where it likes it… How did you install openhab2 and which version?

If you installed via the apt repository can you try (I assume you don’t have any openhab configuration you need to keep?):

apt-get --purge remove openhab2
rm -rf /etc/openhab2
rm -rf /var/lib/openhab2

Make sure openhab is not running:

ps -ef | grep "karaf"
ps -ef | grep "openhab"

should return no results

Then try reinstalling openhab2 and configure as above.
Start openhab:

systemctl start openhab2

Hopefully that’ll work…

I removed openhab-runtime from the image and then I installed OH2 according to: openHAB 2 on Linux - openHAB 2 - Empowering the Smart Home

wget -qO - 'https://bintray.com/user/downloadSubjectPublicKey?username=openhab' | sudo apt-key add -
echo 'deb http://dl.bintray.com/openhab/apt-repo2 stable main' | sudo tee /etc/apt/sources.list.d/openhab2.list

sudo apt-get update
sudo apt-get install openhab2
sudo apt-get install openhab2-addons

sudo systemctl start openhab2.service
sudo systemctl status openhab2.service
  
sudo systemctl daemon-reload
sudo systemctl enable openhab2.service

I’ll try your instructions right away.

Hmmm looks like I didn’t remove openHAB 1 like it should. The old service still exists… When stopping the service it looks like it keeps trying to re-activate.

pi@emonpi(rw):/$ sudo service openhab start
Job for openhab.service failed. See 'systemctl status openhab.service' and 'journalctl -xn' for                                   details.
pi@emonpi(rw):/$ sudo service openhab stop
pi@emonpi(rw):/$ sudo service openhab status
● openhab.service - Starts and stops the openHAB Home Automation Bus
   Loaded: loaded (/usr/lib/systemd/system/openhab.service; disabled)
   Active: failed (Result: exit-code) since Mon 2017-02-27 22:41:42 UTC; 11s ago
     Docs: http://www.openhab.org
  Process: 1169 ExecStartPre=/usr/share/openhab/bin/setpermissions.sh (code=exited, status=217/                                  USER)

Feb 27 22:41:42 emonpi systemd[1]: openhab.service: control process exited, code=exited s...217
Feb 27 22:41:42 emonpi systemd[1]: Failed to start Starts and stops the openHAB Home Auto...us.
Feb 27 22:41:42 emonpi systemd[1]: Unit openhab.service entered failed state.
Feb 27 22:41:51 emonpi systemd[1]: Stopped Starts and stops the openHAB Home Automation Bus.
Hint: Some lines were ellipsized, use -l to show in full.
pi@emonpi(rw):/$ sudo service openhab2 status
● openhab2.service
   Loaded: not-found (Reason: No such file or directory)
   Active: failed (Result: exit-code) since Mon 2017-02-27 22:35:36 UTC; 6min ago
 Main PID: 31662 (code=exited, status=255)

Feb 27 22:35:32 emonpi start.sh[31662]: Launching the openHAB runtime...
Feb 27 22:35:34 emonpi start.sh[31662]: /var/log/openhab2/openhab.log (No such file or directory)
Feb 27 22:35:34 emonpi systemd[1]: openhab2.service: main process exited, code=exited, status=255/n/a
Feb 27 22:35:34 emonpi stop[31828]: stop: Ignoring predefined value for KARAF_HOME
Feb 27 22:35:35 emonpi systemd[1]: Stopping openHAB 2 - empowering the smart home...
Feb 27 22:35:36 emonpi stop[31828]: /var/lib/openhab2/tmp/port shutdown port file doesn't exist. The container is not running.
Feb 27 22:35:36 emonpi systemd[1]: openhab2.service: control process exited, code=exited status=3
Feb 27 22:35:36 emonpi systemd[1]: Stopped openHAB 2 - empowering the smart home.
Feb 27 22:35:36 emonpi systemd[1]: Unit openhab2.service entered failed state.
Feb 27 22:36:36 emonpi systemd[1]: Stopped openhab2.service.

How did you clean the emonSD image from openHAB 1?

Shouldn’t actually need to uninstall it but it will need to be stopped I think.

Those logs are complaining that openhab2 can’t write to it’s log file. Can you try:

mkdir -p /var/log/openhab2 && chown openhab:openhab /var/log/openhab2

Since /var/log is mounted as tmpfs on the emonSD the logs are non-persistent and the dirs must be created on each boot.

We do this in /etc/rc.local which is symlinked to ~/emonpi/rc.local_jessieminimal

Thanks for your help @mattwire, I’m watching this thread closely. I’m keen to get OH2 running on emonSD and to release and upgrade. I started documenting my process here:

I need to revisit, to remember how far I got. Sorry, I’ve been busy today.

Using rc.local isn’t a good idea, not only is rc.local dependent on zero errors from all it’s entries to complete, it also doesn’t run untill after all the services are started, so then you have to restart any that have failed to start prior to the rc.local running, it undermines the whole purpose of the start up processes, dependencies and order etc.

Either the init.d script should create the folders as emonhub does or if using systemctl adding something like

PermissionsStartOnly=true
ExecStartPre=-/bin/mkdir -P /var/log/openhab2
ExecStartPre=/bin/chown -R openhab:openhab /var/log/openhab2/

before the ExecStart line in the service unit, or as in this case you already have an “ExecStartPre=/usr/share/openhab2/start_pre.sh” line in there you could simply add the log file creation to that additional file, although IMO I would possibly consider moving the contents of that script directly into the service unit to avoid the need for a separate script (and whether it’s executable) and to maximize on the systemctl error logging.

although IMO I would possibly consider moving the contents of that script directly into the service unit to avoid the need for a separate script (and whether it’s executable) and to maximize on the systemctl error logging.

@pb66 I like that idea, so you’d just add multiple ExecStartPre= lines to the systemctl script? What was the purpose of the “-” in the ExecStartPre=-/bin/mkdir… line in your example?

Yes, That was my thinking.

The minus means ignore any errors, in this instance, if the folder already exists.

Haven’t got it running (yet :slight_smile:)… Have you had any success Glyn?

Latest effort. You don’t need to configure any other scripts. Just modify the service file:
/usr/lib/systemd/system/openhab2.service:

[Unit]
Description=openHAB 2 - empowering the smart home
Documentation=http://docs.openhab.org
Documentation=https://community.openhab.org
Wants=network-online.target
After=network-online.target

[Service]
EnvironmentFile=/etc/default/openhab2
User=openhab
Group=openhab
WorkingDirectory=/usr/share/openhab2
PermissionsStartOnly=true
ExecStartPre=/bin/mkdir -p /var/log/openhab2
ExecStartPre=/bin/chown -R openhab:openhab /var/log/openhab2/
ExecStartPre=/bin/sh -c '/usr/bin/test -d /var/lib/openhab2/cache || /bin/mkdir /var/lib/openhab2/cache'
ExecStartPre=/bin/sh -c '/bin/mountpoint -q /var/lib/openhab2/cache || /bin/mount -t tmpfs tmpfs /var/lib/openhab2/cache'
ExecStartPre=/bin/sh -c '/usr/bin/test -d /var/lib/openhab2/tmp || /bin/mkdir /var/lib/openhab2/tmp'
ExecStartPre=/bin/sh -c '/bin/mountpoint -q /var/lib/openhab2/tmp || /bin/mount -t tmpfs tmpfs /var/lib/openhab2/tmp'
ExecStart=/usr/share/openhab2/start.sh server
ExecStop=/usr/share/openhab2/runtime/bin/stop
# Shutdown delay in seconds, before process is tried to be killed with KILL (if configured)
TimeoutStopSec=120
Restart=on-failure

[Install]
WantedBy=multi-user.target

Then do a:

systemctl daemon-reload
systemctl start openhab2

Once you have got it working, it would be a good idea to modify the logging config so you don’t run out of memory:
Edit /etc/openhab/etc/org.ops4j.pax.logging.cfg and change the maxFileSize and maxBackupIndex to match the entries below:

# File appender - openhab.log
log4j.appender.out=org.apache.log4j.RollingFileAppender
log4j.appender.out.layout=org.apache.log4j.PatternLayout
log4j.appender.out.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss.SSS} [%-5.5p] [%-36.36c] - %m%n
log4j.appender.out.file=${openhab.logdir}/openhab.log
log4j.appender.out.append=true
log4j.appender.out.maxFileSize=5MB
log4j.appender.out.maxBackupIndex=1

# File appender - events.log
log4j.appender.event=org.apache.log4j.RollingFileAppender
log4j.appender.event.layout=org.apache.log4j.PatternLayout
log4j.appender.event.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss.SSS} [%-26.26c{1}] - %m%n
log4j.appender.event.file=${openhab.logdir}/events.log
log4j.appender.event.append=true
log4j.appender.event.maxFileSize=5MB
log4j.appender.event.maxBackupIndex=1

Ok, I took a clean image, did sudo apt-get update & upgrade. All is working fine now.

Do I remove the openhab-runtime (1.8.xxx) before installing the 2.0 version according to: Raspberry Pi - openHAB 2 - Empowering the Smart Home and here: openHAB 2 on Linux - openHAB 2 - Empowering the Smart Home
Or should I should remove / disable the existing openhab service from emonSD?

sudo systemctl disable openhab.service

echo 'deb http://dl.bintray.com/openhab/apt-repo2 stable main' | sudo tee /etc/apt/sources.list.d/openhab2.list
sudo apt-get update
sudo apt-get install openhab2
sudo apt-get install openhab2-addons

 

sudo nano /usr/lib/systemd/system/openhab2.service

    [Unit]
    Description=openHAB 2 - empowering the smart home
    Documentation=http://docs.openhab.org
    Documentation=https://community.openhab.org
    Wants=network-online.target
    After=network-online.target

    [Service]
    EnvironmentFile=/etc/default/openhab2
    User=openhab
    Group=openhab
    WorkingDirectory=/usr/share/openhab2
    PermissionsStartOnly=true
    ExecStartPre=/bin/mkdir -p /var/log/openhab2
    ExecStartPre=/bin/chown -R openhab:openhab /var/log/openhab2/
    ExecStartPre=/bin/sh -c '/usr/bin/test -d /var/lib/openhab2/cache || /bin/mkdir /var/lib/openhab2/cache'
    ExecStartPre=/bin/sh -c '/bin/mountpoint -q /var/lib/openhab2/cache || /bin/mount -t tmpfs tmpfs /var/lib/openhab2/cache'
    ExecStartPre=/bin/sh -c '/usr/bin/test -d /var/lib/openhab2/tmp || /bin/mkdir /var/lib/openhab2/tmp'
    ExecStartPre=/bin/sh -c '/bin/mountpoint -q /var/lib/openhab2/tmp || /bin/mount -t tmpfs tmpfs /var/lib/openhab2/tmp'
    ExecStart=/usr/share/openhab2/start.sh server
    ExecStop=/usr/share/openhab2/runtime/bin/stop
    # Shutdown delay in seconds, before process is tried to be killed with KILL (if configured)
    TimeoutStopSec=120
    Restart=on-failure

    [Install]
    WantedBy=multi-user.target

 

sudo systemctl daemon-reload
sudo systemctl start openhab2
sudo systemctl status openhab2
sudo systemctl enable openhab2.service

sudo systemctl stop openhab2.service

 

sudo nano ./var/lib/openhab2/etc/org.ops4j.pax.logging.cfg

    # File appender - openhab.log
    log4j.appender.out=org.apache.log4j.RollingFileAppender
    log4j.appender.out.layout=org.apache.log4j.PatternLayout
    log4j.appender.out.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss.SSS} [%-5.5p] [%-36.36c] - %m%n
    log4j.appender.out.file=${openhab.logdir}/openhab.log
    log4j.appender.out.append=true
    log4j.appender.out.maxFileSize=5MB
    log4j.appender.out.maxBackupIndex=1

    # File appender - events.log
    log4j.appender.event=org.apache.log4j.RollingFileAppender
    log4j.appender.event.layout=org.apache.log4j.PatternLayout
    log4j.appender.event.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss.SSS} [%-26.26c{1}] - %m%n
    log4j.appender.event.file=${openhab.logdir}/events.log
    log4j.appender.event.append=true
    log4j.appender.event.maxFileSize=5MB
    log4j.appender.event.maxBackupIndex=1

sudo reboot

sudo systemctl enable openhab2.service
sudo systemctl start openhab2
    pi@emonpi(ro):~$ sudo systemctl status openhab2
    ? openhab2.service - openHAB 2 - empowering the smart home
       Loaded: loaded (/usr/lib/systemd/system/openhab2.service; enabled)
       Active: active (running) since Sat 2017-03-11 19:29:17 UTC; 10min ago
         Docs: http://docs.openhab.org
               https://community.openhab.org
      Process: 930 ExecStartPre=/bin/sh -c /bin/mountpoint -q /var/lib/openhab2/tmp || /bin/mount -t tmpfs tmpfs /var/lib/openhab2/tmp (code=exited, status=0/SUCCESS)
      Process: 908 ExecStartPre=/bin/sh -c /usr/bin/test -d /var/lib/openhab2/tmp || /bin/mkdir /var/lib/openhab2/tmp (code=exited, status=0/SUCCESS)
      Process: 892 ExecStartPre=/bin/sh -c /bin/mountpoint -q /var/lib/openhab2/cache || /bin/mount -t tmpfs tmpfs /var/lib/openhab2/cache (code=exited, status=0/SUCCESS)
      Process: 877 ExecStartPre=/bin/sh -c /usr/bin/test -d /var/lib/openhab2/cache || /bin/mkdir /var/lib/openhab2/cache (code=exited, status=0/SUCCESS)
      Process: 862 ExecStartPre=/bin/chown -R openhab:openhab /var/log/openhab2/ (code=exited, status=0/SUCCESS)
      Process: 844 ExecStartPre=/bin/mkdir -p /var/log/openhab2 (code=exited, status=0/SUCCESS)
     Main PID: 938 (karaf)
       CGroup: /system.slice/openhab2.service
               +- 938 /bin/bash /usr/share/openhab2/runtime/bin/karaf server
               +-1622 /usr/bin/java -Dopenhab.home=/usr/share/openhab2 -Dopenhab.conf=/etc/openhab2 -Dopenhab.runtime=/usr/share/openhab2/runtime -Dopenhab.userdata=/var/lib/openhab2 -Dopenhab.logdir=/var...

    Mar 11 19:29:40 emonpi start.sh[938]: at org.eclipse.osgi.internal.framework.EquinoxBundle$EquinoxModule.startWorker(EquinoxBundle.java:318)
    Mar 11 19:29:40 emonpi start.sh[938]: at org.eclipse.osgi.container.Module.doStart(Module.java:571)
    Mar 11 19:29:40 emonpi start.sh[938]: at org.eclipse.osgi.container.Module.start(Module.java:439)
    Mar 11 19:29:40 emonpi start.sh[938]: at org.eclipse.osgi.container.ModuleContainer$ContainerStartLevel.incStartLevel(ModuleContainer.java:1582)
    Mar 11 19:29:40 emonpi start.sh[938]: at org.eclipse.osgi.container.ModuleContainer$ContainerStartLevel.incStartLevel(ModuleContainer.java:1562)
    Mar 11 19:29:40 emonpi start.sh[938]: at org.eclipse.osgi.container.ModuleContainer$ContainerStartLevel.doContainerStartLevel(ModuleContainer.java:1533)
    Mar 11 19:29:40 emonpi start.sh[938]: at org.eclipse.osgi.container.ModuleContainer$ContainerStartLevel.dispatchEvent(ModuleContainer.java:1476)
    Mar 11 19:29:40 emonpi start.sh[938]: at org.eclipse.osgi.container.ModuleContainer$ContainerStartLevel.dispatchEvent(ModuleContainer.java:1)
    Mar 11 19:29:40 emonpi start.sh[938]: at org.eclipse.osgi.framework.eventmgr.EventManager.dispatchEvent(EventManager.java:230)
    Mar 11 19:29:40 emonpi start.sh[938]: at org.eclipse.osgi.framework.eventmgr.EventManager$EventThread.run(EventManager.java:340)

It is working right now. But looks like I need to put the FS in RW mode when installing bindings or UI/Actions etc. Otherwise they end op in an infinite loop.

And all of a sudden it stopped working. Didn’t change a thing, just left it for a week.

pi@emonpi(ro):~$ sudo service openhab2 status
● openhab2.service - openHAB 2 - empowering the smart home
   Loaded: loaded (/usr/lib/systemd/system/openhab2.service; enabled)
   Active: deactivating (stop) (Result: exit-code) since Sun 2017-03-19 14:07:56 UTC; 1s ago
     Docs: http://docs.openhab.org
           https://community.openhab.org
  Process: 10664 ExecStart=/usr/share/openhab2/start.sh server (code=exited, status=255)
  Process: 10658 ExecStartPre=/usr/share/openhab2/start_pre.sh (code=exited, status=0/SUCCESS)
 Main PID: 10664 (code=exited, status=255);         : 10826 (karaf)
   CGroup: /system.slice/openhab2.service
           └─control
             ├─10826 /bin/bash /usr/share/openhab2/runtime/bin/karaf stop
             └─10982 /usr/lib/jvm/java-8-oracle/jre/bin/java -Dopenhab.home=/usr/share/openhab2 -Dopenhab.conf=/etc/openhab2 -Dopenhab.runtime=/usr/share/openhab2/runtime ...

Mar 19 14:07:54 emonpi systemd[1]: Started openHAB 2 - empowering the smart home.
Mar 19 14:07:54 emonpi start.sh[10664]: Launching the openHAB runtime...
Mar 19 14:07:56 emonpi start.sh[10664]: Unable to update instance pid: /usr/share/openhab2/runtime/instances/instance.properties (Read-only file system)
Mar 19 14:07:56 emonpi start.sh[10664]: /var/log/openhab2/openhab.log (No such file or directory)
Mar 19 14:07:56 emonpi start.sh[10664]: Unable to update instance pid: /usr/share/openhab2/runtime/instances/instance.properties (Read-only file system)
Mar 19 14:07:56 emonpi systemd[1]: openhab2.service: main process exited, code=exited, status=255/n/a
Mar 19 14:07:56 emonpi stop[10826]: stop: Ignoring predefined value for KARAF_HOME





pi@emonpi(ro):~$ sudo service openhab2 stop
pi@emonpi(ro):~$ rpi-rw
Filesystem is unlocked - Write access
type ' rpi-ro ' to lock
pi@emonpi(rw):~$ sudo service openhab2 start
pi@emonpi(rw):~$ sudo service openhab2 status
● openhab2.service - openHAB 2 - empowering the smart home
   Loaded: loaded (/usr/lib/systemd/system/openhab2.service; enabled)
   Active: deactivating (stop) (Result: exit-code) since Sun 2017-03-19 14:08:53 UTC; 1s ago
     Docs: http://docs.openhab.org
           https://community.openhab.org
  Process: 14848 ExecStart=/usr/share/openhab2/start.sh server (code=exited, status=255)
  Process: 14842 ExecStartPre=/usr/share/openhab2/start_pre.sh (code=exited, status=0/SUCCESS)
 Main PID: 14848 (code=exited, status=255);         : 15010 (karaf)
   CGroup: /system.slice/openhab2.service
           └─control
             ├─15010 /bin/bash /usr/share/openhab2/runtime/bin/karaf stop
             ├─15152 /bin/bash /usr/share/openhab2/runtime/bin/karaf stop
             ├─15153 /usr/lib/jvm/java-8-oracle/jre/bin/java -version
             ├─15154 grep -E "([0-9].[0-9]\..*[0-9]).*"
             ├─15155 awk {print substr($3,2,length($3)-2)}
             ├─15156 awk {print substr($1, 3, 3)}
             └─15157 sed -e s;\.;;g

Mar 19 14:08:51 emonpi systemd[1]: Started openHAB 2 - empowering the smart home.
Mar 19 14:08:51 emonpi start.sh[14848]: Launching the openHAB runtime...
Mar 19 14:08:53 emonpi start.sh[14848]: /var/log/openhab2/openhab.log (No such file or directory)
Mar 19 14:08:53 emonpi systemd[1]: openhab2.service: main process exited, code=exited, status=255/n/a
Mar 19 14:08:54 emonpi stop[15010]: stop: Ignoring predefined value for KARAF_HOME

@glyn.hudson Have you had succes with Matthew’s steps?

Learnt last night that the repository on GitHub is work-in-progress, rather a working solution. Silly me!

I’ve just deployed a solution loosely based upon the steps on the GitHub page into an emonpi image with most of emoncms moved away. I’ve added symbolic links to /etc/openhab2 and /var/lib/openhab2 to move those files into the data partition so all the config files sit in the same place and I don’t need to repeatedly change into RW mode whenever I edit. I haven’t put /var/lib /openhab2 into tmpfs.
I tried the manual install into a folder in the data partition which didn’t work, before I installed with apt-get and then added the links.
Everything is good and a little bit faster than OH 1.8 so far. The rules needed minor tweaks, and the GPIO binding didn’t work reliably, so I switched to calling wiringPi from a rule. The GPIO problem may be down to the read only file system as it kept complaining about a “lock,” but as I only operate relays with GPIO its easy to do that another way.
I’ll update this thread as I go, and once I’ve had chance to prove its reliability.
@fluppie007 have you tried scheduling a regular reboot to get around the problems? My OpenHAB 1.8 needed to be booted twice a week to prevent errors in the user interface, usually at a bad time, so as I’ve got a LOT of items I just scheduled a regular reboot. My other V1.8 Pi which doesn’t get much user interface use didn’t seem to have the problem.
Interestingly the file OH2 was complaining about in the post above references /var/lib/openhab2, and I think that’s rerouted to tmpfs? could that folder be full and its trying to move it elsewhare but can’t due to the read only file system?
Interestingly enough mine has the access date of right now on that file, even though the FS is mounted RO, so perhaps I will get the problem after a few days despite the reboots.
My /usr/share/openhab2/runtime/instances/instance.properties contains

count = 1
item.0.name = main
item.0.loc = /var/lib/openhab2
item.0.pid = 0
item.0.root = true

and I reboot with this rule

rule "Reboot downstairs Pi"
when Time cron "0 50 3 ? * WED,SAT" then	{	executeCommandLine('"sudo" "reboot"', 5000)	}
end

I can’t get openHab2 running. I’ve removed openHab and followed fluppie007 and Glyn’s advice above to install openHab2, but it is still not working. I’m not clear on what ‘install via deb:’ means, or how to load that code starting with ‘mount’.
Thanks in advance.

@mattwire
I am a user of openHAB2 for a year now, and i am interested in including energy monitoring, both usage and solar pannel output (SolarEdge). So i am following this thread for some time and waiting for a solution befor aquireing an emonPI. (I definately do not want to go back to OH1 anymore and do not have the time to troubleshoot this problem myself.)

Can you please tell me what the status is of an emonSD image with openhab2 included?
Kind regards, Bert

@deltabert I don’t know what the official stance is and Openhab2 has been somewhat of a moving target. I am running openhab2 (snapshot 2.2.0) daily on a Pi controlling my home, it’s installed roughly according to the instructions I posted but they have changed a little bit and I haven’t got around to documenting what has changed.

Thanks for your reply @mattwire.
I’m hung up on implementing the code you posted on Dec. 16
As follows: (the mount commands are hanging me up)

mount -t tmpfs tmpfs /var/lib/openhab2/cache
mount -t tmpfs tmpfs /var/lib/openhab2/tmp
mount -t tmpfs tmpfs /var/lib/openhab2/log
rm /var/lib/openhab2/port && ln -s /var/lib/openhab2/tmp/port /var/lib/openhab2/port
rm -rf /var/lib/openhab2/lock && ln -s /var/lib/openhab2/tmp/lock /var/lib/openhab2/lock
rm -rf /var/lib/openhab2/persistence && ln -s /mnt/rwdata/openhab2/persistence /var/lib/openhab2/persistence
mv /var/lib/openhab2/etc /etc/openhab2/etc
ln -s /etc/openhab2/etc /var/lib/openhab2/etc
mv /var/lib/openhab2/jsondb /etc/openhab2/jsondb
ln -s /etc/openhab2/jsondb /var/lib/openhab2/jsondb