spacemanspiff2007 / HABApp

Easy home automation with MQTT and/or openHAB
Apache License 2.0
54 stars 23 forks source link

Wrong timestamp format #319

Closed chakjer closed 1 year ago

chakjer commented 1 year ago

I have some issue with timestamp for NumberItem after upgrade version to 1.0.4 Do i miss something in configuration?

[2022-09-02 00:18:00,349] [HABApp.openhab.connection]    ERROR | Error  in output_queue_listener:
[2022-09-02 00:18:00,350] [HABApp.openhab.connection]    ERROR | File "/opt/habapp/lib/python3.9/site-packages/HABApp/openhab/connection_handler/http_connection.py", line 281 in output_queue_listener
[2022-09-02 00:18:00,350] [HABApp.openhab.connection]    ERROR | --------------------------------------------------------------------------------
[2022-09-02 00:18:00,350] [HABApp.openhab.connection]    ERROR |      267 | async def output_queue_listener():
[2022-09-02 00:18:00,350] [HABApp.openhab.connection]    ERROR |      268 |     # clear Queue
[2022-09-02 00:18:00,350] [HABApp.openhab.connection]    ERROR |        (...)
[2022-09-02 00:18:00,350] [HABApp.openhab.connection]    ERROR |      277 |             while True:
[2022-09-02 00:18:00,350] [HABApp.openhab.connection]    ERROR |      278 |                 item, state, is_cmd = await QUEUE.get()
[2022-09-02 00:18:00,350] [HABApp.openhab.connection]    ERROR |      280 |                 if not isinstance(state, str):
[2022-09-02 00:18:00,350] [HABApp.openhab.connection]    ERROR | -->  281 |                     state = convert_to_oh_type(state)
[2022-09-02 00:18:00,350] [HABApp.openhab.connection]    ERROR |      283 |                 if is_cmd:
[2022-09-02 00:18:00,351] [HABApp.openhab.connection]    ERROR |    ------------------------------------------------------------
[2022-09-02 00:18:00,351] [HABApp.openhab.connection]    ERROR |      e = ValueError()
[2022-09-02 00:18:00,351] [HABApp.openhab.connection]    ERROR |      is_cmd = False
[2022-09-02 00:18:00,351] [HABApp.openhab.connection]    ERROR |      item = 'var2'
[2022-09-02 00:18:00,351] [HABApp.openhab.connection]    ERROR |      item, state, is_cmd = ('var2', <NumberItem name: var1, value: 7313937, last_change: 2022-09-02T00:18:00.328498, last_update: 2022-09-02T00:18:00.328498>, False)
[2022-09-02 00:18:00,351] [HABApp.openhab.connection]    ERROR |      log = <Logger HABApp.openhab.connection (INFO)>
[2022-09-02 00:18:00,351] [HABApp.openhab.connection]    ERROR |      QUEUE = <Queue at 0x7fc0cd553ac0 maxsize=0 tasks=88862>
[2022-09-02 00:18:00,351] [HABApp.openhab.connection]    ERROR |      state = <NumberItem name: var1, value: 7313937, last_change: 2022-09-02T00:18:00.328498, last_update: 2022-09-02T00:18:00.328498>
[2022-09-02 00:18:00,351] [HABApp.openhab.connection]    ERROR |    ------------------------------------------------------------
[2022-09-02 00:18:00,351] [HABApp.openhab.connection]    ERROR | 
[2022-09-02 00:18:00,351] [HABApp.openhab.connection]    ERROR | File "/opt/habapp/lib/python3.9/site-packages/HABApp/openhab/connection_handler/func_async.py", line 29 in convert_to_oh_type
[2022-09-02 00:18:00,351] [HABApp.openhab.connection]    ERROR | --------------------------------------------------------------------------------
[2022-09-02 00:18:00,352] [HABApp.openhab.connection]    ERROR |      27 | def convert_to_oh_type(_in: Any) -> str:
[2022-09-02 00:18:00,352] [HABApp.openhab.connection]    ERROR |      28 |     if isinstance(_in, BaseValueItem):
[2022-09-02 00:18:00,352] [HABApp.openhab.connection]    ERROR | -->  29 |         raise ValueError()
[2022-09-02 00:18:00,352] [HABApp.openhab.connection]    ERROR |      31 |     if isinstance(_in, datetime.datetime):
[2022-09-02 00:18:00,352] [HABApp.openhab.connection]    ERROR |      32 |         # Add timezone (if not yet defined) to string, then remote anything below ms.
[2022-09-02 00:18:00,352] [HABApp.openhab.connection]    ERROR |      33 |         # 2018-11-19T09:47:38.284000+0100 -> 2018-11-19T09:47:38.284+0100
[2022-09-02 00:18:00,352] [HABApp.openhab.connection]    ERROR |    ------------------------------------------------------------
[2022-09-02 00:18:00,352] [HABApp.openhab.connection]    ERROR |      (set, list, tuple, frozenset) = (<class 'set'>, <class 'list'>, <class 'tuple'>, <class 'frozenset'>)
[2022-09-02 00:18:00,352] [HABApp.openhab.connection]    ERROR |      _in = <NumberItem name: var1, value: 7313937, last_change: 2022-09-02T00:18:00.328498, last_update: 2022-09-02T00:18:00.328498>
[2022-09-02 00:18:00,352] [HABApp.openhab.connection]    ERROR |      Any = typing.Any
[2022-09-02 00:18:00,352] [HABApp.openhab.connection]    ERROR |    ------------------------------------------------------------
[2022-09-02 00:18:00,352] [HABApp.openhab.connection]    ERROR | 
[2022-09-02 00:18:00,352] [HABApp.openhab.connection]    ERROR | --------------------------------------------------------------------------------
[2022-09-02 00:18:00,353] [HABApp.openhab.connection]    ERROR | Traceback (most recent call last):
[2022-09-02 00:18:00,353] [HABApp.openhab.connection]    ERROR |   File "/opt/habapp/lib/python3.9/site-packages/HABApp/openhab/connection_handler/http_connection.py", line 281, in output_queue_listener
[2022-09-02 00:18:00,353] [HABApp.openhab.connection]    ERROR |     state = convert_to_oh_type(state)
[2022-09-02 00:18:00,353] [HABApp.openhab.connection]    ERROR |   File "/opt/habapp/lib/python3.9/site-packages/HABApp/openhab/connection_handler/func_async.py", line 29, in convert_to_oh_type
[2022-09-02 00:18:00,353] [HABApp.openhab.connection]    ERROR |     raise ValueError()
[2022-09-02 00:18:00,353] [HABApp.openhab.connection]    ERROR | ValueError
spacemanspiff2007 commented 1 year ago

