I have an old HDTV controlled by an Arduino. The Arduino knows if the state of the TV is “ON” or “OFF”. I’ve been trying to use rules to set a switch on or off by what is received from the Arduino. It’s not working. I’ve tried integers < and >, string ==, you name it.
Right now my rule is:
rule "television"
when
Item televisionpower changed
then
var result = sendHttpGetRequest("URL FOR ARDUINO") <<--- gets "ON" or "OFF" from Arduino
tvstatus.postUpdate(result) <<-- displays ON or OFF on the openhab menu so know it's working to here
if (tvstatus.state == "ON") television.state = ON <<-- I've tried tvstatus.state, integer, < >, result ==...lots of stuff
else television.state = OFF
end
any ideas?
In order to set the state of an item you have to use the postUpdate
action. Either;
postUpdate(television, ON)
or
television.postUpdate(ON)
Either should work.
First, why don’t you use the http binding to get the state from arduino, since you defined an item already?
Second, if you try to compare a string, you have to use a string on both sides of equality sign, so try out
if (tvstatus.state.toString == "ON")
Just as well you could compare states, but states aren’t strings, so you would have to write
if (tvstatus.state == ON)
without quotations around the ON
. Surely it depends upon your Item Definition (string? switch?) if the state comparison would be successful.
I just tried both of those.
if (tvstatus.state.toString == “ON”)
and
if (result == “ON”)
I’m either doing some way wrong or stupid, or comparing to strings to make decisions is not possible
it is definitely possible
Could you please make a log what result
really does contain?
logInfo("mylog","Result is {}",result)
should do it, you will find the log in ./log/openhab.log