Skip to content

Parsing value error when unit included #348

@Gehetzter

Description

@Gehetzter

What's the issue?

Hi, I'm using JSON_HTTP with json_path but my value includes an unit:
234.56 W
Therefore it can't be parsed.
Can we add a simple regex replace that removes all letters and spaces before trying to parse the float?

Thank you

Your setup

  • Installation method: Docker
  • Power meter source: Openhab Rest Api

Configuration

[JSON_HTTP]
URL = http://192.168.178.27:8080/rest/items/LeistungStromzaehlerAktuell
JSON_PATHS = $.state

Error messages or logs

2026-05-03 03:46:04 ERROR:astrameter:Failed to test powermeter after 4 attempts: could not convert string to float: '331.74 W'
Traceback (most recent call last):
  File "/app/.venv/lib/python3.12/site-packages/astrameter/main.py", line 85, in test_powermeter
    value = await powermeter.get_powermeter_watts()
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/.venv/lib/python3.12/site-packages/astrameter/powermeter/json_http.py", line 70, in get_powermeter_watts
    values.append(extract_json_value(data, path))
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/.venv/lib/python3.12/site-packages/astrameter/powermeter/json_http.py", line 16, in extract_json_value
    return float(match[0].value)
           ^^^^^^^^^^^^^^^^^^^^^
ValueError: could not convert string to float: '331.74 W'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
  File "/app/.venv/lib/python3.12/site-packages/astrameter/main.py", line 643, in main
    asyncio.run(async_main(cfg, args, device_types, device_ids, skip_test))
  File "/usr/local/lib/python3.12/asyncio/runners.py", line 195, in run
    return runner.run(main)
           ^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/asyncio/runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/asyncio/base_events.py", line 691, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "/app/.venv/lib/python3.12/site-packages/astrameter/main.py", line 399, in async_main
    await test_powermeter(powermeter, client_filter)
  File "/app/.venv/lib/python3.12/site-packages/astrameter/main.py", line 102, in test_powermeter
    raise RuntimeError(
RuntimeError: Failed to test powermeter after 4 attempts: could not convert string to float: '331.74 W'

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions