We have moved at community.getvera.com

Author Topic: Can I extract data from a website to use in PLEG (as a variable)  (Read 11761 times)

Offline mikee123

  • Hero Member
  • *****
  • Posts: 1521
  • Karma: +18/-11
Re: Can I extract data from a website to use in PLEG (as a variable)
« Reply #45 on: April 17, 2015, 03:48:38 am »
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

Offline RexBeckett

  • Beta Testers
  • Master Member
  • *****
  • Posts: 3891
  • Karma: +483/-13
Re: Can I extract data from a website to use in PLEG (as a variable)
« Reply #46 on: April 17, 2015, 04:51:40 pm »
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)

Offline mikee123

  • Hero Member
  • *****
  • Posts: 1521
  • Karma: +18/-11
Re: Can I extract data from a website to use in PLEG (as a variable)
« Reply #47 on: April 20, 2015, 07:43:22 am »
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 
« Last Edit: April 20, 2015, 07:45:01 am by mikee123 »

Offline RichardTSchaefer

  • Community Beta
  • Master Member
  • ******
  • Posts: 10091
  • Karma: +764/-143
Re: Can I extract data from a website to use in PLEG (as a variable)
« Reply #48 on: April 20, 2015, 09:22:24 am »
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.
« Last Edit: April 20, 2015, 09:27:47 am by RichardTSchaefer »

Offline mikee123

  • Hero Member
  • *****
  • Posts: 1521
  • Karma: +18/-11
Re: Can I extract data from a website to use in PLEG (as a variable)
« Reply #49 on: April 20, 2015, 09:48:27 am »
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

Offline RichardTSchaefer

  • Community Beta
  • Master Member
  • ******
  • Posts: 10091
  • Karma: +764/-143
Re: Can I extract data from a website to use in PLEG (as a variable)
« Reply #50 on: April 20, 2015, 10:16:13 am »
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!

Offline mikee123

  • Hero Member
  • *****
  • Posts: 1521
  • Karma: +18/-11
Re: Can I extract data from a website to use in PLEG (as a variable)
« Reply #51 on: April 20, 2015, 10:43:34 am »
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 )
« Last Edit: April 20, 2015, 10:47:55 am by mikee123 »

Offline RexBeckett

  • Beta Testers
  • Master Member
  • *****
  • Posts: 3891
  • Karma: +483/-13
Re: Can I extract data from a website to use in PLEG (as a variable)
« Reply #52 on: April 20, 2015, 01:26:50 pm »
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.

Offline mikee123

  • Hero Member
  • *****
  • Posts: 1521
  • Karma: +18/-11
Re: Can I extract data from a website to use in PLEG (as a variable)
« Reply #53 on: April 20, 2015, 02:32:35 pm »
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
« Last Edit: April 20, 2015, 02:49:45 pm by mikee123 »

Offline RexBeckett

  • Beta Testers
  • Master Member
  • *****
  • Posts: 3891
  • Karma: +483/-13
Re: Can I extract data from a website to use in PLEG (as a variable)
« Reply #54 on: April 20, 2015, 03:51:31 pm »
Now that KWh2 has a non-nil value, you should be able to restore the expression.

Offline mikee123

  • Hero Member
  • *****
  • Posts: 1521
  • Karma: +18/-11
Re: Can I extract data from a website to use in PLEG (as a variable)
« Reply #55 on: April 20, 2015, 05:22:00 pm »
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 ?

Offline mikee123

  • Hero Member
  • *****
  • Posts: 1521
  • Karma: +18/-11
Re: Can I extract data from a website to use in PLEG (as a variable)
« Reply #56 on: April 20, 2015, 05:27:00 pm »
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.
« Last Edit: April 20, 2015, 05:41:41 pm by mikee123 »

Offline RichardTSchaefer

  • Community Beta
  • Master Member
  • ******
  • Posts: 10091
  • Karma: +764/-143
Re: Can I extract data from a website to use in PLEG (as a variable)
« Reply #57 on: April 20, 2015, 07:27:25 pm »
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.

Offline mikee123

  • Hero Member
  • *****
  • Posts: 1521
  • Karma: +18/-11
Re: Can I extract data from a website to use in PLEG (as a variable)
« Reply #58 on: April 21, 2015, 02:55:00 am »
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

Offline mikee123

  • Hero Member
  • *****
  • Posts: 1521
  • Karma: +18/-11
Re: Can I extract data from a website to use in PLEG (as a variable)
« Reply #59 on: April 21, 2015, 06:11:42 am »
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...