Is updating InfluxDB values before saving possible?

I have OpenHab 3 running in Docker with InfluxDB as persistence layer.
Also mosquitto as MQTT message broker and Grafana creates charts out of it.
I am measuring energy consumption using smart plugs.

If my understanding is correct the data in InfluxDB is persisted according to my persistence strategy. What I want to do is to manipulate a value before saving it.

So if I receive 15 watts from an Item through the when block in a rule

when Item BW5TV_Waktuell received update

I want to manipulate the watt value before storing it. Is that possible?

Trying sendCommand and postUpdate on my Item in the then block leads to an endless loop because I am myself causing the rule to be executed immediately again.

I only want to trigger the rule when the smart plug is sending a value to manipulate it before saving.

Why do you want to manipulate an item before persisting it? If you need it manipulated within openHAB anyways, there’s other means of doing that, have a look at “profiles”, e.g. “offset”, which you can use to adjust an item’s value BEFORE it even reaches openHAB.

If you want to persist an item for whatever reason different from the item’s state in openHAB - you can try to write a rule, which persists the item and add your manipulation there. But IMHO it’s not possible to add a “profile” or whatever to a persistence. It simply makes no sense, because what openHAB does with persistence is simply a) restore the value and b) use persistence for historic comparisons or similar.

1 Like

Sure, stick your magic value in another Item and persist that.

openHAB persistence services do not provide a general database interface, thay are really only persisting Item states.

Exotic access could be provided by scripts external to OH.

I agree with @binderth though, if you tell us what you are actually trying to achieve (instead of how) you might get more useful suggestions.

1 Like

The idea behind it was to separate devices in the Grafana graphs. I have 1 smart plug in my TV area. 2 devices are connected to it to turn them off at the same time. Energy consumption wise the watt measured are from the 2 devices of course.
Knowing that the consumption from 1 device is pretty much linear while the other ones is completely depending on the current usage and fluctuates hard I wanted to have them separated in Grafana like:

watt device 1 = hard coded flat 5 watt
watt device 2 = plug measurement - 5 watt

I will go for that thanks. I was already able to persist custom values and will simply split the measured energy consumption into my 2 values and persist them separately.

I aggree that my use case might seem to be a bit weird but building this energy dashboard not for myself but more for the family I wanted to split those 2 devices even though they are on 1 plug. I do not want to buy more plugs and for my use case it would be sufficient as planned.

2 Likes

That’s a very normal and logical use case. It was your initial focus on the persistence database that made it seem unusual. :wink:

When people ask about non-standard approaches, we typically request more details so that we can help them with their actual issue. You realized that you just need another item and a rule, so it’s an easy conversation and a quick solution. Others…not so much. :man_shrugging:

Glad you got it figured out!

1 Like

Just to be clear for others (my bet is you figured it out already):

  • if you want to use two separate values from one source => use two items, if you can seperate them
  • even if they come from one “thing”

=> that’s the concept of openHAB: regardless the origin, treat “physical” items you want to interact with as separate items within openHAB.
The beauty of it is: only then you can use the consumption of the fluctuating device for some openHAB rules - and not only for a graph in grafana! e.g.: turn the whole socket off, if you won’t need both devices anymore.
That’s at least what I do, if there’s no more “action” on our livingroom TV, I’ll turn off the whole socket for entertainment.

another example:
If you would be able to distinguish the consumption of your fridge from your overall smart meter, you should also split that into one “fridge consumption” item. And you can then use the fridge as standalone item with all its benefits.

1 Like