If you do not want to supply the items as a rule argument I suggest you create a list of contact items and store it. Then iterate over the list without fetching the item again.
If you want to post an example I can try help you optimize your rule.
Good thing I found this post! Just upgraded to OH3 and since 99% of my rules are HABApp since a few weeks, the system became useless. With OH2 I had small latencies around 100-200ms for some cascaded sequences that now take 10-15 seconds!
I disabled the AUTH for now and wouldn’t mind if that was a permanent option in openHAB, but would it be possible to add an option for using the API key in HABApp?
Doing some simple tests with cURL shows that using an API key is way faster:
I’m sorry for the inconvenience and really disappointed that there was no fix included in the openhab release especially since I addressed the issue with M5 and a simple off switch in the config or a small cache would have been sufficient.
I’ll investigate about the token and it is on my roadmap but it’s a little bit tricky since I am using multiple libraries because of the SSE event listener.
Thank you for that, but if it is a lot of work, I can live with the workaround. Once auth is off, I have no problem with timing anymore.
In my simple setup, I don’t really see the need for any authentication anyway.
The file name is the same that is used in the RequestFileLoadEvent from the EventBus.
In your case that would be params/plantcare.yml.
Also you can use multiple spaces in the beginning:
Why did you do that? I never stated that you should change your configuration!
It is also not a relative path to the rule folder, I don’t know how you got that idea.
It’s params/ + name relative to the parameters folder.
When in doubt just touch your parameter file, watch for the RequestFileLoadEvent on the HABApp event bus and use the name from the event for the file property as written in the docs.
Correct. If the logic is always the same you can create your triggers inside the rule since the event contains the name of the item that changed.
it’s either one rule for each item
class PlantStatusUpdater(HABApp.Rule):
def __init__(self, plant_prefix):
super().__init__()
log.info(f'Initialising plant status updater on prefix: {plant_prefix}')
# Collect all required items
self.control = SwitchItem.get_item(f'{plant_prefix}_Control')
self.time = DatetimeItem.get_item(f'{plant_prefix}_LastUpdate')
self.time.listen_event(self.update_status, ValueChangeEvent)
or one rule for all items
class PlantStatusUpdater(HABApp.Rule):
def __init__(self):
super().__init__()
for plant_prefix in HABApp.Parameter('plantcare', 'plants').value:
item = DatetimeItem.get_item(f'{plant_prefix}_LastUpdate')
item.listen_event(self.update_status, ValueChangeEvent)
def update_status(self, event: ValueChangeEvent):
assert isinstance(event, ValueChangeEvent), type(event)
log.info(f'Rule triggerd with value: {event}')
# Strip away "_LastUpdate" to build the prefix
# plant_prefix = event.name[:-11]
plant_prefix = event.name.replace('_LastUpdate', '')
switch = SwitchItem.get_item(f'{plant_prefix}_Control')
if switch.is_on():
log.info(f'Sensordata should be processed!')
else:
log.info(f'Mööp - No processing!')
PlantStatusUpdater()
Note that I use the class factories every time because it helps catch typos.
I’d also prefer the first version because all items get resolved when the rule is created.
That way everything is less error prone.
It is not easy to dig into all the deatils of your development process and so i try to rebuild things in examples and then change them step by step. Beginner problems.
So next step look at the RequestFileLoadEvent … Found it … Did i get it now right, that params is the placeholder for the param path in the configuration yaml?
And then i have to decide which approach to go “many Rules” or only “one Rule”
No, it’s literally just params/. You can name your parameter folder how you want.
The prefix name is not related to the configured folder - it’s always params/ for parameters.
Yea - the docs could definitely be improved. I’m already working on it for the 0.19.0 release.
With great power comes great responsibility.
Imho it highly depends on the logic you want to implement.
However rules are inexpensive so why not create many of them.
If you want to transform the value in multiple rule files you can use a Parameter and create key/value pairs in the file.
param = DictParameter('my_file')
param[key]
If you want to transform in just one rule file you can create a global dict and simply use it and look up.
If you want to do more complex stuff you can use an HABApp Item as proxy
Using the transforms from openhab is (currently) not possible and to be honest I never saw the need for them.
Key “plants” is used to iterate through all plant items (Have choosen the approach with only one rule. ;-)) From key States i generate a dictionary to do the mapping with
# Get plant state mapping
self.plant_states = dict(HABApp.DictParameter('plantcare', 'states').value)
log.info(f'State changed from "{self.plant_states.get(last_state)}" to "{self.plant_states.get(target_state)}"')
Next step is to add locations (a new parameter file) and plant names
Have you guys moved to OH3 yet? I am trying to move more and more of my rules over to HABapp before the upgrade. This is another advantage of HABApp: reduce coupling. The chance of HABApps and OH upgrade at the same time is very low. And as we mainly use OH3 for its core, binding and definition of items, our upgrades should be a lot less painful than when we’re fully within OH. At the same time, we have another process/system to maintain.
When I have sometimes, I will write up a section on “Why HABapps and how is it difference from other approaches?”.
I think we should also have a section that reference source code / projects that based on HABApps. It will provide examples for people new to the project.
for name, params in HABApp.DictParameter('plantcare', 'plants').items():
PlantStatusUpdater(name, config['param1'])
Also if you cache the parameter value you might as well put a global dict in your rule files.
It’s either
self.plant_states = HABApp.DictParameter('plantcare', 'states') # note you don't have to cast to dict
log.info(f'State changed from "{self.plant_states.get(last_state)}" to "{self.plant_states.get(target_state)}"')
or
PLANT_STATES = {-1: 'deaktiviert', 0: 'OK', ... }
...
log.info(f'State changed from "{PLANT_STATES.get(last_state)}" to "{PLANT_STATES.get(target_state)}"')
I am still on 2.5 since I don’t have a lot of time right now and I’d like to fix some issues before I migrate.
That would be nice. You can create a new thread so we can get a discussion going.
I was scratching my head yesterday trying to work around this strange issue. On the HABApp side, I attempted to set a String item to an empty string value like this:
Also, I’d use oh_post_update since oh_send_command might not work as intended depending on your autoupdate settings.
You then can check the events.log and HABApp_events.log and observe the state change.
Posting an empty string as an update definitely works, I do this in one of my rules, too.