Can I extract data from a website to use in PLEG (as a variable)

Yes i am pretty sure i understand the logic. I think that could work. It’s a bit late for me now but I’ll program that in tomorrow and see how it goes. I could use Variable container or something like that to see the value of the variable … just to keep an eye for a few days and compare to the website and possibly extend the interval to reduce cpu usage.
One idea I had. Is there a way i could display that variable in my device ? The kWh in the device is blank, so maybe it could be diplayed there ? I would probably need @lolodomo to tell me which variable is being displayed in that device, then write kWh to that variable ?
Just an idea…

One idea I had. Is there a way i could display that variable in my device ? The kWh in the device is blank, so maybe it could be diplayed there ?

Yes - just set an Action for the Condition KWh that uses PLEG’s SetVariable action to write the required variable. You would need to set Repeat on for KWh.

...and possibly extend the interval to reduce cpu usage.

The logic will update KWh whenever it gets a new power value. If you want to reduce the frequency you would need to change it a little. The following change should prevent updates faster than once per 30 seconds:

Conditions
chgPower lastPower; pPower > 29
KWh sMidNight ? 0 : (chgPower ? (KWh + ((lastPower/1000) * (#chgPower - #lastPower) / 3600)) : KWh )
lastPower chgPower? pPower : lastPower

You could also limit updates by how much the power changes. This should only update if the power changes by at least 10%:

Conditions
chgPower ((pPower-lastPower) >= (lastPower/10)) or ((lastPower-pPower) >= (lastPower/10))
KWh sMidNight ? 0 : (chgPower ? (KWh + ((lastPower/1000) * (#chgPower - #lastPower) / 3600)) : KWh )
lastPower chgPower? pPower : lastPower

This is what I’ve done so far:

Schedules
sMidNight Weekly 05:00:00 1,2,3,4,5,6,7 None Interval 00:00:05 None false 2015-04-15 07:55:15.182 2015-04-15 07:55:20.100

Device Properties
pPower Power Solar Panels Watts 322 2015-04-15 08:43:21.288 2015-04-15 08:39:21.339

Conditions
chgPower No lastPower; pPower true 2015-04-15 08:28:21.183 2015-04-15 08:21:35.355
KWh No sMidNight ? 0 : (chgPower ? (KWh + ((lastPower/1000) * (#chgPower - #lastPower) / 3600)) : KWh ) 0.076620324308276 2015-04-15 08:43:21.332 2015-04-15 08:39:21.391
lastPower No pPower 322 2015-04-15 08:43:21.344 2015-04-15 08:39:21.403

I have also attached the full status but I think its easier to show just the relevant bits here as its a big PLEG.

I have started with the do it now on the schedule. After that I watched my KWh in ther status. That should increase with every change. It doesn’t so something is slightly wrong I think.

Try changing to:

lastPower chgPower? pPower : lastPower

Also try:

chgPower pPower != lastPower

Something is still not quite right. The number in KWh is changing, but it is going negative.

KWh No sMidNight ? 0 : (chgPower ? (KWh + ((lastPower/1000) * (#chgPower - #lastPower) / 3600)) : KWh ) -3.3149971883759

Also there must be something else slightly wrong with how we calculate KWh. I have been monitoring the changes and compared to what I can see on the website

KWh Owl website

-2.254 0.7
-2.309 0.8
-2.944 0.8
-3.31 0.9
-4.5 1.0

I have changed lastPower to chgPower? pPower : lastPower, the figures above are from after the change

I have changed lastPower to chgPower? pPower : lastPower, the figures above are from after the change

Did you also change:

chgPower pPower != lastPower

No I didn’t see it. Changed it now, fingers crossed… and I got the Multistring working now as well displaying the Value. Next project is to have it displayed in the PV device

Many thanks for all your work !

There is still a gremlin somewhere in the calculations…

KWh OWL

-5.94 1.3
-6.7 1.5
-6.77 1.6
-7.07 1.7
-10.4 1.9

chgPower No pPower != lastPower true 2015-04-15 10:16:20.108 2015-04-15 10:15:53.652
KWh No sMidNight ? 0 : (chgPower ? (KWh + ((lastPower/1000) * (#chgPower - #lastPower) / 3600)) : KWh ) -7.5627948234535 2015-04-15 10:29:20.406 2015-04-15 10:29:08.374
lastPower No chgPower? pPower : lastPower 1288 2015-04-15 10:29:20.438 2015-04-15 10:29:08.387

I think I might have found a glitch.

KWh No sMidNight ? 0 : (chgPower ? (KWh + ((lastPower/1000) * (#chgPower - #lastPower) / 3600)) : KWh ) -0.31325140817655 2015-04-15 12:44:55.844 2015-04-15 12:44:49.143
lastPower No chgPower? pPower : lastPower 2392 2015-04-15 12:44:55.856 2015-04-15 12:44:43.863
lastpowertime No (#chgPower - #lastPower) -254.7188000679 2015-04-15 12:45:43.456 2015-04-15 12:44:55.868
kwhcalc No (KWh + ((lastPower/1000) * (#chgPower - #lastPower) / 3600)) -0.48249789977722 2015-04-15 12:45:43.468 2015-04-15 12:44:55.883

I had reset sMidNight to start with 0.
(#chgPower - #lastPower) has gone negative which in turn causes KWh to go negative. I will try to change to (#lastPower - #chgPower)

Not sure how that will work but I am trying that now

That was one problem. What is happening though lastPower timestamp changes to the current time, and chgPower doesn’t. Next time this happens this interval gets longer, and added again to KWh. So KWh increases too much. Example with a snapshot a few minutes apart:

chgPower No pPower != lastPower true 2015-04-15 13:26:15.130 2015-04-15 13:25:34.793
KWh No sMidNight ? 0 : (chgPower ? (KWh + ((lastPower/1000) * (#lastPower - #chgPower) / 3600)) : KWh ) 18.423663961577 2015-04-15 13:26:15.167 2015-04-15 13:24:32.277
lastPower No chgPower? pPower : lastPower 2484 2015-04-15 13:26:15.188 2015-04-15 13:24:32.305
lastpowertime No (#lastPower - #chgPower) 0.05768609046936 2015-04-15 13:26:15.210 2015-04-15 13:25:34.803
kwhcalc No (KWh + ((lastPower/1000) * (#lastPower - #chgPower) / 3600)) 18.42370376498 2015-04-15 13:26:15.227 2015-04-15 13:25:34.817

and a few mins later:

chgPower No pPower != lastPower true 2015-04-15 13:26:15.130 2015-04-15 13:25:34.793
KWh No sMidNight ? 0 : (chgPower ? (KWh + ((lastPower/1000) * (#lastPower - #chgPower) / 3600)) : KWh ) 19.402439199988 2015-04-15 13:32:20.295 2015-04-15 13:31:20.709
lastPower No chgPower? pPower : lastPower 2530 2015-04-15 13:32:20.309 2015-04-15 13:31:20.720
lastpowertime No (#lastPower - #chgPower) 365.17838406563 2015-04-15 13:32:20.321 2015-04-15 13:31:20.732
kwhcalc No (KWh + ((lastPower/1000) * (#lastPower - #chgPower) / 3600)) 19.659078453234 2015-04-15 13:32:20.336 2015-04-15 13:31:20.747

I think the problem is when PLEG updates without a change in Power. Then KWh gets updated, with the timestamp of chgPower not being changed. If PLEG would only update KWh when chgPower is triggered, it would work I think. So I could try moving this logic to a dedicated PLEG. But I think that’s overkill and I would have to get another license… there must be another way but I cannot think of one at the moment…

I have tried moving the logic to a deditacted PLEG. The problem is still the same. PLEG status attached

Try this slightly simpler version:

KWh sMidNight ? 0 : ((pPower != lastPower) ? (KWh + ((lastPower/1000) * (#pPower - #lastPower) / 3600)) : KWh )
lastPower pPower

Otherwise we may have to go back to Lua…

I am planning to modify PLEG to be able to run actions from INPUT changes (triggers, schedules, device properties) as well as conditions.

In this case you can put this type of code in the LUA action for the INPUT property. From the LUA code you can write another property, possibly one created on the PLEG device itself.
That property can be used in other PLEG logic … via a Property input.

In case you need it, and I am not online, here’s some Lua to try.

Device Properties
pPower Current power reading from solar unit

Conditions
updkWh [Repeat=Y] pPower

Actions
updkWh

local sID = <Service ID of power device> local dID = <Device number of power device> local lastp, tlastp = luup.variable_get(sID,"LastPower",dID) luup.variable_set(sID,"LastPower",PPOWER.state,dID) if (tlastp or 0) ~= 0 then local kWh = tonumber((luup.variable_get(sID,"KWH",dID))) or 0 kWh = kWh + ((tonumber(lastp) or 0) / 1000) * (os.time() - tlastp) / 3600 luup.variable_set(sID,"KWH",kWh,dID) end

You need to set appropriate values for sID and dID to suit your device. The code creates new variables LastPower and KWH using these. You could place them in another device or use MultiString variables though.

The daily reset can be done using PLEG’s SetVariable action and writing zero to KWH.

Thanks i might give the Lua code a try. At the moment things look good with the last modification. I only tested for the rest of the afternoon, and the result was close enough for what I want it to do. But I’ll see over a full day tomorrow.

[quote=“RichardTSchaefer, post:33, topic:186888”]I am planning to modify PLEG to be able to run actions from INPUT changes (triggers, schedules, device properties) as well as conditions.

In this case you can put this type of code in the LUA action for the INPUT property. From the LUA code you can write another property, possibly one created on the PLEG device itself.
That property can be used in other PLEG logic … via a Property input.[/quote]

That sounds useful, Richard. I shall be interested to see how you manage the timing. If a change in an input property causes Lua code to be run which changes another property, can that initiate the evaluation of Conditions that use it?

It does right now … but that will be a separate evaluation …
i.e. if LUA (or an Action) does a SetVariable … the variable will not effect subsequent condition evaluations in the set of conditions.

A separate evaluation because the variable was changed will happen and ALL of the conditions will be re-evaluated with the variable change.

I am struggling to get my Power device to display the calculated value. I have tried endless versions, nothing works, @korttoma has tried to help me in another thread but its not working. I have not tried using the Lua version above, I am not really sure about what sid to use and the PLEG version is working well enough.

The calculated (PLEG condition which has the value to be displayed) variable is KWh
The Power device where I want to display it is device id 379
The Power device variable I want to use is KWH

I tried this (and countless other versions) in the luup section of a PLEG action

local Value = KWh
luup.variable_set(“urn:micasaverde-com:serviceId:EnergyMeter1”, “KWH”,KWh,379)

First you need to find-out for sure the ServiceId and name of the variable displayed by the power device. If the variable is shown on the device’s Advanced tab, hovering your mouse over the variable name will show you the ServiceId. If it isn’t shown, you will need to inspect the device’s json file (or post it here).

Once you have that information, you could use PLEG’s SetVariable action to update the variable.

If you use Lua in PLEG, you need to use special syntax to access Inputs and Conditions. For your example, you would need to use:

luup.variable_set("urn:micasaverde-com:serviceId:EnergyMeter1", "KWH",KWH.state,379)

I tried to use PLEG set variable. I used this:

ServiceId urn:micasaverde-com:serviceId:EnergyMeter1
VariableName KWH
Value {(KWh)}
Device 379

that did not work. I have the .json attached. From what I can see (but I never looked at a .json before) that looked right to me…

I have just seen, KWH does now show up in advanced (It didnt before), with the right value. But its not shown on the device. The KWH is there as before (see previous attached Power Solar Panels pic), but still no value next to it.