How can I access “global” settings like the currency I’ve set to be used or the country I’m in?
Or the lon/lat location ?
Is there a list of fixed variable names for these ?
FWIW I’m using rules DSL, but I guess it’s the same with other languages.
How can I access “global” settings like the currency I’ve set to be used or the country I’m in?
Or the lon/lat location ?
Is there a list of fixed variable names for these ?
FWIW I’m using rules DSL, but I guess it’s the same with other languages.
I don’t know how to do this in DSL, but this JRuby script may give you some idea
(edited to make it easier for people unfamiliar with Ruby to follow the code)
location_provider = OpenHAB::OSGi.service("org.openhab.core.i18n.LocationProvider")
logger.info("location: #{location_provider.getLocation}")
time_zone_provider = OpenHAB::OSGi.service("org.openhab.core.i18n.TimeZoneProvider")
logger.info("time_zone: #{time_zone_provider.getTimeZone}")
locale_provider = OpenHAB::OSGi.service("org.openhab.core.i18n.LocaleProvider")
logger.info("locale: #{locale_provider.getLocale}")
unit_provider = OpenHAB::OSGi.service("org.openhab.core.i18n.UnitProvider")
currency = unit_provider.getUnit(org.openhab.core.library.dimension.Currency.java_class)
logger.info "currency: #{currency}"
Output:
location: xxxxx,xxxxx
time_zone: Australia/Brisbane
locale: en_AU
currency: AUD
Similar approach can be done for any openhab public interface. You first need to get the OSGi service then call the relevant public methods to get to the data you want.
I’ve just thought of an idea. If you need to use these in DSL and don’t know an easy way to get to the OSGi services, you could install jruby addon, and run this script on startup (either using a UI rule or a file based rule - it’s the same exact code)
PROVIDERS = {
location: "org.openhab.core.i18n.LocationProvider",
time_zone: "org.openhab.core.i18n.TimeZoneProvider",
locale: "org.openhab.core.i18n.LocaleProvider"
}
PROVIDERS.each do |key, provider|
setting = OpenHAB::OSGi.service(provider).send(key)
shared_cache["i18n:#{key}"] = setting
end
unit_provider = OpenHAB::OSGi.service("org.openhab.core.i18n.UnitProvider")
shared_cache["i18n:unit_provider"] = unit_provider
shared_cache["i18n:unit:currency"] = unit_provider.get_unit(org.openhab.core.library.dimension.Currency.java_class)
Then you can access them from DSL using sharedCache.get("xxx") where xxx is the same key stored by JRuby (e.g. i18n:time_zone, i18n:locale, etc)
The only caveat is when those things changed at runtime, you’d have to re-run the ruby script to fill the sharedCache with the current values, or restart openhab of course.
This would make an excellent candidate for a rule template. Then users could install it and be ready to go.
This is how I did it in JavaScript to get the location:
const { osgi } = require('openhab');
const LocationService = Java.type('org.openhab.core.i18n.LocationProvider');
const locationService = osgi.getService(LocationService);
const ohLocation = locationService.getLocation().toString();
const [lat, lon, alt] = ohLocation.split(',');
For the locale:
const { osgi } = require('openhab');
const LocaleProvider = Java.type('org.openhab.core.i18n.LocaleProvider');
const localeProvider = osgi.getService(LocaleProvider);
const locale = localeProvider.getLocale().toString();
And for the currency:
const { osgi } = require('openhab');
const CurrencyProvider = Java.type('org.openhab.core.library.unit.CurrencyProvider');
const currencyProvider = osgi.getService(CurrencyProvider);
const currency = currencyProvider.getBaseCurrency().toString();
Be aware of the .toString(), otherwise you end up with an object.