Extract value from json

Hi, I have I think a rather simple query which hopefully someone can help with.

The API returns a value of 13.29 V using Postman

"current_sensor_state": "13.29 V",

I think with the below value template I am using, I need to extract the number only (exclude the space and the unit of measurement (V)

  - scan_interval: 1800
    resource: https://console.radiobridge.com/api/visualization/v1/devices/[REDACTED]
    headers:
      Authorization: !secret radiobridge_token
      Organization: [REDACTED]
      Accept: application/json
      Content-Type: application/json
    sensor:
      - name: volmeter_current_sensor_state
        value_template: "{{ value_json.data.device_status.current_sensor_state }}"
        unique_id: radiobridge_volmeter_current_sensor_state
        device_class: voltage
        unit_of_measurement: "V"

Do you think someone could help me to extract just the value with the correct value template please?

Many thanks.

If it returns JSON then post the whole JSON. We cannot guess at the structure.

The JSON path is correct I think, it is extracting the values as per the object that I need help with.

However, herewith the whole JSON payload

{
    "data": {
        "id": 2619,
        "device_id": "[REDACTED]",
        "device_type_id": 54,
        "device_type_name": "RBS306-VM30 - LoRa Wireless Voltage Sensor",
        "device_name": "Batalert1",
        "network_key": "[REDACTED]",
        "network_name": "[REDACTED]",
        "last_seen": "2024-05-06 11:37:52",
        "region": "US915",
        "register_status": "Active",
        "device_status": {
            "lastmsg_time": "Aug 22, 2023 15:51:12 (UTC)",
            "battery": "3.2V",
            "tamper": "No",
            "tamper_detect_since_reset": "Yes",
            "config_error": "No",
            "rate_limit_status": "No",
            "current_sensor_state": "13.32 V",
            "hardware_version": "2.4",
            "firmware_version": "2.0.2",
            "low_battery": "No",
            "last_seen": "1692719472",
            "rssi": null,
            "snr": null,
            "last_downlink_status": "Msg Valid",
            "measurement": "13.32 V",
            "accumulation_count": "44",
            "event_description": "Periodic Report"
        },
        "device_key": "[REDACTED]",
        "join_eui": "[REDACTED]",
        "subscription": null,
        "gateway": null,
        "extra_notes": null,
        "created_at": "2021-02-23 12:31:36",
        "commissioned_at": "2021-02-23 22:18:04",
        "active_gateway": "[REDACTED]",
        "user": {
            "id": [REDACTED],
            "timezone_format": "UTC",
            "console_only": false,
            "timezone_id": 41,
            "identity": {
                "id": [REDACTED],
                "last_login": "2024-05-05 12:21:10",
                "joined_on": "2021-02-23 12:27:30",
                "activated": 1
            }
        }
    }
}

Not sure what the question is then. DO you want just the number without the " V" ?
The path looks correct so if you look at the state of sensor.volmeter_current_sensor_state is it “13.32 V”?

If yes, what is the question? Or better said what do you mean by “just the value”?

If you only want the number:

{{ "24.23 V".split(" ")[0] }}

Would be “24.23”. Assuming of course there is a space always in the result and you don;t want the unit of measurement. So I would think this:

{{ (value_json.data.device_status.current_sensor_state).split(' ')[0]  }}

Or if you want a float then this:

{{ (value_json.data.device_status.current_sensor_state).split(' ')[0]  | float }}

sorry, I thought I had made it relatively clear when I said

I think with the below value template I am using, I need to extract the number only (exclude the space and the unit of measurement (V)

I did mention that the path was correct, so I am not sure how I could have phrased the question better?

Anyway, I think that there must be a limitation of some sort on the API as I am still getting an error saying “REST result could not be parsed as JSON” which I thought was being driven by the spaces and UOM, which is isn’t.

OK, understood I missed that in the original question. But you are not getting values and the part I see as an error in your JSON could just be from your edit (not sure because they deal with the redacted part):

This is a JSON error in two places:

        "user": {
            "id": [REDACTED],
            "timezone_format": "UTC",
            "console_only": false,
            "timezone_id": 41,
            "identity": {
                "id": [REDACTED],
                "last_login": "2024-05-05 12:21:10",
                "joined_on": "2021-02-23 12:27:30",
                "activated": 1
            }
        }

Both id entries are not strings, but others are so maybe you redacted the quotes also? Using a JSON parser on your input shows this:

image

Value should be a string surrounded by double quotes or it should be an array, an object, a number, null, or the true/false boolean flags.

If you edited this (assuming REDACTED means that) then those two are wrong. If not and they are actually in the payload, you need a more “friendly” JSON parser (possibly JQ) to process the data because the device sending that data is sending incorrectly structured JSON.

That is not it, those values are irrelevant and simply part of the entire payload that is returned. They are not used at all in my query.

btw, this syntax is incorrect as it throws:

configuration is not valid: Error loading /config/configuration.yaml: while parsing a block mapping in "/config/sensors.yaml", line 112, column 3 expected <block end>, but found '<scalar>' in "/config/sensors.yaml", line 121, column 84

when I use your example (I tried it just in case)

{{ (value_json.data.device_status.current_sensor_state).split(' ')[0] }}

Well no. value_json would have nothing if the JSON is wrong so they are not irrelevant at all. If it sent back like:

foo
bar
Stuff
{"foo": "bar"}
more stuff
wow

You could not use a JSON parser to grab the value of the key “foo” because the entire JSON is invalid. I dont have your data nor setup. I asked once, with no answer … without anything I suggested, do you get the value as the state with the “V” on it? Or not?

Plus that syntax is not incorrect, using a simple template:

Perhaps you have double quotes mixed both inside and outside the template? That in the template editor proves that a value of “24.23 V” is processed to 24.23 as a float. Replacing that with your template should not be an issue, so you have some other quoting issue for the sensor or you are not actually returning the data at all as the JSON is invalid … and you are using value_json. It is not ignoring the fact that the JSON is invalid and letting you go on to a lower level to get data. It would error the instant you asked for value_json and nothing else would happen.