openhab-core
openhab-core copied to clipboard
Use “Location” from Configuration - System - Regional Setting as Global Attribute in Rules
During the setup process of OH3 you can set up the location coordinates. It would be very nice to use this exact location as global attribute in rules and functions like "distance from" and "distance changed".
As a workaround you have to create a location like "home location" which is a duplicate to the location coordinates from the setup.
discussion thread https://community.openhab.org/t/how-can-i-use-location-from-configuration-system-regional-setting-in-rules/53816
just came across the same question @ghys sorry to ping you but maybe you know a way this can already be done ?
Not sure if I'm the most competent to answer 😅 but I think it's possible to get the LocationProvider service from FrameworkUtil
.
Retrieving an OSGi service is possible (see https://github.com/openhab-scripters/openhab-helper-libraries/blob/master/Core/automation/lib/python/core/osgi/init.py) so I guess it could be done.
var FrameworkUtil = Java.type('org.osgi.framework.FrameworkUtil');
var bundle = FrameworkUtil.getBundle(ir.class);
var bundleContext = bundle.getBundleContext();
var serviceRef = bundleContext.getServiceReference('org.openhab.core.i18n.LocationProvider');
var service = bundleContext.getService(serviceRef);
var location = service.getLocation();
print(location);
This works (JS).
Can the issue be closed?
Hmm no while I appreciate Yannick's input it isn't really answered. It's still impossible (at least unclear how) to access the location that was provisioned on initial install from within DSL rules.
It's one year later but I see still the necessity in the enhancement. There's a request https://github.com/openhab/openhab-addons/issues/12783 to enhance the MyBMW binding with a distance-from-home
channel. Use case is to trigger a rule if the car reaches home location which is in fact quite useful.
In a rule you either have to define a hard coded PointType or maintain a self created Home LocationItem. Rule access towards home location from openHAB config would be more convenient to implement the rule
rule "Distance from Home"
when
System started or
Item MyBMWi394REX_Koordinaten changed
then
val home = new PointType("1.234,9.876")
if( home.distanceFrom((MyBMWi394REX_Koordinaten.state as PointType)) > 100) {
logInfo("Home Location","Vehicle on the road")
} else {
logInfo("Home Location","Vehicle at home")
}
end