HABApp - Easy automation with openHAB

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. :wink:
Imho it highly depends on the logic you want to implement.
However rules are inexpensive so why not create many of them.

Yes that right.

Did i get it right that the use of transformations is not possible like the use of actions?

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.

I understand. The only reason to use OG transformation was to use the same files.

Now i build it on a parameter file. This is a nice benefit from the parameter files. I could put all types of information in it.

plants:
  - ZO_PC_Miflora01
  - ZO_PC_Miflora02
  - ZO_PC_Miflora03
  - ZO_PC_Miflora04
  - ZO_PC_Miflora05
  - ZO_PC_Miflora06
  - ZO_PC_Miflora07
  - ZO_PC_Miflora08
  - ZO_PC_Miflora09
  - ZO_PC_Miflora10
  - ZO_PC_Miflora11
  - ZO_PC_Miflora12
  - ZO_PC_Miflora13
  - ZO_PC_Miflora14
  - ZO_PC_Miflora15

states:
  -1 : 'deaktiviert'
  0  : 'ok'
  2  : 'gießen'
  4  : 'düngen'
  6  : 'gießen & düngen'

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

1 Like

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.

1 Like

@Dibbler42
You can use Dicts in the parameter files:

ZO_PC_Miflora01:
  location: loc_1
  param1: value2
ZO_PC_Miflora02:
  location: loc_2
  param1: value4

and use this to set up your rule

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.

1 Like

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:

HABApp.openhab.interface.send_command('EmailBody', '')

Naturally I expect that on the JSR223 side, its value via the following statement would be a string value as well.

body = scope.items['EmailBody'].toString()

but that is not the case. Its value is in fact a scope.UnDefType.NULL.

I wonder if this is a bug on the HABapp side or on the JSR223 side.

I recommend you use the class factory to interact with openhab:

StringItem.get_item('EmailBody').oh_send_command('')

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.

Is it possible to send NULL or UNDEF value to openhab items?

Sure, just send None

That was just what I tried.

self.ohitem_metno_forecastnow_temp = NumberItem.get_item("SmetNo_Ute_est_temp")
self.ohitem_metno_forecastnow_temp.oh_send_command(None)

This is from habapp log:

[2021-01-04 19:05:54,704] [HABApp.openhab.connection]  WARNING | Status 400 for POST http://bitfrost:8080/rest/items/SmetNo_Ute_est_temp/ NULL

And there is no entry in openhab/events.log

Just snipped this from the rule I currently writing, sorry for the long names :slight_smile:

You have to use oh_post_update!
send_command: Send something to an external device
post_update: Update an item in openhab

Thanks, I will try oh_post_update.
In this case there is no physical devices involved. Or, ie. Habapp is the the interface to the hardware.
It is my intention that the openhab item(s) and changes to them to be picked up by my older rules.

Yes, but this is not specific to HABApp but openhab logic.
SendCommand sends something to a physical device. postUpdate will change the state of the item.
There is this autoupdate mechanism that changes items if you issue a sendcommand, that’s where the confusion comes from.

Thanks, Reading into the docs and explanations I think I get it:
sendcommand can only be a command and not a state. NULL is a state. For example: ON or OFF is a command for a Switch item.

1 Like

One more question:
Is the contents of config.yml stored somewhere in HabApp and accessible from rules?

More specific; it would be great to get the location data from one defined place.

Also, some of my rules need to store some data (caches and reports). So a directory for these things could be defined under “directories:”

I believe that these belongs in the main config rather than in separate parameter files.
It would ease moving/copy rules between systems that are not identical.

I think it is available as HABApp.CONFIG. Just check with your IDE, it will autocomplete.

Currently there is no proper way to modify this config from a rule. You can read it but you should not attempt to write it.

I just use one parameter file (the built in from HABApp) for the setup of the system, e.g. different API tokens, etc. You can structure it nicely and HABApp reads it for you.

It is there :smiley:
I had to play around with it a bit to get the value (float) and not a ruamel object:

latitude = HABApp.CONFIG.location.latitude.real

Without PyCharm I would need very many hours to read thru and understand all the libs.

Read only is fine, this is system administrator stuff and not something a more or less competent HABApp rule writer (me) should dabble with. :slight_smile:

With my system administrator hat on;
I still want to propose a entry for a general data directory in config.yml
One could name it “data”,“misc” or something else.
It would simplify setting up HABApp and move rules between very different machines.

@Spaceman_Spiff
Thank you for answering my questions!
And a much bigger thank you for the work on HABApp!
For me HABApp is the thing that really lifts my home automation up.

1 Like