Scrape 123solar

Hi, i’ m novice and i need help to configure a sensor for import power from my 123solar web page using scrape. the page “123solar/programs/programmultilive.php” publish this data:
‘[{“GPTOT”:431.1,“PMAXOTD”:0,“PMAXOTDTIME”:“08:48”}]’
i need to get value after GPTOT in watts
i configure in configuration.yaml
sensor:

can someone help me correct the “value_template”

thanks

Please format your code properly by surrounding it with three backticks. See item 11.

If that’s real JSON coming from the PHP resource, you could try a REST sensor:

sensor:
  - platform: rest
    resource: http://192.168.1.210/123solar/programs/programmultilive.php
    name: Produzione fotovoltaico
    value_template: '{{ value_json[0]["GPTOT"] }}'
    unit_of_measurement: "W"
    scan_interval: 60
1 Like

thank for help, I tried but the sensor does not show the value.
maybe you can find the solution reading
this is programmultilive.php code:

<?php
// Credit Louviaux Jean-Marc 2013
include('../scripts/read_maincfg.php');
date_default_timezone_set($DTZ);

function tricsv($var)
{
    return !is_dir($var) && preg_match('/\.csv$/i', $var);
}

for ($invt_num = 1; $invt_num <= $NUMINV; $invt_num++) {
    $invtnum = $invt_num;
    include('../scripts/read_invtcfg.php');
    $systemid = '1231' . $invt_num;
    @$shmid = shmop_open($systemid, 'a', 0444, 256);
    if (!empty($shmid)) {
        $livedash[0] = shmop_read($shmid, 0, 256);
        shmop_close($shmid);
    } else {
        $nowUTC      = strtotime(date('Ymd H:i:s'));
        $livedash[0] = "$nowUTC,0,0,0,0,0,0,0,0,0,0,0,0,0,0";
    }
    $array = preg_split('/,/', $livedash[0]);
    
    if (!isset($GPTOT)) {
        $GPTOT = 0;
    }
    
    $array[9] *= $CORRECTFACTOR;
    $GPTOT += $array[9];
}

if ($GPTOT > 1000) {
    $GPTOT = round($GPTOT, 0);
} else {
    $GPTOT = round($GPTOT, 1);
}

@$shmid = shmop_open(12320, 'a', 0444, 32); //pmotd
if (!empty($shmid)) {
    $data = shmop_read($shmid, 0, 32);
} else {
    $nowUTC      = strtotime(date('Ymd H:i:s'));
    $data = "$nowUTC,0";
}
$pmotdarray = preg_split('/,/', $data);
$pmotd_t    = date('H:i', $pmotdarray[0]);

$arr = array(
    'GPTOT' => floatval($GPTOT),
    'PMAXOTD' => floatval(round($pmotdarray[1], 0)),
    'PMAXOTDTIME' => ($pmotd_t)
);

$ret = array(
    $arr
);
header("Content-type: text/json");
echo json_encode($ret);
?>

Sorry, my eyes start to bleed when reading PHP.

I assume you restarted HA to read the new sensor definition?

To check the connection, remove the value_template line, restart, then post a screenshot of the sensor in Developer Tools / States, like this but for your sensor:

yes i always restart HA every mod.

I’m out of suggestions then: the value_template line I suggested above ought to pull out the GPTOT value. What does the sensor show with that line back in? Are there any errors in the log?

with value template show unknow

the logs not error show

maybe some error in text ?

i’m using file editor directly in HA

Lose the quotes around the 0:

value_template: '{{ value_json[0]["GPTOT"] }}'

Apologies: that was in my first attempt for a few seconds before I realised and edited them out. :frowning_face: You must have been quick to copy my original response.

Working, thank you very much,

1 Like

hello how I can read total data yearly ?

hello from my page http://192.168.1.51/metern/programs/programtotal.php
I have this data:
{"Totalcounter1":"23.480,872 k","Dailycounter1":"226 ","Totalcounter2":"22.196,979 k","Dailycounter2":"4,7 k","Totalcounter3":"5.359,363 k","Dailycounter3":"4,5 k","Totalcounter4":"4.346,347 k","Dailycounter4":"0 ","Totalcounter5":"4.285,523 k","Dailycounter5":"226 ","Totalcounter6":"615,696 k","Dailycounter6":"60 ","Totalcounter7":"1.860,490 k","Dailycounter7":"408 ","Totalcounter8":"2.889,468 k","Dailycounter8":"4,0 k","Totalcounter9":"4.752,102 k","Dailycounter9":"4,4 k","Totalcounter10":"--","Dailycounter10":"--"}

