pdmangel / node-red-contrib-openhab2

Other
21 stars 15 forks source link

Get item value on startup #10

Open fab33 opened 6 years ago

fab33 commented 6 years ago

Is it possible to add an option on openhab-in to get value on startup ? I know I can use openhab-get with inject node but the payload is the object.

pdmangel commented 6 years ago

The node is already programmed to get the item's state at startup and expose it as an ItemStateEvent. Is this sufficient ?

fab33 commented 6 years ago

So it doesn't work ! I put an openhab-in node on a new flow with a debug connected to the 2 outputs. When deploying flow, nothing happen on debug node and under openhab-in it's "state: ?" until state chage.

pdmangel commented 6 years ago

I didn't occur to my configuration ... however after inspecting the code I might have found the issue. Fixing it might take some time since some code refactoring is required.

fab33 commented 6 years ago

Ok it's not very important. Most of my sensors send update frequently. I will test in the next release.

pdmangel commented 6 years ago

Please check the new version 1.0.7. I started with delaying status retrieval for a few seconds.

fab33 commented 6 years ago

This new version cause node-red crashing.

18 Oct 15:46:35 - [info] [openhab2-controller:OpenHab] url = http://localhost:8080
18 Oct 15:46:35 - [warn] [openhab2-out:2cb3d6d3.e8d46a] response error '{"statusCode":400,"body":"<html>\n<head>\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"/>\n<title>Error 400 Bad Request</title>\n</head>\n<body><h2>HTTP ERROR 400</h2>\n<p>Problem accessing /rest/items/Bureau_Lampe. Reason:\n<pre>    Bad Request</pre></p><hr><i><small>Powered by Jetty://</small></i><hr/>\n\n</body>\n</html>\n","headers":{"date":"Wed, 18 Oct 2017 13:46:35 GMT","content-type":"text/html; charset=ISO-8859-1","cache-control":"must-revalidate,no-cache,no-store","content-length":"311","connection":"close","server":"Jetty(9.2.19.v20160908)"},"request":{"uri":{"protocol":"http:","slashes":true,"auth":null,"host":"localhost:8080","port":"8080","hostname":"localhost","hash":null,"search":null,"query":null,"pathname":"/rest/items/Bureau_Lampe","path":"/rest/items/Bureau_Lampe","href":"http://localhost:8080/rest/items/Bureau_Lampe"},"method":"POST","headers":{"content-length":0}}}' on 'http://localhost:8080/rest/items/Bureau_Lampe'
18 Oct 15:46:35 - [info] [openhab2-controller:OpenHab] url = http://localhost:8080
18 Oct 15:46:35 - [info] [openhab2-controller:OpenHab] url = http://localhost:8080
18 Oct 15:46:35 - [info] [openhab2-controller:OpenHab] url = http://localhost:8080
18 Oct 15:46:35 - [warn] [openhab2-out:2cb3d6d3.e8d46a] request error 'Error: Argument error, options.body.' on 'http://localhost:8080/rest/items/Bureau_Lampe'
18 Oct 15:46:35 - [red] Uncaught Exception:
18 Oct 15:46:35 - TypeError: First argument must be a string or Buffer
    at ClientRequest.OutgoingMessage.write (_http_outgoing.js:457:11)
    at Request.write (/root/.node-red/node_modules/request/request.js:1501:27)
    at end (/root/.node-red/node_modules/request/request.js:546:18)
    at Immediate.<anonymous> (/root/.node-red/node_modules/request/request.js:575:7)
    at runCallback (timers.js:672:20)
    at tryOnImmediate (timers.js:645:5)
    at processImmediate [as _immediateCallback] (timers.js:617:5)

So for the crash, after testing few version : 1.0.5 : OK 1.0.6 : NOK 1.0.7 : NOK

pdmangel commented 6 years ago

In order to find the crash : can you export your flows so I can have a look at your setup ?

I also forgot to document a change in 1.0.6 : the openhab2-out node now doesn't translate the input payload anymore to 'ON' or 'OFF'. Maybe that explains the openhab2-out warnings. I created a subflow with a function node containing following code and reuse it in fron of several openhab2-out nodes:

