# Generic Presence Detection

**URL:** <https://community.openhab.org/t/generic-presence-detection/17878>\
**Category:** Tutorials & Examples\
**Tags:** presence\
**Created:** [December 7, 2016, 11:14pm UTC](https://community.openhab.org/t/generic-presence-detection/17878 "2016-12-07T23:14:44Z")\
**Posts on this page:** 20\
**Page:** 3

<div class="post-metadata">

**Author:** ![ei\_Gelb\_Geek](https://community.openhab.org/user_avatar/community.openhab.org/ei_gelb_geek/32/14503_2.png) [@ei\_Gelb\_Geek](https://community.openhab.org/u/ei_Gelb_Geek)\
**Post date:** [January 7, 2018, 8:41pm UTC](https://community.openhab.org/t/generic-presence-detection/17878/41 "2018-01-07T20:41:01Z")

</div>

> [@rlkoshak](#):
>
> else if(gPresent.state == OFF and Present.state != OFF){

Little Typo … and → &&

---

<div class="post-metadata">

**Author:** ![RayJay](https://community.openhab.org/letter_avatar_proxy/v4/letter/r/aca169/32.png) [@RayJay](https://community.openhab.org/u/RayJay)\
**Post date:** [March 9, 2018, 6:53pm UTC](https://community.openhab.org/t/generic-presence-detection/17878/42 "2018-03-09T18:53:36Z")

</div>

Sorry, I might be totally wrong over here but I am highly interested into integrating a Dexcom G5 into openhab. There might be a chance via the share-account…cause the pebble gets the information that way as well.

Unfortunately I do not have a clue on how to integrate it. But the idea is just to activate a light while glucose level becomes critical. That might help a lot!

I cannot find anything within the web but very complicated solutions via nightscout…and I think it would be pretty much easier to get the information directly from dexcom’s share server and integrate it.

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [March 9, 2018, 10:22pm UTC](https://community.openhab.org/t/generic-presence-detection/17878/43 "2018-03-09T22:22:51Z")

</div>

This is an intriguing idea. My wife (the one with the Dexcom) had her’s integrated with the Pebble for awhile but now has an Apple Watch with the direct integration with her iPhone (no more little blue and black receiver 🙂 ).

As far as I can tell, Dexcom discontinued their support for Share:

[https://www.dexcom.com/faq/dexcom-share/dexcom-share-app](https://www.dexcom.com/faq/dexcom-share/dexcom-share-app)

and I’m pretty sure the G5 doesn’t use any Dexcom cloud servers at all. Though that doesn’t make much sense as there is still the Follow app.

[google google google]

Aha! I found a REST API.

> **[Dexcom API | Home](https://developer.dexcom.com/overview)**

Looking around, this should be very doable. It probably warrants its own binding but it should be feasible to make something work with the HTTP binding and/or sendHttp actions. It would look a whole lot like the [iCloud integration prototype](https://community.openhab.org/t/icloud-device-data-integration-in-openhab/32329)

It might even be possible to make just one binding that doesn’t require every user to create a developer account with Dexcom. That would be really cool.

This is really exciting. I wish I had more time to work on this. I’ll have to talk to my wife to see if she is willing to be a guinea pig (she is pretty well controlled and has good hypo and hyper awareness so will not be gung-ho to have her CGM linked to the home automation).

Of course, if you or anyone else wants to run with this that would be awesome. I’ve not much time to work on something like this. If not I’ll start to chip away at it as my time allows.

Thanks for posting!

---

<div class="post-metadata">

**Author:** ![RayJay](https://community.openhab.org/letter_avatar_proxy/v4/letter/r/aca169/32.png) [@RayJay](https://community.openhab.org/u/RayJay)\
**Post date:** [March 11, 2018, 5:55pm UTC](https://community.openhab.org/t/generic-presence-detection/17878/44 "2018-03-11T17:55:08Z")

</div>

Well as far as I am able interpreting it the share servers still do exist. The pebbles in my household are still working via their share accounts. So from that point of view I think that there must be a simple way integrating it.

I had a closer look to the site you mentioned. Well I am able to understand the logic but I am not able turning it into a code. My programming experiences are quite old…

The idea is just getting logging in via the http-binding, and getting the glucose level. As far as it igets underneath a certain value, let’s say 65mg% turn a light into red. As soon as it rises above, turn to the last state.

Hypo awareness is great. But one thing is certain: Uncertain things occur 😉

If you could give me some clues, or a very basic solution I would be very happy.

Thank you 🙂

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [March 11, 2018, 7:12pm UTC](https://community.openhab.org/t/generic-presence-detection/17878/45 "2018-03-11T19:12:03Z")

</div>

Your best best will be too read up the docs at that link and look at the iCloud integration link for an example that will work very similarly.

The way these REST APIs work is to create an account to get a token you use in your http commands. Then it is a matter of making the right http commands with the right body and format to send and receive.

---

<div class="post-metadata">

**Author:** ![RayJay](https://community.openhab.org/letter_avatar_proxy/v4/letter/r/aca169/32.png) [@RayJay](https://community.openhab.org/u/RayJay)\
**Post date:** [March 11, 2018, 7:38pm UTC](https://community.openhab.org/t/generic-presence-detection/17878/46 "2018-03-11T19:38:17Z")

</div>

Okay. Thanks. I will try to do so.

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [June 19, 2018, 9:17pm UTC](https://community.openhab.org/t/generic-presence-detection/17878/47 "2018-06-19T21:17:25Z")

</div>

Over the past few months I’ve spent a few minutes here and there working with the Dexcom API, trying to make it work with OH Rules.

I finally have it almost working with their sandbox and I’ve run into this nice little footnote:

> Data from the Dexcom API is available with a three-hour delay.

So for all intents and purposes the available data through the Dexcom API is useless in a home automation context.

Oh well, it was a good thought.

---

<div class="post-metadata">

**Author:** ![RayJay](https://community.openhab.org/letter_avatar_proxy/v4/letter/r/aca169/32.png) [@RayJay](https://community.openhab.org/u/RayJay)\
**Post date:** [June 19, 2018, 9:55pm UTC](https://community.openhab.org/t/generic-presence-detection/17878/48 "2018-06-19T21:55:58Z")

</div>

Well…I can’t believe it cause there seem to be several real time apps available. Or are you trying the sandbox data only? That might use a delay for…whatever reason. Perhaps in generating artifical data. All the pebble watch faces for example are using the api…in real time. Try to shift your example app to your wife’s share account and see what happens.

Unfortunately I failed with the oauth.. .otherwise I would really like to waste some time with it.

If you would disclose your code I could try it as well (well… I am a real dilettante). But I would be very interested in that.

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [June 19, 2018, 10:03pm UTC](https://community.openhab.org/t/generic-presence-detection/17878/49 "2018-06-19T22:03:02Z")

</div>

It’s the result of a compromise with the FDA. The FDA insists that apps that use Dexcom’s API cannot be used to make treatment decisions.

> Data Availability  
> Data from the Dexcom API is available with a three-hour delay. This delay is enforced on the data upload time, not on the timestamps of individual data points. The G5® and G6® Mobile applications upload once per hour, so the data will (on average) be 3.5 hours delayed. On the other hand, data uploaded directly from a receiver over USB (via the Dexcom CLARITY® uploader) is available immediately because it is viewed as an active, rather than passive, upload. For more information on this delay, please see the FAQ.  
> [Dexcom API | Home](https://developer.dexcom.com/endpoint-overview)

and

> **[Dexcom API | Home](https://developer.dexcom.com/content/frequently-asked-questions)**

See “Can apps get real-time estimated glucose values (EGVs) with the Dexcom API?”

> [@RayJay](#):
>
> All the pebble watch faces for example are using the api…in real time.

I believe Dexcom only supports Apple Watches officially for real time data using their app. Pebble support was done through a hack (NightWatch or NightScout) or if I remember correctly, required a “Dexcom → phone → cloud → back to phone → pebble” communication path and that path used Dexcom’s internal APIs not the public API.

> [@RayJay](#):
>
> If you would disclose your code I could try it as well (well… I am a real dilettante). But I would be very interested in that.

I’m posting a tutorial on the OAuth part. Stay tuned.

> [@OAuth2 using just OH Rules and myopenhab.org](https://community.openhab.org/t/oauth2-using-just-oh-rules-and-myopenhab-org/46680):
>
> On prompting from a user on another thread I’ve spend a little bit of time here and there over the past few months trying to get integration with the Dexcom API. Unfortunately, after getting it almost all the way working I discovered that the data from the sensor is delayed by 3.5 hours, making it useless in a home automation context. But, the API does use OAuth2 for authentication and I managed to figure out how to do that so it isn’t a total loss. OAuth2 [image] OAuth2 is a standard protoco…

---

<div class="post-metadata">

**Author:** ![RayJay](https://community.openhab.org/letter_avatar_proxy/v4/letter/r/aca169/32.png) [@RayJay](https://community.openhab.org/u/RayJay)\
**Post date:** [June 28, 2018, 7:23pm UTC](https://community.openhab.org/t/generic-presence-detection/17878/50 "2018-06-28T19:23:14Z")

</div>

Hi Rich

Sorry, I have been completely offline during the last two weeks.

It might be right that there is a delay of 3h or more via the api.

But nevertheless I can tell you that the Pebbles in use got their information in (near) real time. They are using the/an api which is supported by dexcom. They are using the so called “share account”. Although Dexcom once announced they finished the “share service” they decided in continuing it.

They are using the way watchface → cell phone → pebble app → internet / share account → pebble app → watchface.

To give an example of a working watchface: [https://apps.getpebble.com/en\_US/application/56534d58d2d67de36d00005f](https://apps.getpebble.com/en_US/application/56534d58d2d67de36d00005f)

Well and of course those result of hacking hobbyists might result in accountability risks…that’s why they let you know “not for treatment decisions”. Nevertheless it is always a question where you are going to use it for. I thought it to be a good idea having flickering lights within your home if there seems to be something extraordinary.

Those guys doing diy closed loop systems are using the same way afaik…

So there might be a second api.

Dexcom supports apple as well as android watches. The usage of pebble is indeed a thing developed by ambitious hobbyists. From everyday usage I cant tell you that the pebble solution is as reliable as the apple or android one. The big advantage in reliability is that you do not have to recharge it once or twice a day. Makes you much more independent. The only little drawback is that you need internet connection cause of the way via dexcom share → cell phone and not app → pebble app directly. But that’s in most of the cases neglectable, especially while the watchfaces tell you the age of the last measurement in min.

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [June 28, 2018, 11:02pm UTC](https://community.openhab.org/t/generic-presence-detection/17878/51 "2018-06-28T23:02:00Z")

</div>

> [@RayJay](#):
>
> But nevertheless I can tell you that the Pebbles in use got their information in (near) real time.

As did my wife’s but that wasn’t through the official API. It was through the rube-goldberg system set up to make systems like night watch work.

As far as I can tell there is no officially sanctioned way for us to get the numbers in real time. Doesn’t mean there is no way, we just can’t do it through the official API.

I’m pretty sure those faces do not work through the official Dexcom apis. I could be wrong but at the time (this was year ago) it seemed to work using screen scraping.

> [@RayJay](#):
>
> From everyday usage I cant tell you that the pebble solution is as reliable as the apple or android one.

That wasn’t our experience which is why we switched to Apple watch. And when Pebble stopped being supported we had no hope that it would ever get better. YMMV.

> [@RayJay](#):
>
> The only little drawback is that you need internet connection cause

Which was also a problem when we are out hiking in the mountains and such where cell signal is along way away. The newer unit take straight to the phone and if one has a general Apple watch the watch take directly to the sensor and you don’t even need the phone.

---

<div class="post-metadata">

**Author:** ![RayJay](https://community.openhab.org/letter_avatar_proxy/v4/letter/r/aca169/32.png) [@RayJay](https://community.openhab.org/u/RayJay)\
**Post date:** [June 29, 2018, 6:51am UTC](https://community.openhab.org/t/generic-presence-detection/17878/52 "2018-06-29T06:51:57Z")

</div>

Thanks Rich. I was just dreaming of a simple solution which was that obvious and close that we just did not recognize it 😉

Perhaps they might offer their api being in real time in the future…

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [June 29, 2018, 1:54pm UTC](https://community.openhab.org/t/generic-presence-detection/17878/53 "2018-06-29T13:54:20Z")

</div>

> [@RayJay](#):
>
> Perhaps they might offer their api being in real time in the future…

One can hope. But from what I can tell on their site and the various forums it wasn’t their decision. It was the only way that the FDA would allow them to offer their API at all. Perhaps users in other countries will have better luck.

It would have been nice to get the real time information to drive devices though. Create an announcement, light up a colored bulb, etc.

I’m sure it could be done with NightScout et al given their open source nature so if someone were motivated that could be an option to look into.

I can at least give Dexcom this much credit. They at least have an API. I could find nothing about a Medtronic API.

---

<div class="post-metadata">

**Author:** ![RayJay](https://community.openhab.org/letter_avatar_proxy/v4/letter/r/aca169/32.png) [@RayJay](https://community.openhab.org/u/RayJay)\
**Post date:** [July 3, 2018, 4:22pm UTC](https://community.openhab.org/t/generic-presence-detection/17878/54 "2018-07-03T16:22:55Z")

</div>

…well, there seems to be a way. If you follow one of the dexcom-pebble-watch-faces…you will find the way..

Just try a little simple reverse engineering. You will get to the share server… I do not know wether you are using the dexcom companion app. this is an official app for supporting family members or medical staff… and this is using the share server / share service.

Just a snippet of the pebble watch face code:

```csv
//use D's share API------------------------------------------//
function share(options) {

    if (options.unit == "mgdl" || options.unit == "mg/dL")
    {
        fix = 0;
        options.conversion = 1;
        options.unit = "mg/dL";
        
    } else {
        fix = 1;
        options.conversion = 0.0555;       
        options.unit = "mmol/L";
    }
    
    var host = "share1";
    if (options.region != 'outside') {
        host = "share1";
    } else {
        host = "shareous1";
    }
    options.vibe = parseInt(options.vibe, 10);
    var defaults = {
        "applicationId": "d89443d2-327c-4a6f-89e5-496bbb0317db"
        , "agent": "Dexcom Share/3.0.2.11 CFNetwork/711.2.23 Darwin/14.0.0"
        , login: 'https://' + host + '.dexcom.com/ShareWebServices/Services/General/LoginPublisherAccountByName'
        , accept: 'application/json'
        , 'content-type': 'application/json'
        , LatestGlucose: "https://"+ host + ".dexcom.com/ShareWebServices/Services/Publisher/ReadPublisherLatestGlucoseValues"
    };

    authenticateShare(options, defaults);
}

function authenticateShare(options, defaults) {   
 
    var body = {
        "password": options.password
        , "applicationId": options.applicationId || defaults.applicationId
        , "accountName": options.accountName
    };

    var http = new XMLHttpRequest();
    var url = defaults.login;
    http.open("POST", url, true);
    http.setRequestHeader("User-Agent", defaults.agent);
    http.setRequestHeader("Content-type", defaults['content-type']);
    http.setRequestHeader('Accept', defaults.accept);
    
    var data;
    http.onload = function (e) {
        if (http.status == 200) {
            data = getShareGlucoseData(http.responseText.replace(/['"]+/g, ''), defaults, options);
        } else {
                sendAuthError();           
        }
    };
    
       http.ontimeout = function () {
        sendTimeOutError(options);
    };
    
    http.onerror = function () {
        sendServerError(options);
    };

    http.send(JSON.stringify(body));

}

```

This is be the result of reverse engineering of the official app…and this is in fact real time.

You can find the entire code within [https://github.com/tynbendad/simplecgmanalog](https://github.com/tynbendad/simplecgmanalog) if you are downloading the zip-file within the pebble-js-app.js file. And this is in fact no rube-goldberg-machine or algorithm.

What do you think about it? I am quite happy seing it.

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [July 3, 2018, 5:16pm UTC](https://community.openhab.org/t/generic-presence-detection/17878/55 "2018-07-03T17:16:39Z")

</div>

If you have the time I say give it a try. I just remember for the G4 with my wife’s pebble the path was the Sensor-\>CGM-\>iPhone-\>Dexcom Cloud-\>iPhone Pebble App-\>Pebble

If any link in that chain broke then no readings. NightScout is even a longer chain (or it was last time I saw it IIRC):

Sensor-\>CGM-\>Android (iOS wasn’t supported at the time I think)-\>Dexcom Cloud-\>Android-\>NightScout Server-\>Android-\>Watch Face

For some reason, the BT connection to the Pebble was flaky and the we are out of cell network far to frequently for it to be a viable option much of the time. When FitBit bought and discontinued support for Pebble we realized the BT problems would never get better.

Anyway, long story apropos to nothing.

This posting on the NightScout github might be a tad more complete:

> <https://github.com/nightscout/share2nightscout-bridge/issues/15>
>
> I'd like to access the data in other places, such as a Query Table in Microsoft …Excel. Could you add a file that documents the Share API?

It’s three years old so it might not be kept up to date. But there are curl examples and everything. The hard part will be constructing the HTTP requests which is already done in the OAuth2 tutorial I posted.

If you’ve the time I say go for it. I won’t have the time to look back into this at least until September.

---

<div class="post-metadata">

**Author:** ![RayJay](https://community.openhab.org/letter_avatar_proxy/v4/letter/r/aca169/32.png) [@RayJay](https://community.openhab.org/u/RayJay)\
**Post date:** [July 3, 2018, 5:54pm UTC](https://community.openhab.org/t/generic-presence-detection/17878/56 "2018-07-03T17:54:44Z")

</div>

Ok thanks. Perhaps I will have some time (and spirit) trying a little on our summer holidays. Thanks so far.

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [July 3, 2018, 6:27pm UTC](https://community.openhab.org/t/generic-presence-detection/17878/57 "2018-07-03T18:27:09Z")

</div>

Please do report your progress if you do tackle it.

---

<div class="post-metadata">

**Author:** ![Nick\_Moad](https://community.openhab.org/user_avatar/community.openhab.org/nick_moad/32/42326_2.png) [@Nick\_Moad](https://community.openhab.org/u/Nick_Moad)\
**Post date:** [September 1, 2018, 10:51pm UTC](https://community.openhab.org/t/generic-presence-detection/17878/58 "2018-09-01T22:51:11Z")

</div>

Thank you for this code, I added a manual over ride switch for the babysitter or guests. This is assuming I don’t use zwave 3in1 sensors as presence sensors…not sure yet but it was fun to play with the code anyway as I am new to openHAB.

I added the manual switch item (probably doesn’t really need to be in the group switch but it didn’t work by just setting that switch to ON when in the group switch):

```csv
Group:Switch:AND(OFF,ON) gManualPresent <present>
Switch Manual_Present <present> // master manual present switch

```

then added the below in to the “gpresent updated” rule

```php
 // no one is home
    else if(gPresent.state == OFF && Present.state != OFF && Manual_Present.state == OFF)

```

then another rule

```php
rule "Manual Present Switch Turned Off"
when
    Item Manual_Present received command OFF
then
    Present_Timer.sendCommand(ON)
end

```

The idea being that present switch won’t turn OFF if manual over ride is on. Then when I turn it off it just sets the presence timer on to resume normal operation.

Thanks again!

---

<div class="post-metadata">

**Author:** ![vzorglub](https://community.openhab.org/user_avatar/community.openhab.org/vzorglub/32/17166_2.png) [@vzorglub](https://community.openhab.org/u/vzorglub)\
**Post date:** [September 1, 2018, 11:29pm UTC](https://community.openhab.org/t/generic-presence-detection/17878/59 "2018-09-01T23:29:15Z")

</div>

Thanks @Nick_Moad  
Good idea  
Could you edit your post and add the [code fences](https://community.openhab.org/t/how-to-use-code-fences/38383), please?  
Thanks

---

<div class="post-metadata">

**Author:** ![Nick\_Moad](https://community.openhab.org/user_avatar/community.openhab.org/nick_moad/32/42326_2.png) [@Nick\_Moad](https://community.openhab.org/u/Nick_Moad)\
**Post date:** [September 2, 2018, 11:55am UTC](https://community.openhab.org/t/generic-presence-detection/17878/60 "2018-09-02T11:55:51Z")

</div>

After thinking about this, it is probably not required to integrate it with the timers above as you could just set any rules that use the presence to check “manual presence != ON”

[Previous page](https://community.openhab.org/t/generic-presence-detection/17878.md?page=2)

[Next page](https://community.openhab.org/t/generic-presence-detection/17878.md?page=4)
