Scrape sensor on local website

Hello, im new to home assistant but i really enjoy the platform! I have a question though, i use winpower manager ups server to monitor my ups which is runnning on localhost (192.168.1.xx:xxx) and i need to scrape values from the web page (Voltage etc) and add them to sensor. I try to use scrape platform but i dont know what to use on selector. I tried to use only the id (#in-v) but the sensor returns nothing. As far as i understant i need to provide the full path to the specific id but im lost after that. I upload a picture of the webpage and its html code. I appreciate everybody for the help :slight_smile:

Thank you!!

Please post the code you used for your scrape sensor. You donā€™t need the full path ā€” what you are trying to do should work if:

  • your code is correct
  • that website is published as HTML rather than dynamically-generated with XMLHTTP (AJAX) requests

Thereā€™s no need to conceal the IP address or port number on your local LAN.

Thanks for your reply. The code is this:

Example configuration.yaml entry

But now i see that the site is dynamicaly generated from javascript and ajax. So i think the scrape platform is not going to work right? Is there any other way to take the values i want?

I have same problem. Same UPS page, actually webserver generating from Winpower manager 6.
Did you find some way of extracting values to HA sensors?

Unfortunately I didnā€™t find any solution. I donā€™t think there is a solution like this because the page is dynamic.

If the page is dynamic, then the first step is to open Developer Tools in your web browser, refresh the webpage and then look at the Network tab in your browser and see what requests are being made. With any luck the dynamic information will be coming from a static endpoint that is returning JSON and in that case, you can get direct access to the raw data.

2 Likes

You are right! the json file located in https://192.168.1.139:xxxx/0/json. And it generates json files every second.

{
   "key":"USB/LINE-INT",
   "device":{
      "key":"USB/LINE-INT",
      "id":0,
      "protocol":4,
      "portIndex":4,
      "ip":null,
      "status":"",
      "upsIndex":0,
      "statusIcon":"online",
      "hasWarn":false
   },
   "version":"",
   "status":"Normal",
   "model":"LINE-INT",
   "batV":"27.8V",
   "bypassFreq":"",
   "ls2":-1,
   "inVolt":"238.2V",
   "outW":"",
   "inFreq":"",
   "workMode":0,
   "bypassVolt":"",
   "outFreq":"49.6Hz",
   "ls1":-1,
   "outVolt":"238.2V",
   "oidType":0,
   "iStatus":0,
   "outVA":"",
   "noModule":false,
   "warning":"",
   "extStatus":0,
   "outA":"",
   "abmState":"",
   "batTemp":"",
   "emdTemp":"",
   "emdAlarm1":"",
   "emdAlarm2":"",
   "lastEvent2":"2021/10/15 21:18:43 Start Agent",
   "cfgKVA":"",
   "lastEvent1":"",
   "upsTemp":"",
   "batTimeRemain":"",
   "batCapacity":"100%",
   "loadPercentMax":26,
   "emdHumidity":"",
   "loadPercent":"26%",
   "cfgBatNumber":"",
   "loadSegment1State":"",
   "statusColor":0,
   "redundantNumber":"",
   "loadSegment2State":"",
   "supportTest":true
}

So now i will need a sensor to parse the json file and have the values batV, inVolt and outVolt.
Any ideas how is this possible? Maybe i will need a template sensor?

Thanks a lot!

Depends what bits you are after?
Iā€™d create a main sensor with I am guessing inVolt is what you were trying to get before, as the value of the rest sensor. Then everything else you want is listed under json_attributes.

sensor:
  - platform: rest
    resource: https://192.168.1.139:xxxx/0/json
    headers: 
      Content-Type: application/json
    name: UPS Sensor
    value_template: '{{ value_json.inVolt | float }}'
    json_attributes:
      - status
      - batV
      - outVolt
      - batCapacity

From there you can either create multiple template sensors to take the attributes of this sensor, or you leave them where they are, because you can still trigger automations from attributes rather than the main state, and you can still access the attributes to send notifications etc.

3 Likes

Perfect! It works! I made different sensor for each attribute.

Thank you very much!

1 Like

Yes, thank you very much!! Oh, so much to learn about HA.
Tip for other people like me, put ā€œverify_ssl: falseā€ in sensor.

1 Like

Hi
Trying to format the time that comes from this sensor, it reads for example 37m3s and i want to convert it to time so i can get it into a graph.

This is the code i use.

  sensor:

    - platform: rest
      resource: https://192.168.1.xxx:8888/0/json
      verify_ssl: false
      headers: 
        Content-Type: application/json
      name: UPS Sensor
      value_template: '{{ value_json.inVolt | float }}'
      json_attributes:
        - statusIcon
        - lastEvent1
        - lastEvent2
        - status
        - outFreq
        - loadPercentMax
        - batCapacity
        - batTimeRemain
        - inVolt
        - loadPercent
        - outVolt
        - batV

    - platform: template
      sensors:
        ups_battimeremain:
          friendly_name: "Time left"
          value_template: "{{ state_attr('sensor.ups_sensor', 'batTimeRemain') }}"