Scrape value CSS select - debug

Hi,

Can someone please help me debug my code?
I cannot get it to work.
I think it is something wrong with the CSS select, but I do not know what.
I used Copy Select in the inspect tool in Chrome.

I tried to scrape different values from the same site, but without any success.

scrape:
  - resource: https://glb.no/vannstander/tabelloversikt/
    scan_interval: 3600
    sensor:
      - name: vattenflode_agardselva
        select: "#waterlevelstable > table > tbody > tr:nth-child(37) > td:nth-child(5)"
        value_template: '{{ value | replace(",", ".") | float }}'
        unit_of_measurement: "mÂł"
        device_class: water
        state_class: measurement

https://glb.no/vannstander/tabelloversikt/

Scrape this value:

Thanks

//Gustav

That number isn’t in the HTML, so scrape won’t work. Do “View Source” and you won’t find it. It’s dynamically pulled in from:

https://data.glb.no/json/vannstander.json

Build a REST sensor with that URL as the resource and this as the value_template:

value_template: "{{ (value_json|selectattr('navn','eq','ÅgĂ„rdselva')|list|first)['avlesninger'][-1]['avlesning'] }}"

Thank you for your support.

I tried to make a rest sensor, but I cannot get it to work.
I got the following error message in https://jsonpathfinder.com/ when I added the code using https://reqbin.com/ "Unexpected token â€˜ï»żâ€™, "ï»ż [{ “
 is not valid JSON”.

Any idea how to solve this?

Thanks

//Gustav

That was harder than it should have been. The URL is not using correct headers and is sending a Byte Order Mark first. The configuration below works to pull the indicated number: apologies if the name makes no sense in Norwegian:

sensor:
  - platform: rest
    name: "ÅgĂ„rdselva avlesning"
    resource: https://data.glb.no/json/vannstander.json
    value_template: "{{ ((value[1:])|from_json|selectattr('navn','eq','ÅgĂ„rdselva')|list|first)['avlesninger'][-1]['avlesning'] }}"
    unit_of_measurement: "mÂł"
    device_class: water

It gives this:

image

If you open the JSON in a browser and copy/paste it to online tools, it works. My value_template strips off the first byte (value[1:]), converts it from a JSON string to an object, finds the (first and only) data block where navn is ÅgĂ„rdselva, looks up the list of avlesninger data entries, and pulls the avlesning number for the last one ([-1]) of those entries.

1 Like

Thank you, now it works :slight_smile:
Glad you helped me. With my knowledge I would spend a lot of time sorting this out


Regards
Gustav