Integrate loudness measurement from local webserver with scrape or rest?

Dear Community

HA

I started to run Home Assistant in our local cultural venue and I want to integrate the current loudness measurements into HA, so everyone working is able to check that we are within the legally allowed bounds. Home Assistant 2023.12.3 is deployed as a Docker install on a Synology NAS in the local network where also the loudness measurement device is situated. This device can either output the dBA to an external display or is accessible through a webserver that runs on the machine itself. Due to legal situation, i cannot access the webserver on the device itself as the machine is responsible for legally binding loundness measurement.

It upgrades the loudness every second on the homepage that we cann access through http://192.168.1.101/monitor.html
With scrape I wanted to integrate it in my HA. For visualisation, the simple homepage looks like follows (the dBA value I want to scrape).


with the following CSS selector:

#level-dba > span:nth-child(1)

This selector doesn’t work. I then found out in other threads that it probably is the case, that i cannot scrape that selector, because the value is loaded with an java script over the address:
http://192.168.1.101/audio_set?monitor=*&status=?

This address is updated every second and has the following content

monitor=*&status={"company":"","location":"","dba":603,"dbc":814,"peak":814,"leq1":{"enable":1,"filter":"a","timeunit":"m","timespan":60,"db":638},"leq2":{"enable":1,"filter":"a","timeunit":"s","timespan":1,"db":603},"audio":{"left":-797,"right":-818},"reduction":{"max":200,"total":0,"multiband":[0,0,0,0,0,0,0,0,0,0]},"limiter":{"allowed_levels":{"a":{"level":1000},"c":{"level":950},"peak":{"level":980}},"time_till_sanction":20,"time_left_till_sanction":20,"sanction_time":20,"sanction_time_left":0,"mode":"live"},"events":[],"serial_number":"6700","cpu_temp":"42.7","cpu_uptime":"28:21%20min","firmware":"3.0T","update_available":0,"sanction":0,"overload":0,"reduce":0,"detect":0,"correlation":86}

What I want to do is have the first value after dba (in this case 603) updated every second in homeassistant and changed from 603 to 60 (the 3 is the decimal value, so 60.3 dba). But here I’m stuck. Can I do that with scrape or with rest or should I use another integration?i

edit:
I tried to scrape the whole html code into HA with the following yaml config:

scrape:
  - resource: http://192.168.1.101/audio_set?monitor=*&status=?
    sensor:
      - name: "db-messung"
        select: "body"

But to no success.

Best and thank you for any inputs and hints, bluecrimson

Use a REST sensor and for the value use:

value_template: >-
  {% set j = ((value).split("status="))[1]|from_json %}
  {{ j['dba']/10 }}

Thank you so much for your help. That works like a charm.

In the end my code looks like this

sensor:
  - platform: rest
    resource: "http://192.168.1.101/audio_set?monitor=*&status=?"
    state_class: measurement
    scan_interval: 1
    name: "dbamessung"
    value_template: >-
      {% set j = ((value).split("status="))[1]|from_json %}
      {{ j['dba']/10 }}
       
  - platform: rest
    resource: "http://192.168.1.101/audio_set?monitor=*&status=?"
    state_class: measurement
    scan_interval: 1
    name: "dbcmessung"
    value_template: >-
      {% set j = ((value).split("status="))[1]|from_json %}
      {{ j['dbc']/10 }}
1 Like

If you are wanting 2 sensors from the same resource - you can use the rest platform instead:

rest:
  scan_interval: 10
  resource: "http://192.168.1.101/audio_set?monitor=*&status=?"
  sensor:
    - name: "dbamessung"
      state_class: measurement
      value_template: >-
        {% set j = ((value).split("status="))[1]|from_json %}
        {{ j['dba']/10 }}
      unique_id: dba-341819e7-239e-4276-8cbc-4f5f75b43046

    - name: "dbcmessung"
      state_class: measurement
      value_template: >-
        {% set j = ((value).split("status="))[1]|from_json %}
        {{ j['dbc']/10 }}
      unique_id: dbc-764f0121-8a3b-4e6b-b247-97ba519794eb

Then you only have to hit the endpoint once for both sensors, instead of twice.
I wouldn’t recommend a scan interval more frequent than 10 seconds.

Ah, thank you for that input. I changed the configuration accordingly.
That works perfectly, thank you so much.

1 Like