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

Mike I looked at the JSON you provided.

The ServiceId is: "urn:micasaverde-com:serviceId:EnergyMetering1

What a silly mistake. I should have looked at it properly. Its working now. One thing I still would like to fix, the value displayed is with a huge amount of digits. Is there a way to round this ? Either in PLEG in a condition, or in the device itself ? I would like it to have max 2 decimal places. In multistring I can round, but looking through the PLEG manual I cannot see a possibility to do that. If there is a integer fuction, I could multiply by 100, take the interger and then devide by 100 again… but I have not seen that function

You need to keep full precision in the PLEG Condition otherwise your results will not be accurate. It would be better to apply rounding before display using Lua. So replace your current PLEG action with this:

local kWhR = string.format("%0.2f", KWH.state) luup.variable_set("urn:micasaverde-com:serviceId:EnergyMetering1", "KWH",kWhR,379)

That worked. I was thinking of a separate rounding condition in PLEG for exactly the reason you stated. Thanks everybody for all your help. And I learnt a few things along the way as well…

I’m glad you got what you wanted. You can round down in PLEG Conditions by using the Modulus operator (%) but it gets a bit messy:

KWhR ((KWh * 100) - ((KWh * 100) % 1)) / 100

I was looking at the modulus operator, But I thought it only gives me the fraction. In your formula, what is % 1 doing ? From the PLEG manual I wasn’t sure how to use it, and before I could try you came up with the Lua solution… but for future reference I would like to understand the formula

X % 1 gives the remainder of dividing X by 1 - so the fractional part. X - (X % 1) is therefore the integer part of X. You can use it for rounding if you add 0.5: (X + 0.5) - ((X + 0.5) % 1).

You can also use it with a decimal modulus so you could get your two decimal places using:

(X + 0.005) - ((X + 0.005) % 0.01)

I tried to add another logic to the calculation. What I want to do is add up KWh, but only if it is over 924 W, and I only want to add up anything above 924 W. I thought I have the solution, but I get a error, attempt to perform arithmetic on a nil value

This is the condition in question:

