[SOLVED] Get state with webpage

Hello everyone, I have some problems with the implementation of a rule, I have to read a value on a web page and if the value is 1 change the status of the items in ON if 0 in OFF, every 5 seconds I have to read the status on the page .

rule “Stato Sonoff5”

when

Time cron “3 * * * * ?”

then

var stato5

stato5=getHttpRequest(“http://192.168.1.90/stato.php?idx=43”)

if (stato5 = 1) {

Sonoff5.state = ON

}

if (stato5 = 0) {

Sonoff5.state = OFF

}

end

where am I wrong??
Thk!

You might want to look at what you actually get in response to your http request. It certainly won’t be numeric 1 or zero. It will come in string form. But most likely there’ll be other web-pagey padding as well, and you will have to parse out the part you are interested in.

the page already returns only a value of 1 or 0 how can I see the value that is assigned to the variable on the logs?

a line in your rule
logInfo( "myRule" , "stato5 is " + stato5.toString)

But do note that 1 and “1” are not the same thing, and the http request returns a string. It can never equal 1.

can read if 1 or 0 but never enters into the if how can I convert the data to integer?

Why don’t you compare with string instead?
Anyways,
val newnumber = Integer::parseInt("XX")
should do the trick

1 Like

This assigns the value 1 to stato5. To compare, you need ==.

2 Likes
rule “Stato Sonoff5”
when
    Time cron “3 * * * * ?”
then
    var String stato5 = getHttpRequest(“http://192.168.1.90/stato.php?idx=43”)
    if (stato5 == "1") Sonoff5.sendCommand(ON)
    if (stato5 == "0") Sonoff5.sendCommand(OFF)
end

You could do that with a transformation
Make sure the MAP transform is installed in the paperUI
Create a file called httpsonoff.map in the transform folder with the following content:

0=ON
1=OFF

Then your rule:

rule “Stato Sonoff5”
when
    Time cron “3 * * * * ?”
then
    Sonoff5.sendCommand(transform("MAP", "httpsonoff.map", getHttpRequest(“http://192.168.1.90/stato.php?idx=43))
end
1 Like

@BaXaras
Was your problem solved?
Can you tick the solution, please?
Thanks

I solved that

create a items.items

Switch Sonoff5 “Sonoff5” <light> (gStudio) [ “Switchable” ]

create a rule.rules

////////////////Sonoff5//////////

rule “Sonoff5 ON”

when

Item Sonoff5 received update ON

then

sendHttpGetRequest(“http://ipdomoticz:8080/json.htm?type=command&amp;param=switchlight&amp;idx=43&amp;switchcmd=On”)

end

rule “Sonoff5 OFF”

when

Item Sonoff5 received update OFF

then

sendHttpGetRequest(“http://ipdomoticz:8080/json.htm?type=command&amp;param=switchlight&amp;idx=43&amp;switchcmd=Off”)

end

///-------//////////////////////CONTROL STATE///////////////////////

rule “Stato Sonoff5”

when

Time cron “0/10 * * * * ?”

then

var String stato5 = sendHttpGetRequest(“http://ipdomoticz/stato.php?idx=43”)

logInfo( “myRule” , "Stato5 is " + stato5)

if (stato5.toString.contains(“1”)) {

Sonoff5.state = ON

}

if (stato5.toString.contains(“0”)) {

Sonoff5.state = OFF

}

logInfo( “myRule” , "Sonoff5 is " + Sonoff5.state )

thank you all for the advice