iCloud device data integration in openHAB

Any thoughts on if this will work with 2 factor auth ?

… to my own surprise it seems to work.

with kind regards,
Patrik

1 Like

Hi!

Would be cool if the position could be shown on a Street map e.g. in Habpanel - any ideas?

Herbert

Any idea if you have multiple apple devices ?
I can’t get it to work, and i suspect that having many devices, might be the case.

Hi Robert,

yes - if you have multiple devices some small adjustments are required. For each device you´ll get a “content” in the JSON response. You can extract the location of the device by using the index in the JSON path. Below example will get the location of the 2nd device:

   logInfo(".rules", sb.toString())
   
   var String _longitude = transform("JSONPATH", "$.content[1].location.longitude", sb.toString())
   var String _latitude = transform("JSONPATH", "$.content[1].location.latitude", sb.toString())
   var String _positionString = _latitude + "," + _longitude

1st add the logInfo to your rule to check the response (I recommend an online json viewer); then you should be able to find yout which device has what index. As a second step adjust the JSON path accordingly. You can also extract the location of more than one device for the response of course.

let me know if this helped, or if you need more details.

with kind regards,
Patrik

I do not know about habpanel - but this might help you the get started (I use ii in basic UI):

let me know if you need help with the map example (maybe in the other topic); of course I also like to know if it works :slight_smile:

with kind regards,
Patrik

@patrik_gfeller, this is great. We were able to retrieve some nice data from the API. You should think about renaming the thread to something like “iCloud device data integration in openHAB”.

As promised I did some digging myself and cam up with a few enhancements and corrections. The following is your code but extended. Please feel free to copy it to the first posting, I’ll redact it here later.

[… redacted …]

2 Likes

This looks excellent!! I’ve been looking for a way of switching off a smart switch when my iPhone is fully charged, so that it doesn’t continually over charge. This looks like the starting point to a solution! I’ll have to have a play…

1 Like

Continuing…

Add this to the end of the rule given above:

[… redacted …] can be found in the first posting.

@patrik_gfeller once again, feel free to transfer :wink:

1 Like

@patrik_gfeller Thx for the hint - will take some time until I try that.

@ThomDietrich Thx for the code --> in your distance to work location your variable should be named work instead of home

Regards,
Herbert

I followed your and @patrik_gfeller helpful instructions, but I get the following error, when Openhab tries to format the location in BasicUI:

2017-08-01 10:50:56.529 [WARN ] [ui.internal.items.ItemUIRegistryImpl] - Exception while formatting value '49.xxxxxxxxx,8.xxxxxxxxxx' of item iPhone_Coordinates with format '%.0f': java.util.IllegalFormatConversionException: f != org.eclipse.smarthome.core.library.types.DecimalType

Any hints for that issue?

Thanks, you are right. That just happened because I replaced my actual places (which are not called home and work).

This: 49.xxxxxxxxx,8.xxxxxxxxxx is not a simple float. You could try %s instead.
However these coordinates are pretty worthless unless you are a walking map.

On your Basic UI all you need to add are those two elements:

  • Text item=iPhone_Battery icon="iphone"
  • Text item=iPhone_Location icon="iphone"

… which are the result of the above rules and which actually are meaningful and comprehendible to the user.

Thank you for your explanations.
%s did’t work either.

Because I’m obvious not a walking map, I finally added

Mapview item=iPhone_Coordinates height=5

to my Sitemap, which work’s well in Basic UI (in Classic UI the zoom level of the map isn’t correct)

Ah right, of course :smiley: sorry I didn’t think of it :slight_smile:

hi there… very intresting solution! thanks for it!
in the log message there are all data but an error also:

"16:40:01.921 [ERROR] [ntime.internal.engine.ExecuteRuleJob] - Error during the execution of rule iCloud Data Retrieval: An error occurred during the script execution: The name 'iPhoneLocation' cannot be resolved to an item or type."

Any ideas?

Best regards

Thanks for the hint. There was a small spelling mistake in the first posting. You can check to see that there was indeed no item with that name in your items file. I’ve done the needed changes, didn’t test them though. Please let us know if all works fine now.

An easy way to show a OpenStreetMap in Habpanel:

  1. Create an extra Sitemap
sitemap map label="Location" {
		Mapview item=iPhone_Coordinates height=13 //height depends from your needs
}
  1. Create a Frame Widget in HabPanel
    URL=https://Your.IP:8443/basicui/app?sitemap=map
    Tick the “Fill to edges” and “Hide title label” checkbox

Edit:

  1. You can hide the default navigation from the Basic UI as described here to see just the Map in the Frame Widget.
3 Likes

:slight_smile:

I wondered about the error in the designer but was not clever enough to solve the problem itself…

And yes, now it works very good…

1 Like

Hi guys,

another problem now: Finding a solution for a rule using this data!
I want to change a switch for presence detection with checking the location has changed

rule "iPhone Anwesenheit"
when
  
  Item iPhone_Coordinates changed
then	
	if (iPhone_Location.state == home )
		{
		sendCommand(DummyItem,ON)	
		}
end

But this doesn´t work…
Also, it does not help to put the word home in quotation marks. The switch is also switched off without that the iphone changes the location.

I forgot to say: there is a second rule which switches the DummyItem to OFF when the iPhone_Location.state != home
Best regards