Read JSON Data and calculate value

Hi guys! Pls help!
I created a sensor to read JSON Data from a url and extract the current price for bitcoin.

Worked perfectly.

Now I want to integrate red and green arrows if the price is rising or falling.

Can I use rules for that?

Read JSON Data --> store the value --> compare the stored value to the next value (eg. every 10sec)

–> write sensor value UP/DOWN

Thx in advance!

Lee

Yes, you can do rules for this. For storing the old value you will use in memory variable. The compare can be done in a rule. For UP/DOWN arrow you will use an image widget linked with a custom sensor with a value set by the rule.

Kind regards,

Michal

Thanks Michal,
can you give me an example for storing a value in rules, sorry, haven´t worked with rules before…

This forum post might help.

https://groups.google.com/forum/#!searchin/openremotecommunity/In-memory/openremotecommunity/er-pDE1prGQ

There are is also lots of examples and guidance for rules on the GitHib OpenRemote Documentation page -

https://github.com/openremote/Documentation/wiki/Rules

And a few examples that contains references to storing values within in-memory registers can be found here.

https://github.com/openremote/Documentation/wiki/OpenRemote-Rules-examples

I may be mistaken with this link, but I think this is where you can find the syntax for the maths you need.

(I'm sure Michal will happily correct me on this, he knows very well that my skills aren't in coding)

https://docs.jboss.org/drools/release/7.6.0.Final/drools-docs/html_single/index.html

So - could be like this:

package org.openremote.controller.protocol;
global org.openremote.controller.statuscache.CommandFacade execute;
import java.util.*;
import java.util.regex.*;
**save value from "Sensor bitcoin price"**


Rule "if bitcoin price rises"

   Event( source == "Sensor bitcoin price", value > saved value )

then

  custom sensor( "Green arrow true" );
else
    custom sensor( "Green arrow false" )

end

The rule should look something like this (not tested, writing from my head so from nothing :wink: ):

rule"if bitcoin price changes"
when
Event(source==“Sensor bitcoin price”, $price: value)
Event(source==“Old in-memory variable sensor”, $oldPrice: value != $price)
then
double current_price = Double.parseDouble($price.toString());
double old_price = Double.parseDouble($oldPrice.toString());
if(current_price > old_price){
execute.command(“Image Widget command”, “greenArrow.png”); // this should set green arrow in the custom sensor linked with image widget
}else{
execute.command(“Image Widget command”, “redArrow.png”);
}
// remember the old value
execute.command(“Old in-memory variable command”, $price);
end

``

This code is not tested so it can not run at the first time. Nevertheless, it gives you an idea what commands, sensors, images and widget you should have. When you define all this in the designer come back if you have the problems with the rule, or come back if everything is running from the first attempt :slight_smile:

Kind regards,

Michal

Thank you very much Michal!
Had to chew on this one - and the code looks ugly as hell - but it does what it should:

the “else” command didn´t work, so I put in 2 rules. But the oldPrice had no initial value, so I created a 3rd rule to initially set the old value.

But again - thank you Michal

package org.openremote.controller.protocol;

global org.openremote.controller.statuscache.CommandFacade execute;

import java.util.*;

import java.util.regex.*;

rule “initial”

timer (cron: 0 0 0/1 * * ?)

when

eval(true)

Event(source == “ETH SENSOR”, $val: value)

then

double val = Double.parseDouble($val.toString());

{execute.command(“ETH OLD”, $val.toString());}

end

rule “up”

timer (cron: 0 0/1 * * * ?)

when

eval(true)

Event(source == “ETH SENSOR”, $val: value)

Event(source == “ETH OLD SENSOR”, $oldval: value)

then

double val = Double.parseDouble($val.toString());

double oldval = Double.parseDouble($oldval.toString());

if(val>oldval)

{execute.command(“ETHTREND UP”);}

{execute.command(“ETH OLD”, $val.toString());}

end

rule “down”

timer (cron: 0 0/1 * * * ?)

when

eval(true)

Event(source == “ETH SENSOR”, $val: value)

Event(source == “ETH OLD SENSOR”, $oldval: value)

then

double val = Double.parseDouble($val.toString());

double oldval = Double.parseDouble($oldval.toString());

if(val<oldval)

{execute.command(“ETHTREND DOWN”);}

{execute.command(“ETH OLD”, $val.toString());}

end