Ok here is the process I have written up. I want to help those who have no idea what to do and also give them access to the Hubitat using OpenVPN too. Anyone want to try it? The code I am using is attached here as well.
app.js code:
var sleep = require('sleep');
var harmony = require('harmonyhubjs-client')
var request = require('request')
const makerLink = 'http://<your_hubitat_ip>/apps/api/<your_MAKERAPI_ID>/devices?access_token=<your_MAKERAPI_access_token>'
const activityXref = //create an array variable to associate activity IDs from Harmony to switch IDs from Hubitat
[
['<Harmony Activity ID>','<HARMONY_DEVICE_MAKER_ID>'], //Activity 1
['<Harmony Activity ID>','<HARMONY_DEVICE_MAKER_ID>'], //Activity 2
['<Harmony Activity ID>','<HARMONY_DEVICE_MAKER_ID>'], //Activity 3
]
const harmonyIP = '<your_harmony_ip>'
harmony(harmonyIP).then(function(harmonyClient) {
console.log('hub client started for ' + harmonyIP)
harmonyClient.getActivities()
.then(function (activities) {
console.log('activities found on hub')
console.log('-----------------------')
var i
for(i = 0; i < activities.length; i++) {
if(activities[i].label != 'PowerOff') {
console.log(activities[i].id + ' (' + activities[i].label + ')')
}
}
console.log('---------------------------')
console.log('listening for state digests')
})
! function keepAlive(){
harmonyClient.request('getCurrentActivity').timeout(5000).then(function(response) {
setTimeout(keepAlive, 45000);
}).catch(function(e){
//disconnected from hub
});
}();
harmonyClient.on('stateDigest', function(digest) {
var statusCode = digest.activityStatus
var currentlyOn = digest.runningActivityList
var targetActivity = digest.activityId
console.log('***received state digest***')
if (statusCode == '0') { //Hub is off`
if(currentlyOn == '' && targetActivity == '-1') {
console.log('hub is off')
} else {
console.log('activity ' + currentlyOn + ' is now off')
changeSwitch(currentlyOn, 'refresh')
}
}
if (statusCode == '1') { //Activity is starting
console.log('activity is starting')
}
if (statusCode == '2') { //Activity is started
if(currentlyOn == targetActivity) {
console.log('activity is started')
} else {
if(currentlyOn == '') {
console.log('activity ' + targetActivity + ' is now on')
changeSwitch(targetActivity, 'refresh')
}
else {
console.log('activity ' + targetActivity + ' is now on')
changeSwitch(targetActivity,'refresh')
console.log('activity ' + currentlyOn + ' is now off')
changeSwitch(currentlyOn,'refresh')
}
}
}
if (statusCode == '3') { //Hub is turning off
console.log('hub is turning off')
}
})
}).catch(function(e){
console.log('error')
});
function changeSwitch (activityId, changeTo) {
sleep.sleep(3)
var activityCount = activityXref.length
var i
var switchId = ""
var tokenLabelPos = makerLink.indexOf('access_token')
var makerURL = makerLink.substring(0,tokenLabelPos - 1)
var makerToken = makerLink.replace(makerURL,'')
for(i = 0; i < activityCount; i++) {
if (activityXref[i][0] == activityId) {
switchId = activityXref[i][1]
console.log('xref activity ' + activityId + ' -> switch ' + switchId)
}
}
var options = {
uri: makerURL + "/" + switchId + "/" + changeTo + makerToken,
//uri: makerURL + "/" + switchId + "/Refresh/" + makerToken,
method: 'GET',
json: true
}
request(options, function(error, response, body) {
if (!error && response.statusCode == 200) {
console.log("sending " + changeTo + " command to switch " + switchId)
//console.log(body)
}
})
}
The Setup Script:
Flash Image Raspi-lite using Etcher
passwd pi
sudo passwd root
sudo apt-get update
sudo apt-get dist-upgrade
Install Samba, Winbind and Share
sudo apt-get install samba samba-common-bin
sudo apt-get install winbind
sudo mkdir -m 1777 /share
sudo nano/etc/samba/smb.conf
(add to bottom of file)
[share]
Comment = Pi shared folder
Path = /share
Browseable = yes
Writeable = Yes
only guest = no
reate mask = 0777
directory mask = 0777
Public = yes
Guest ok = yes
sudo smbpasswd -a pi
sudo /etc/init.d/samba restart
sudo nano /etc/nsswitch.conf
(add wins to the hosts: line)
sudo nano /etc/hostname
(change to your hostname preference)
sudo nano /etc/hosts
(change to your hostname preference)
sudo reboot
Install OpenVPN (optional)
curl -L https://install.pivpn.io | bash
(follow all basic recommendations)
sudo mkdir /share/ovpns
sudo reboot
Setup Certs for OpenVPN (optional)
pivpn -a
(enter client name and password; repeat as necessary for each client)
sudo cp ~/ovpns/* /share/ovpns
Install Node.JS
curl -sL https://deb.nodesource.com/setup_11.x | sudo -E bash -
sudo apt install -y nodejs
Install Forever and forever-service (must be SuperUser)
su
npm install -g forever
npm install -g forever-service
Install Sleep Service (must be SuperUser)
su
cd /share/harmony
npm install sleep --save
Install HarmonyHub Real-time Updates
mkdir /share/harmony
cd /share/harmony
su
npm install harmonyhubjs-client --save
Setup Hubitat for HarmonyHub Real-time Updates with use of Logitech Harmony (Connect) [https://github.com/mattw01/HubitatHarmony]
http://portal.hubitat.com
Install MakerAPI in Apps
Add Harmony Devices installed by Logitech Harmony (Connect) App
Note MakerAPI ID and Access Token "http://<your_hubitat_ip>/apps/api/[MAKER_API]/devices?access_token=<your_token>"
Click "Get All Devices" to acquire Hubitat ID for your Harmony Devices
Continue Setup
cd /share/harmony
nano app.js
(copy raw code from code repository)
- Change makerLink with your information above
- Change Activity section ['harmony activity','MAKER_ID activity'] per activity. Delete any additional activities or add accordingly
- Change HarmonyIP to reflect your Harmony IP
- save/exit
node app.js
(take note of activity name and activity #)
CTRL-C
nano app.js
(modify each activity to coorespond with MAKER_ID activity)
node app.js
(if no errors test your harmony hub devices and monitor realtime updates in Hubitat)
Set service to run on boot as a service
cd /share/harmony
sudo forever-service install harmonyhub
sudo service harmonyhub status
(if no errors test your harmony hub devices and monitor realtime updates in Hubitat)