hdtodd / rtl_watch

Actively monitor rtl_433 for devices in your neighborhood
MIT License
6 stars 1 forks source link

rtl_433 multiple time formats #1

Closed rct closed 1 year ago

rct commented 1 year ago

If rtl_433 is configured to use fractional seconds (IIRC -M time:usecs), rtl_watch throws an error because there is unparsed data in strptime().

Adding .%f (fractional seconds) to the strptime string %Y-%m-%d %H:%M:%S.%f resolves that but will fail if fractional seconds aren't present.

strptime() is pretty rigid for parsing. Suggest trying datetime.fromisoformat(). However, It won't handle the case if rtl_433 has been configured to output epoch seconds, but it is part of the default library, so no additional installs.

< eTime = time.mktime(time.strptime(y["time"], "%Y-%m-%d %H:%M:%S.%f"))
--
> eTime = datetime.fromisoformat(y["time"]).timestamp() `

Stack trace as an FYI

(py310) G:\git\rtl_watch>python rtl_watch.py
subscribed to mqtt feed
Connected to MQTT Broker!
Exception in thread Thread-1 (_thread_main):
Traceback (most recent call last):
  File "C:\sw\Anaconda3\envs\py310\lib\threading.py", line 1016, in _bootstrap_inner
    self.run()
  File "C:\sw\Anaconda3\envs\py310\lib\threading.py", line 953, in run
    self._target(*self._args, **self._kwargs)
  File "C:\sw\Anaconda3\envs\py310\lib\site-packages\paho\mqtt\client.py", line 3591, in _thread_main
    self.loop_forever(retry_first_connection=True)
  File "C:\sw\Anaconda3\envs\py310\lib\site-packages\paho\mqtt\client.py", line 1756, in loop_forever
    rc = self._loop(timeout)
  File "C:\sw\Anaconda3\envs\py310\lib\site-packages\paho\mqtt\client.py", line 1164, in _loop
    rc = self.loop_read()
  File "C:\sw\Anaconda3\envs\py310\lib\site-packages\paho\mqtt\client.py", line 1556, in loop_read
    rc = self._packet_read()
  File "C:\sw\Anaconda3\envs\py310\lib\site-packages\paho\mqtt\client.py", line 2439, in _packet_read
    rc = self._packet_handle()
  File "C:\sw\Anaconda3\envs\py310\lib\site-packages\paho\mqtt\client.py", line 3033, in _packet_handle
    return self._handle_publish()
  File "C:\sw\Anaconda3\envs\py310\lib\site-packages\paho\mqtt\client.py", line 3327, in _handle_publish
    self._handle_on_message(message)
  File "C:\sw\Anaconda3\envs\py310\lib\site-packages\paho\mqtt\client.py", line 3570, in _handle_on_message
    on_message(self, self._userdata, message)
  File "G:\git\rtl_watch\rtl_watch.py", line 228, in on_message
    eTime = time.mktime(time.strptime(y["time"], "%Y-%m-%d %H:%M:%S"))
  File "C:\sw\Anaconda3\envs\py310\lib\_strptime.py", line 562, in _strptime_time
    tt = _strptime(data_string, format)[0]
  File "C:\sw\Anaconda3\envs\py310\lib\_strptime.py", line 352, in _strptime
    raise ValueError("unconverted data remains: %s" %
ValueError: unconverted data remains: .542401
hdtodd commented 1 year ago

Thanks for the feedback and analysis. Obviously, my rtl_433 was set up to report just seconds in the JSON log. I'll go back and test your suggestion and update the code.

I think I've used the same approach in my other code, so I'll need to go back and fix them all and document the problem for anyone using epoch seconds.

David

rct commented 1 year ago

Instead of documenting that -M time:utc (IIRC) doesn't work try something like this (off the top of my head)

 def parse_ts(ts):
    """Parse timestamp string in ISO format timestemps or epoch seconds"""
     if ts.find('-') > 1: # rtl_433 yyyy-mm-dd hh:mm:ss{.f]
         return datetime.datetime.fromisoformat(ts).timestamp()
     else: # assume epoch seconds or epoch seconds with fraction
         return float(ts)

There are utilities like dateutil that will accept many timestamp formats, but they aren't part of the standard library or can be slow.

hdtodd commented 1 year ago

Thanks for the suggestion, Rob. I've incorporated the fix to process either UTC or Epoch time format into rtl_watch as well as DNT and rtl_433_stats.

The edits in rtl_watch may currently be only in the v2.0.0 (development) branch, which you can download, but I hope to merge that into the main branch shortly.