openhab-core icon indicating copy to clipboard operation
openhab-core copied to clipboard

Use “Location” from Configuration - System - Regional Setting as Global Attribute in Rules

Open funkstille opened this issue 4 years ago • 6 comments

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

funkstille avatar Feb 20 '21 08:02 funkstille

just came across the same question @ghys sorry to ping you but maybe you know a way this can already be done ?

mstormi avatar Apr 07 '21 14:04 mstormi

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.

ghys avatar Apr 07 '21 14:04 ghys

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).

ghys avatar Apr 07 '21 15:04 ghys

Can the issue be closed?

lolodomo avatar May 04 '21 14:05 lolodomo

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.

mstormi avatar May 04 '21 14:05 mstormi

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

weymann avatar Jun 09 '22 19:06 weymann