HABApp icon indicating copy to clipboard operation
HABApp copied to clipboard

Enhancement: Implement function to persist a value on purpose

Open TBail opened this issue 4 years ago • 9 comments

While implementing my energy managemnt i found that i am missing a function to persist a value on purpose.

Once every hour i caclulate the energy consuption of the last hour e.g. between 18:00 an 19:00. Idealy the value is persisted at 19:00.

with the persist funtion is no need to caclulate the value at the full hour. It could be done 5 Minutes later and than the value is persisted to a defined time. This helps if there are may action at the full our.

The function should reflect the option from the openHAB API.

Would be nice if this could be implemented

TBail avatar Nov 17 '21 19:11 TBail

Just so I understand this correctly: The idea is to implement a wrapper that calls /persistence/items/{itemname}


This helps if there are may action at the full hour.

But if there is so much traffic how can you be sure that you calculate the consumption correctly at the full hour? Are you sure that a performance issue is the root case here and not another issue?

spacemanspiff2007 avatar Nov 18 '21 04:11 spacemanspiff2007

Yes you understood it right.

The consumption is calculated using the get persistence data funtion. I get all the Data within the desired time frame and then calculate the difference between the earliest and the last value. Due to the fact that i receive evry 30 seconds new values there is maybe a little difference to real consumption. With this method i could calculate the consuption value 15 minuten later, but without persisting it on a specified time the calculation should be done at to the full hour. This is my code

        now = datetime.now()
        persistence_end = datetime(year=now.year, month=now.month, day=now.day, hour=now.hour, minute=0, second=0)
        persistence_start = persistence_end + timedelta(hours=-1)
        persistence_data = self.meter_value.get_persistence_data(self.persistence_service, start_time=persistence_start, end_time=persistence_end)
        if persistence_data is not None:
            min_key = min(persistence_data.get_data().keys())
            max_key = max(persistence_data.get_data().keys())
            min_value = persistence_data.get_data().get(min_key)
            max_value = persistence_data.get_data().get(max_key)
            energy_delta = persistence_data.get_data().get(max_key) - persistence_data.get_data().get(min_key)
            self.meter_hourly.oh_post_update(energy_delta)
            log.info(f'Energy meter {self.name} consumed {energy_delta:.3f} kWh between {persistence_start} and {persistence_end}.')```

TBail avatar Nov 18 '21 07:11 TBail

I think I understand now - your plan is to request the consumption from X-1:00:00 - X-1:59:59 through the persistence service e.g. at X:05 and than set the hourly consumption through the persistence service at X:00

spacemanspiff2007 avatar Nov 18 '21 09:11 spacemanspiff2007

Yes that's right. so i should be able to process many energy meters without performance issues at a specific time and persisted data is on time.

Do you have a other/better idea?

TBail avatar Nov 18 '21 10:11 TBail

I tried playing around a little bit but I can't get the persistence to accept a new value. Have you tested the API endpoint? Does it work for you? I always get

{
  "error": {
    "message": "Persistence service not modifiable: mapdb",
    "http-code": 400
  }
}

spacemanspiff2007 avatar Dec 14 '21 13:12 spacemanspiff2007

Didn't work for me either. I get the same error with an influxdb. Additionaly i searched the openhab repositories if the is an issue - found nothing. Searched the community - Only how to get data with the API.

That leads to the assumption that no one has ever testet it. :-(

TBail avatar Dec 14 '21 15:12 TBail

Classic! 😄 Would you be so kind and create an issue in the openhab repo and link it to this issue?

spacemanspiff2007 avatar Dec 15 '21 10:12 spacemanspiff2007

Opened an issue in the openhab cor repository.

openhab/openhab-addons#12663

TBail avatar Dec 15 '21 20:12 TBail

Perfect - thank you for your help!

spacemanspiff2007 avatar Dec 16 '21 04:12 spacemanspiff2007