sensor it is correct as below:

sensor:
  - platform: rest
    resource: http://192.168.1.51/metern/programs/programtotal.php
    name: Produzione fotovoltaico Totale day
    value_template: '{{ value_json[0]["Dailycounter1"] }}'
    unit_of_measurement: "W"
    scan_interval: 60

I tried like this but when I restart I have no data, maybe the formatting of the sensor is wrong?

image

Few mistakes there:

  • your data is in value_json["Dailycounter1"] — there’s no list, so no need for [0].
  • A counter for energy should have a unit of measurement of Wh or kWh, not W.
  • Your screenshot suggests that the sensor is picking up a unit of “Kwh” from somewhere, overriding the value in your configuration. This should be kWh.

If you have any control over the PHP script, maybe modify it to output “UK-format” numbers (decimal point not comma, no multipliers): for example, "23480872" instead of "23.480,872 k".

Yes I change with dot and not comma see below:

{"Totalcounter1":"23.481.958 k","Dailycounter1":"1.3 k","Totalcounter2":"22.198.161 k","Dailycounter2":"5.9 k","Totalcounter3":"5.359.595 k","Dailycounter3":"4.7 k","Totalcounter4":"4.346.481 k","Dailycounter4":"134 ","Totalcounter5":"4.286.475 k","Dailycounter5":"1.2 k","Totalcounter6":"615.928 k","Dailycounter6":"292 ","Totalcounter7":"1.860.490 k","Dailycounter7":"408 ","Totalcounter8":"2.889.468 k","Dailycounter8":"4.0 k","Totalcounter9":"4.752.102 k","Dailycounter9":"4.4 k","Totalcounter10":"--","Dailycounter10":"--"}

the sensor does not appear, and I do not understand why.
Then, now what should I do to read the value correctly? I mean with the dot?
but it doesn’t show me the value with the dot but in full…
now it is as below:


  - platform: rest
    resource: http://192.168.1.51/metern/programs/programtotal.php   
    name: solar consumi totale giornaliero
    value_template: '{{ value_json.Dailycounter2 }}'
    force_update: true
    unit_of_measurement: kWh
    device_class: power

Look at your logs. You’ll probably see an error about declaring units of kWh but passing a non-numeric value of 5.9 k. Also, remove the device_class: power as kWh is an energy measurement.

You can’t have two dots in a number. You need to swap dots and commas, or better still remove the thousand separators (dots in EU, commas in UK). Your Totalcounter1, for example, needs to be 23481.958 k. Once you’ve done that, try this as a template if you cannot remove the k multiplier:

    value_template: >
      {% set v = value_json.Dailycounter2 %}
      {% set k = " k" in v %}
      {{ v.replace(" k","")|float(0) * (1000 if k else 1) }}

1 Like

your code doesn’t work unfortunately.

My screenshot shows that it does, for numbers that use no commas and use dots for decimals. Please post the latest PHP output and your latest sensor definition, along with any log errors.

{"Totalcounter1":"23.486,200 k","Dailycounter1":"5,6 k","Totalcounter2":"22.202,765 k","Dailycounter2":"10,5 k","Totalcounter3":"5.360,639 k","Dailycounter3":"5,8 k","Totalcounter4":"4.347,165 k","Dailycounter4":"818 ","Totalcounter5":"4.290,039 k","Dailycounter5":"4,7 k","Totalcounter6":"616,968 k","Dailycounter6":"1,3 k","Totalcounter7":"1.860,490 k","Dailycounter7":"408 ","Totalcounter8":"2.889,468 k","Dailycounter8":"4,0 k","Totalcounter9":"4.752,102 k","Dailycounter9":"4,4 k","Totalcounter10":"--","Dailycounter10":"--"}

So we are back to dots for thousands and decimal commas :roll_eyes:. If you can’t get the PHP to feed you real numbers, try this:

value_template: >
      {% set v = value_json.Dailycounter2 %}
      {% set k = " k" in v %}
      {{ v.replace(" k","").replace(".","").replace(",",".")|float(0) * (1000 if k else 1) }}

you can’t remove the commas, I’ve already checked…

Obviously, if you do change $DPOINT / $THSEP to '.' and '', don’t use their replaces in the template above.