Scrape sensor working but only want the numerical value

I have a second Raspberry Pi running PiHole along with PiVPN & I’d like to monitor the temp of the processor on that Pi.
I’ve tried a few different ways and haven’t managed to get it working the way I really want (I’m open to suggestions to make it better).

At the moment I have a scrape sensor working but it’s reporting the value as “Temp: 51.5 °C”.
In an ideal world, I would just want the numerical value and the unit of measurement to be “°C”.
(Id like to be able to use this in a graph and also a trigger for automation if needed)

I’m guessing the best way would be with a second sensor & some templating but I’m just not sure how to do the templating etc.

All help/advice/suggestions appreciated.

Can you post your code for the scrape sensor? You should just be able to pipe it to get rid of anything you don’t want.

Hi @DavidFW1960,

I’m scraping from the GUI for PiHole, this is the code below:

- platform: scrape
  name: PiVPN CPU Temp
  resource: http://192.168.XX.XX/admin/index.php
  select: "#temperature"

Please see below for the elements from the GUI page:
2018-10-31_2139

oh bugger… well you will need a template sensor / value_template then to parse the value for that.

If you click on the sensor in dev tools what does the JSON look like? (in the states) Does the Temp: 50.5 °C show as the state?

Yep, it’s showing the full text as “Temp: 50.5 °C”

But is it showing that as a state???

I think so, it’s in the states box, where you would expect to find on/off for switches etc.

Something like this:

sensor:
  - platform: template
    sensors:
      pivpncputemp:
        entity_id:
          - sensor.pivpn_cpu_temp
        friendly_name: "PiVPN CPU Temp"
        value_template: >
          {{states.sensor.pivpn_cpu_temp.state | replace("Temp: ", "") | replace("°C","") }}%

Try that…

Many thanks @DavidFW1960, but unfortunately its now displaying as “Temp: 49.4 %”

Take the % off the end of the line. Doh!
Then, it seems the substitution for Temp: is wrong… put the right value in there.
Maybe this:

sensor:
  - platform: template
    sensors:
      pivpncputemp:
        entity_id:
          - sensor.pivpn_cpu_temp
        friendly_name: "Pi VPN CPU Temp"
        value_template: >
          {{states.sensor.pivpn_cpu_temp.state | replace("Temp:", "") | replace("°C","") }}

The best thing to do to troubleshoot stuff like this is to use the template tool in the dev tools. You can just plug in the value template and see what it returns.

1 Like

That’s done the trick :white_check_mark:

Many thanks @DavidFW1960

It’s just a matter as playing with it sometimes… the dev tool is invaluable. It’s pretty easy once you get the hang of it.

Glad you sorted it out.

To tell the truth, I’d completely forgotten about the Dev template tool.
Templating on the whole is something I’m still getting to grips with (I can see from looking at your template what you’ve done, but I’d have no idea how to put that together myself (well,not yet anyways)).

Many thanks for all your help :+1:

1 Like

The reason my first one had a % on the end was because I just copied/edited a template I already had… I’m lost writing them from scratch but I have a bit of an understanding now and can hack stuff to suit. The more you do it the easier it gets.

That’s is true, hopefully I’ll get better as time passes.

There is one small issue I’ve noticed. The new template sensor dosnt seem to update when the original changes it’s value.
Woukd it be something like scan interval or force update to to it updated???

hmm…
After the platform and before the sensors try adding a scan_interval:

sensor:
  - platform: template
    scan_interval: 5
    sensors:

I’m not sure what the default scan interval is

Many thanks @DavidFW1960

I think the issues with it not updating was my error, I’d renamed the original sensor so I could easily tell which was the new/old one & hadn’t updated the new sensor to reflect this - all working now.

:+1:

1 Like