Which sensor can handle a JSON stream and extract single values?

I am looking for a sensor that can extract data from a JSON stream. I am getting this JSON stream from a wireless speaker (BeoPlay M3). It streams events like changes in volume, source, etc. without delay. In a first step I want to display these real-time information as one or multiple sensors on the Home Assistant frontend.

I can view the stream outside Home Assistant using a curl GET request. Single events look like this ("muted" changed):

{"notification":{"id":54314,"timestamp":"2018-09-09T19:24:44.810638","type":"VOLUME","kind":"renderer","data":{"speaker":{"level":40,"muted":true,"range":{"minimum":0,"maximum":60}}}}}

{"notification":{"id":54315,"timestamp":"2018-09-09T19:24:52.808627","type":"VOLUME","kind":"renderer","data":{"speaker":{"level":40,"muted":false,"range":{"minimum":0,"maximum":60}}}}}

Is there a sensor or component that can directly or using curl receive and analyse the stream and feed values of selected keys (e.g. id, type or level) to single sensors?

(Online example for a JSON stream:

curl https://proxy.streamdata.io/http://stockmarket.streamdata.io/prices

Source: https://streamdata.io/developers/docs/#_curl)

Running Hass.io 0.75.3 on Raspberry Pi 3 B+.

I think the command line sensor is what you need, and use the value_template to extract the field from the json

Thank you for your suggestion.

I already tried that:

  - platform: command_line
    name: streamdata_test_1
    command: "curl https://proxy.streamdata.io/http://stockmarket.streamdata.io/prices"
    value_template: '{{ value_json.id }}'

But it does not work. On the frontend the sensor shows: -/Unknown

(In contrast, in the case of a valid response, but a non-existent key id, the sensor value would be empty)

Log:

Timeout for command: curl https://proxy.streamdata.io/http://stockmarket.streamdata.io/prices
19:49 components/sensor/command_line.py (ERROR)

Update of sensor.streamdata_test_1 is taking over 10 seconds
19:49 core.py (WARNING)

I assume that the command_line sensor has a defined order of processing by requesting data -> receiving data -> extracting values -> putting those in Home Assistant pipeline, repeating periodically at scan_interval.

The difference with a JSON stream is that the response has no end, hence the command_line sensor timed out. Instead, analysing and extracting has to take place each time a new part of the response comes in. Which sensor could do this?

I don’t know of a sensor that would run, or read a connection continuously, but you could try run the command externally and redirect the output to a file. Then use the file sensor to read the file

Hey, did you ever found a solution? i have the same thing :slight_smile:

1 Like