I am unsure if it's an issue with HABApp or an issue with one of your rules. Could you provide the rule snippets where you interact with var1 and var2?

chakjer commented 1 year ago

I found my error, i pointed to variable self.var not self.var.value. It is really strange because in version <1.0.x it worked well.

spacemanspiff2007 commented 1 year ago

Yes that was introduced with this update. I was unaware this was a breaking change. Could you describe what you are doing (e.g. code snippet). If it makes sense I'll add it again ...

chakjer commented 1 year ago

From my point it was a good change, now code is more clearly. It is a function to store power consumption for shelly devices ''' class powerRules(HABApp.Rule):

def __init__(self):
    super().__init__()
    self.varTotal = NumberItem.get_item('powerTotal')
    self.var= NumberItem.get_item('powerCurr')
    self.var1 = NumberItem.get_item('powerPrev')

    self.var.listen_event(self.power_update,ItemStateChangedEventFilter())

def power_update(self, event: ItemStateChangedEvent):
    if not isinstance(self.var1,NumberItem) or (self.var1.value == None):
        self.var1.value = self.var.value
    if not isinstance(self.varSum,NumberItem) or (self.varSum.value == None):
        self.varTotal.value = 0

    if self.var.value - self.var1.value > 0 :
        self.varTotal.value += self.var.value-self.var1.value
    self.oh.post_update('powerTotal',self.varTotal.value)
    self.oh.post_update('powerPrev',self.var.value)

'''

spacemanspiff2007 commented 1 year ago

Just a hint: The isinstance is not necessary because get_item will throw an error if the type is not correct. If you want to handle None values you should use get_value:

pwr_curr = self.var.get_value(0)

I however prefer to just start a task that initializes the values on rule startup:

def __init__(self)
    ....
    self.run.soon(self.initialize)

def initialize(self):
    if self.var1.value is None:
        self.var1.oh_post_update(0)