Thanks again for this library. It has enabled me to greatly simplify my presence change action rules.
I did encounter one unexpected result, that I was able to work around. In addition to getting the distance from home that I use to set a presence switch, I want to get the distance between my watch and my phone. I usually don’t take my phone on a walk or bike ride, but the phone seems to respond more quickly to the network binding presence detection and I am using the GPS Tracker binding with OwnTracks (which I find is more real time for arrival/departure than the every 5 minute iCloud blinding location updates). But that means I need to know whether to consider the phone’s input, and the requires knowing if the phone is with the watch. (Sorry for the long explanation of the use case).
When I first tried using the Distance between block using the newState for the Phone’s location and the watch location item state for the watch, the block would not let me attach the “get state of item” block with the watch location item. I then tried using the watch location item instead of its state. The block let me attach that, but the result was
2022-09-03 08:52:46.544 [WARN ] [rnal.defaultscope.ScriptBusEventImpl] - State 'NaN' cannot be parsed for item 'GeorgeWatchDistanceFromiPhoneiCloud'.
I then tried creating a variable to which I assigned the watch location item. The block accepted that variable, and the code now works as expected.
Here is the current code that works:
configuration: {}
triggers:
- id: "1"
configuration:
itemName: GeorgesiPhoneiPhone13Pro_Location
type: core.ItemStateChangeTrigger
conditions: []
actions:
- inputs: {}
id: "2"
configuration:
blockSource: '<xml
xmlns="https://developers.google.com/blockly/xml"><variables><variable
id="vwjL40[(bStpY%37YS`A">GWatchLocation</variable></variables><block
type="variables_set" id="hvISg+!AhH).zM}?k.V8" x="62" y="184"><field
name="VAR" id="vwjL40[(bStpY%37YS`A">GWatchLocation</field><value
name="VALUE"><block type="oh_getitem_state"
id="]!^!3`C5_1Qn8Cvs}w_y"><value name="itemName"><shadow type="oh_item"
id="``Arfe,D7;]qDG}I%dDd"><field
name="itemName">GeorgesAppleWatchAppleWatchSeries7GPSCellular_Location</field></shadow></value></block></value><next><block
type="oh_log" id="fn:IL[;|E4MaQBE$+hQK"><field
name="severity">info</field><value name="message"><shadow type="text"
id="~8P-BoM3~qG$Vw*y(op-"><field name="TEXT">abc</field></shadow><block
type="text_join" id="];D!jCb!h-u*$itqP,L`"><mutation
items="7"></mutation><value name="ADD0"><block type="oh_context_info"
id="V,a4+SG{J]!NQzp5kH[0"><field
name="contextInfo">itemName</field></block></value><value
name="ADD1"><block type="oh_context_info"
id="Bcm,t!^|mP.{+t2uYPiD"><field
name="contextInfo">itemState</field></block></value><value
name="ADD2"><block type="oh_text_crlf"
id="$QYVV;z1[^^**K4nTi/|"></block></value><value name="ADD3"><block
type="oh_getitem_attribute" id="!Pc/ML[!xE}Idzf{4J32"><mutation
attributeName="Name"></mutation><field
name="attributeName">Name</field><value name="item"><shadow
type="oh_getitem" id="ve~F[Fvu%o5BdFfHtY.I"><value
name="itemName"><shadow type="oh_item" id="PaxQj1.hWWsxmqXO90E,"><field
name="itemName">GeorgesiPhoneiPhone13Pro_Location</field></shadow></value></shadow></value></block></value><value
name="ADD4"><block type="oh_getitem_state"
id="7RGzFa:w0YFALVZgu4NJ"><value name="itemName"><shadow type="oh_item"
id="vf}KCdeqp}MYgI?F)w/R"><field
name="itemName">GeorgesiPhoneiPhone13Pro_Location</field></shadow></value></block></value><value
name="ADD5"><block type="text" id="SA|DePp7#nWo6}(#c],u"><field
name="TEXT"> Variable: </field></block></value><value name="ADD6"><block
type="variables_get" id="-?n=|H1s#Z,[}!2@p]?S"><field name="VAR"
id="vwjL40[(bStpY%37YS`A">GWatchLocation</field></block></value></block></value><next><block
type="controls_if" id="39L-skYht3H:%kR6oU*i"><mutation
else="1"></mutation><value name="IF0"><block type="logic_compare"
id="50{sPiD|00wao]|CC+.~"><field name="OP">NEQ</field><value
name="A"><block type="oh_context_info" id="?+]tLGF-QBwAgyOiJpj0"><field
name="contextInfo">itemState</field></block></value><value
name="B"><block type="text" id="cb34kc@sV^;1Gxs.R5Gu"><field
name="TEXT">UNDEF</field></block></value></block></value><statement
name="DO0"><block type="oh_event" id="C`eFJ7AKN$E9}GyQyeiI"><field
name="eventType">postUpdate</field><value name="value"><shadow
type="text" id=",HT]dv%XT]h$*YXkC[5`"><field
name="TEXT">value</field></shadow><block
type="lochmueller:blockly:gis_gis_distance"
id="DRx-XEJ+r=^|SEQCWeI4"><value name="POSITION1"><block
type="oh_context_info" id="=VT`dwzRqN4,{S@SFehH"><field
name="contextInfo">itemState</field></block></value><value
name="POSITION2"><block type="lochmueller:blockly:gis_gis_position"
id="o?3k_qUZ#nKFC},Qf7}H"><field name="LATITUDE">47.535516</field><field
name="LONGITUDE">-113.470593</field></block></value></block></value><value
name="itemName"><shadow type="oh_item" id="d%/W,7N*g4YC_hqA5zm{"><field
name="itemName">GeorgeiPhoneDistanceFrom80Burgundy</field></shadow></value><next><block
type="oh_event" id="VjelTQ7Zo2Vpsv+a|[Qy"><field
name="eventType">postUpdate</field><value name="value"><shadow
type="text"><field name="TEXT">value</field></shadow><block
type="lochmueller:blockly:gis_gis_distance"
id=":Qw0O=tFn=x^bq_]G;@*"><value name="POSITION1"><block
type="oh_context_info" id="Hwnm.#1n6wfgxj8YTh4w"><field
name="contextInfo">itemState</field></block></value><value
name="POSITION2"><block type="variables_get"
id="*773i7=/Z@+D#~u7A,y;"><field name="VAR"
id="vwjL40[(bStpY%37YS`A">GWatchLocation</field></block></value></block></value><value
name="itemName"><shadow type="oh_item" id="9WeAN35:.@p/Kqv`V+t["><field
name="itemName">GeorgeWatchDistanceFromiPhoneiCloud</field></shadow></value></block></next></block></statement><statement
name="ELSE"><block type="oh_event" id="Qz50VBOqKgKgcF+h%%$O"><field
name="eventType">postUpdate</field><value name="value"><shadow
type="text" id="JV~XY:ElTAwunuUFQSl%"><field
name="TEXT">UNDEF</field></shadow></value><value name="itemName"><shadow
type="oh_item" id="_Tp)Ek-:+/V4KZXIxL}}"><field
name="itemName">GeorgeiPhoneDistanceFrom80Burgundy</field></shadow></value><next><block
type="oh_event" id=".m_`*;pxp2($ksMIfVQz"><field
name="eventType">postUpdate</field><value name="value"><shadow
type="text" id="wNb.^@c0;]5xD,O;FpB{"><field
name="TEXT">UNDEF</field></shadow></value><value name="itemName"><shadow
type="oh_item" id="Z*D;*4y_R$nnr?!V,+Ym"><field
name="itemName">GeorgeWatchDistanceFromiPhoneiCloud</field></shadow></value></block></next></block></statement></block></next></block></next></block><block
type="oh_item" id="KLAZACMlz19T0tulO=gT" disabled="true" x="80"
y="720"><field
name="itemName">GeorgesAppleWatchAppleWatchSeries7GPSCellular_Location</field></block></xml>'
type: application/javascript
script: >
var GWatchLocation;
var logger = Java.type('org.slf4j.LoggerFactory').getLogger('org.openhab.rule.' + ctx.ruleUID);
function geo_distance(position1, position2) {
var lat1 = position1.latitude;
var lat2 = position2.latitude;
var lon1 = position1.longitude;
var lon2 = position2.longitude;
var R = 6371e3; // metres
var o1 = lat1 * Math.PI/180;
var o2 = lat2 * Math.PI/180;
var Ao = (lat2-lat1) * Math.PI/180;
var Ab = (lon2-lon1) * Math.PI/180;
var a = Math.sin(Ao/2) * Math.sin(Ao/2) +
Math.cos(o1) * Math.cos(o2) *
Math.sin(Ab/2) * Math.sin(Ab/2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
return R * c;
}
function geo_position(latitude, longitude) {
return {'latitude': latitude, 'longitude': longitude};
}
GWatchLocation = itemRegistry.getItem('GeorgesAppleWatchAppleWatchSeries7GPSCellular_Location').getState();
logger.info(([event.itemName,event.itemState,'\r\n',itemRegistry.getItem('GeorgesiPhoneiPhone13Pro_Location').getName(),itemRegistry.getItem('GeorgesiPhoneiPhone13Pro_Location').getState(),' Variable: ',GWatchLocation].join('')));
if (event.itemState != 'UNDEF') {
events.postUpdate('GeorgeiPhoneDistanceFrom80Burgundy', (geo_distance(event.itemState, geo_position(47.535516, -113.470593))));
events.postUpdate('GeorgeWatchDistanceFromiPhoneiCloud', (geo_distance(event.itemState, GWatchLocation)));
} else {
events.postUpdate('GeorgeiPhoneDistanceFrom80Burgundy', 'UNDEF');
events.postUpdate('GeorgeWatchDistanceFromiPhoneiCloud', 'UNDEF');
}
type: script.ScriptAction
It’s working so I’m happy, but I thought I would share my experience because it seems like the block should accept a location type item.