switch (msg.payload)
{
    case 0:
    case "0":
    case false:
    case "false":
    case "FALSE":
    case "off":
    case "OFF":
        msg.payload = "OFF";
        break;
    case 1:
    case "1":
    case true:
    case "true":
    case "TRUE":
    case "on":
    case "ON":
        msg.payload = "ON";
        break;

    default:
        break;
}
return msg;
fab33 commented 6 years ago

Ok my error is related to the 0 or 1 payload. The extracted flow :

[{"id":"7f3011c8.e9462","type":"openhab2-in","z":"b465423f.964af","name":"","controller":"46bc8ee8.cb259","itemname":"Bureau_Interrupteur","x":170,"y":120,"wires":[["6a2522b7.b6b18c"],[]]},{"id":"6a2522b7.b6b18c","type":"rbe","z":"b465423f.964af","name":"","func":"rbe","gap":"","start":"","inout":"out","x":348,"y":114,"wires":[["f3b403d7.02af1"]]},{"id":"f43c080a.53d768","type":"mqtt in","z":"b465423f.964af","name":"","topic":"/lelas/switch/SONOFF_TOUCH_BUREAU/button/0","qos":"2","broker":"e722c289.13777","x":266,"y":52,"wires":[["7dba53cc.2be92c"]]},{"id":"a8141fb5.1b78","type":"change","z":"b465423f.964af","name":"set White","rules":[{"t":"set","p":"payload","pt":"msg","to":"0,0,100","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":716,"y":52,"wires":[["d2c8173d.939cd8"]]},{"id":"7dba53cc.2be92c","type":"switch","z":"b465423f.964af","name":"Double tap","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"3","vt":"str"}],"checkall":"true","outputs":1,"x":557,"y":52,"wires":[["a8141fb5.1b78"]]},{"id":"f3b403d7.02af1","type":"change","z":"b465423f.964af","name":"","rules":[{"t":"change","p":"payload","pt":"msg","from":"ON","fromt":"str","to":"100","tot":"num"},{"t":"change","p":"payload","pt":"msg","from":"OFF","fromt":"str","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":560,"y":120,"wires":[["d2c8173d.939cd8"]]},{"id":"d2c8173d.939cd8","type":"openhab2-out","z":"b465423f.964af","name":"","controller":"46bc8ee8.cb259","itemname":"Bureau_Lampe","topic":"ItemCommand","payload":"","x":910,"y":115,"wires":[]},{"id":"46bc8ee8.cb259","type":"openhab2-controller","z":0,"name":"OpenHab","protocol":"http","host":"localhost","port":"8080","path":"","username":"","password":""},{"id":"e722c289.13777","type":"mqtt-broker","z":"","broker":"127.0.0.1","port":"8883","tls":"","clientid":"","usetls":true,"compatmode":true,"keepalive":"60","cleansession":true,"willTopic":"","willQos":"0","willPayload":"","birthTopic":"","birthQos":"0","birthPayload":""}]

When I change "change" node to string 0 or 1 it's ok, no more crash. It's quite annoying because if you made an error it's difficult to recover (node-red crashing so you can't modify faulty node). You must roll back to 1.0.5, change node and revert to 1.0.7. I have a lot of flows so I will prefer to stay in 1.0.5 until this issue will be fixed. And one strange things : Problem happen only after restarting node-red. First deployement is ok.

fab33 commented 6 years ago

I don't have time to make a PR change line 221 with body: String(payload) Sting function return a string primitive (not an object) It works ok without crashing. Perhaps you have to document about this. Another approch is to filter on type and change bullet under node to red when value is not a string.

pdmangel commented 6 years ago

Thanks for the hint! I'll correct ASAP.

fab33 commented 6 years ago

And finally with 1.0.7, values are ok on startup but it's not ok when deploying a flow (when modified) we need to update it. After thinking about it I think it's a good idea to update when deploying flow or on startup. For example if you use a finite state machine in node-red, the order of data is very important. Perhaps a better way is to add a checkbox to know if the node have to retrieve state.