KWh2 No sMidNight ? 0 : ( (pPower != lastPower) ? ((lastPower > 924) ? (KWh2 + (((lastPower-923)/1005) * (#pPower - #lastPower) / 3600))) : KWh2 ) null 2015-04-20 11:24:45.662 2015-04-20 12:29:51.380

I count 3 “?” characters and only two “:” characters … so something is not fully defined!

Another approach:
Create a condition:
PowerChanged pPower

Use LUA to do your math in this condition … and write to a KWH2 variable.
If you also need that for other logic in PLEG than make that variable an input property.

I am not really comfortable with Lua… I changed the condition a few times, still the same error.
This is my latest version (not functioning). I thought I could use the … : KWh2 as a ‘no’ for both cases. Obviously not. So I tried to have a : KHh2 for each , but somehow it doesn’t work

KWh2 No sMidNight ? 0 : ( (pPower != lastPower) ? ((lastPower > 924) ? (KWh2 + (((lastPower-923)/1005) * (#pPower - #lastPower) / 3600)) : KWh2) : KWh2 ) null

I looked at your REPORT … You need to move the KWH2 condition UP to be executed before the lastPower condition!

Otherwise pPower will always be equal to lastPower!

I always get caught out with that. But the error is still there. I tried these 2 versions:

KWh2 sMidNight ? 0 : ( (pPower != lastPower) ? ((lastPower > 924) ? (KWh2 + (((lastPower-923)/1005) * (#pPower - #lastPower) / 3600)) : KWh2) : KWh2 )

KWh2 sMidNight ? 0 : ((pPower != lastPower) ? ((lastPower > 924) ? ((KWh2 + (((lastPower-923)/1005) * (#pPower - #lastPower) / 3600)) : KWh2)) : KWh2 )

I think you are getting the error because KWh2 is currently nil so PLEG will not add to it. This will get fixed when sMidNight fires. You could also fix it by temporarily changing the Condition to force it to have a value of 0.

There must be something else wrong. I have tried firing sMidNight with do it now, but i cannot even do that, I get device communication failure. Also that PLEG has a red banner on the bottom Lua failure. If I change the condition to somethin easy like KWH2 KWh, that all disappears. So something in my condition is wrong. I just tried to expand on your condition which is working fine. This is the original (your version)

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

I thought the easiest way to add another condition which counts everything above 923 Watts, and adds that to KWh2 was the way I tried… I must admitt I have never before used the ?, but I thought that was a very good way. But somewhere I have a mistake in my logic.

Testing a few different things, somehow it has something to do wit KWh2 being 0. But it worked on KWh without a problem…

I tried this (only to try and narrow down wheer the error is coming from):

KWh2 No lastPower > 923 ? (KWh2+ ((lastPower-923)/1005) * (#pPower - #lastPower) / 3600) : KWh

and got the error. I changed it to this:

KWh2 No lastPower > 923 ? (1 + ((lastPower-923)/1005) * (#pPower - #lastPower) / 3600) : KWh

and it works. The problem is, even if I wait until after midnight, sMidNight will not trigger as the PLEG is not running due to the error

Now that KWh2 has a non-nil value, you should be able to restore the expression.

Nope. I get a boolean error with this one

KWh2 sMidNight ? 0 : ((pPower != lastPower) ? ((lastPower > 924) ? ((KWh2 + (((lastPower-923)/1005) * (#pPower - #lastPower) / 3600)) : KWh2)) : KWh2 )

Then i tried this:

sMidNight ? 0 : ( (pPower != lastPower) ? ((lastPower > 924) ? (1+ (((lastPower-923)/1005) * (#pPower - #lastPower) / 3600)) : KWh2) : KWh2 )

That worked without errors, so I replaced the 1 with KWh2 and got the attempt to perform arithmatic on nil value again…

sMidNight ? 0 : ( (pPower != lastPower) ? ((lastPower > 924) ? (KWh2 + (((lastPower-923)/1005) * (#pPower - #lastPower) / 3600)) : KWh2) : KWh2 )

It cannot be that difficult, can it ??? It was working straight away with your first formula. Why is this causing so much trouble ? I have to say I am close to asking how to do this in Lua, it would probably be easier. But just from a point of having it all in PLEG as conditions under each other and if changes need to be made, its a lot easier to see whats going on. And I do not like giving up… It does really sound and look as if the KWh2 starting value is causing a problem. But as that boolean error on teh first one I tried came up, there is obviously also an error in my logic. That did not happen on the last one. But maybe that is another reason for it to fail ?

Look at this. I have just tried this:

KWh No sMidNight ? 0 : ((pPower != lastPower) ? (KWh + (((lastPower-23)/1005) * (#pPower - #lastPower) / 3600)) : KWh ) 0.16604957553529 2015-04-20 22:22:10.856 2015-04-20 22:19:58.900

KWh2 No sMidNight ? 0 : ((pPower != lastPower) ? (KWh2 + (((lastPower-23)/1005) * (#pPower - #lastPower) / 3600)) : KWh2 ) null 2015-04-20 22:02:45.045 2015-04-20 22:04:04.415

same formula, bottom one produces an error. Now I thought maybe its the name… so I changed it

KWh No sMidNight ? 0 : ((pPower != lastPower) ? (KWh + (((lastPower-23)/1005) * (#pPower - #lastPower) / 3600)) : KWh ) 0.16628052599776 2015-04-20 22:25:11.920 2015-04-20 22:23:22.831

Immersun No sMidNight ? 0 : ((pPower != lastPower) ? (Immersun + (((lastPower-23)/1005) * (#pPower - #lastPower) / 3600)) : Immersun ) 0.00008124469030

but if you thought now changing my formula in the same way would work, it doesnt. Its either boolean error or divided by nil value still. I’ll give up for tonight.

Use two expressions (order matters!!!)

newValue KWh2 + (((lastPower-23)/1005) * (#pPower - #lastPower) / 3600)
KWH2 sMidNight ? 0 : ((pPower != lastPower) ? newValue: KWh2 )

Now to initialize … Run your sMidNight schedule … it should set KWH2 to zero. It was failing before because newValue will continue to get an error until KWH2 gets a non null value. By breaking this into two conditions … it does not cause the KWH2 to get a math error during it’s evaluation when KWH2 is currently null.

I thought of splitting but could not find a solution. I only want to add up values above 923 Watts. If I adapt the newValue to

newValue KWh2 + (((lastPower-923)/1005) * (#pPower - #lastPower) / 3600)

newValue will add up negative values while lastPower is below 923. So I would need a if lastPower > 923 then … I couldn’t work out how to, I am having a mental block

I think I might have fixed it. I changed the condition to:

sMidNight ? 0 : ((pPower != lastPower) ? ((lastPower > 924) ? (1 + Immersun + (((lastPower-923)/1005) * (#pPower - #lastPower) / 3600)) : Immersun) : Immersun )

then after a while that lastPower was above 923, I changed it back to

sMidNight ? 0 : ((pPower != lastPower) ? ((lastPower > 924) ? (Immersun + (((lastPower-923)/1005) * (#pPower - #lastPower) / 3600)) : Immersun) : Immersun )

obviously the Immersun value now iss wrong as its been adding up 1+ …, but no errors. The value should be ok after sMidNight has run and reset it to 0 I hope…