Page 2 of 6

Re: Connecting Plugwise-2-py via MQTT to Domoticz

Posted: Monday 17 August 2015 21:16
by dorenberg
SevenW wrote:
dorenberg wrote:Everything works!! Not yet via MQTT but via the solution above. This is the code for Node-red. Slightly adapted. But all circles pop up in the unused list and can be added. I have set the time interval to 10 minutes which is sufficient for me.
Do the circles automatically show up in the "unused list" or do you need to add some hardware and virtual devices first. I am trying to figure out how this works.

Currently I am sending MQTT topics directly from Plugwise-2.py to domoticz/in, but I have to manually create virtual hardware and virtual devices. I do not seem to have control over the "idx", so I cannot use part of the MAC number as index.

Maybe the Node Red way is just superioir over direct MQTT. Still learning here.

SevenW

After copying this code in node-red circles will be found in the unused list in domoticz. The ID of the circle (last 6 numbers/letters) is shown as ID from the plugwise circle.

Re: Connecting Plugwise-2-py via MQTT to Domoticz

Posted: Friday 21 August 2015 13:46
by rron
pepijn wrote:I used Node Red to translate the messages from Plugwise-2-py on MQTT to Domoticz

In Node-Red it looks like this.
Schermafbeelding 2015-07-28 om 20.48.15.png
You can try importing my flow as a starting point.

Code: Select all

[{"id":"44cdab58.bb3254","type":"mqtt-broker","broker":"localhost","port":"1883","clientid":""},{"id":"aa2e3003.55d1d","type":"mqtt in","name":"Plugwise","topic":"plugwise2py/state/energy/#","broker":"44cdab58.bb3254","x":79,"y":43,"z":"5edc3f92.a123c","wires":[["313d8fcf.cec27"]]},{"id":"313d8fcf.cec27","type":"function","name":"Plugwise 2 Domoticz","func":"//\n// Plugwise JSON MQTT bericht vertalen naar Domoticz HTTP\n//\nnode.log (\"Converteer Plugwise MQTT bericht naar Domoticz HTTP\");\nvar pwJSON = JSON.parse(msg.payload);\n\n///json.htm?type=command&param=udevice&hid=$HID&did=$DID&dunit=$DUNIT&dtype=$DTYPE&dsubtype=$DSUBTYPE&nvalue=$NVALUE&svalue=$SVALUE\"\n//var msg.url = 'http://127.0.0.1/json.htm?mac='+pwJSON.mac+'&power='+pwJSON.power+'&energy='+pwJSON.energy\nmsg.url = \"http://127.0.0.1/json.htm?type=command&param=udevice&hid=2&did=\"+pwJSON.mac.substr(10,6)+\"&dunit=1&dtype=248&dsubtype=1&nvalue=&svalue=\"+pwJSON.power+\";\"+pwJSON.energy;\n\nreturn msg;","outputs":1,"x":243,"y":91,"z":"5edc3f92.a123c","wires":[["cb9166da.346e98"]]},{"id":"2b0700e4.d4f9","type":"debug","name":"Debug","active":false,"console":"true","complete":"true","x":627,"y":89,"z":"5edc3f92.a123c","wires":[]},{"id":"cb9166da.346e98","type":"http request","name":"Domoticz","method":"GET","ret":"txt","url":"","x":447,"y":90,"z":"5edc3f92.a123c","wires":[["2b0700e4.d4f9"]]}]
If the flow works, your Plugwise devices should show up on your Devices tab.

Right now it uses HTTP/JSON posts to publish data to Domoticz, I will change this to MQTT but this is some work in progress.
I copied this in node-red and i see only 1 plug instead of 2 and the status isn't updated. One plug is for my solarpannels but the value is not changing. What am I doing wrong.

Re: Connecting Plugwise-2-py via MQTT to Domoticz

Posted: Friday 21 August 2015 18:21
by pepijn
If Domoticz is running on the default port you need to change 127.0.0.1 to 127.0.0.1:8080

Re: Connecting Plugwise-2-py via MQTT to Domoticz

Posted: Saturday 22 August 2015 12:19
by rron
The do both appear now but there is no refresh. How can i make this more frequent?

Re: Connecting Plugwise-2-py via MQTT to Domoticz

Posted: Saturday 22 August 2015 12:50
by pepijn
You need to configure Plugwise-2-py to publish more frequent. Default is 60min i believe.

Re: Connecting Plugwise-2-py via MQTT to Domoticz

Posted: Friday 04 September 2015 15:14
by bink
Also like to further integrate my plugwise circles into Domoticz. Currently have it running with the plugwise_util script and the script from Pepijn (http://www.domoticz.com/forum/viewtopic ... ise#p34352). I can use domoticz now for the following functionalities:
-"real-time" power usage monitoring (cronjob updates it every minute)
-switching of the circle's

I've constructed a script that check's the current circle status and writes it back to domoticz (to update the switch status in Domoticz in case the circle get's switched outside of domoticz as I use the standby killer function on some circles and the switch status can become out of sync). I've tried to implement it in such a way that it will only create a switch event in domoticz if the status doesn't match, however my programming skills are not that good so I never managed to get this implemented. Now I simply publish the current status in Domoticz through the json interface. Disadvantage of this is that it will also trigger a switch event towards the circle and it will therefor not go into stanby mode if this is sent during the time it waits to go into standby (usually 8-12 minutes). In the mean-time I've solved this by only running this script every 15 minutes but this is not ideal...

Anyway it look like using the plugwise-2-py setup could possible solve these problems as it ssupports switch status feedback and if I read the documentation can also be used to import the "historic" power usage into domoticz (e.g. use this to meter Kwh used over a period). Is this assumption correct?

Re: Connecting Plugwise-2-py via MQTT to Domoticz

Posted: Thursday 05 November 2015 11:40
by bloemkool
The original post was an eyeopener for me. NodeRed is pretty cool, did not know it existed.
Did some tweaks on the original post, because I wanted the power reading not every x minutes, but faster.
I have 9 plugwise circles and also want to switch them on and off. Not only reading the energy values.
But then I needed also some scripts, completely in my comfort zone as a unix guy... But then again... another way to switch the devices while everything is already at hand.
Why not switching them on and off in NodeRed?

So I added some NodeRed stuff to also switch them on and off.

What I basically did is to allow a http request in NodeRed that can be called from domoticz. This switches the circles via the MQTT broker.
Create a virtual switch, give it a name and the on action is :

Code: Select all

http://127.0.0.1:1880/switch?mac=038D269&cmd=switch&val=on
and the off action is :

Code: Select all

http://127.0.0.1:1880/switch?mac=038D269&cmd=switch&val=off
It seems a bit slow from time-to-time, but that might be the raspberry.
And then your virtual switch config looks like this:
Image

The only problem now is when I switch a circle with the Plugwise2py interface, Domoticz is unaware of that. That could be solved with the MQTT thing in domoticz. ( working on that )
And of course an idea how to handle the schedules.

Code: Select all

[{"id":"81785a4a.7e87a8","type":"mqtt-broker","broker":"localhost","port":"1883","clientid":""},{"id":"e5050a22.1afaf8","type":"mqtt in","name":"","topic":"plugwise2py/state/power/#","broker":"81785a4a.7e87a8","x":123,"y":104,"z":"4fcfe3e2.b0301c","wires":[["97654dde.689ab"]]},{"id":"c3de3125.3c21d","type":"http request","name":"Domoticz","method":"use","ret":"txt","url":"","x":697,"y":103,"z":"4fcfe3e2.b0301c","wires":[["6f9f1ac2.6815dc"]]},{"id":"97654dde.689ab","type":"function","name":"Power&Energy 2 Domoticz","func":"//\n// Plugwise JSON MQTT bericht vertalen naar Domoticz HTTP\n//\nnode.log (\"Converteer Plugwise MQTT bericht naar Domoticz HTTP\");\nvar powerJSON = JSON.parse(msg.payload);\n\n//var msg.url = 'http://127.0.0.1:8080/json.htm?mac='+pwJSON.mac+'&power='+pwJSON.power+'&energy='+pwJSON.energy\n//msg.url = \"http://127.0.0.1:8080/json.htm?type=command&param=udevice&hid=2&did=\"+pwJSON.mac.substr(10,6)+\"&dunit=1&dtype=248&dsubtype=1&nvalue=&svalue=\"+pwJSON.power+\";\"+pwJSON.energy;\nmsg.url = \"http://127.0.0.1:8080/json.htm?type=command&param=udevice&hid=7&did=\"+powerJSON.mac.substr(10,6)+\"&dunit=1&dtype=248&dsubtype=1&nvalue=&svalue=\"+powerJSON.power+\";\"+powerJSON.energy;\n\nreturn msg;","outputs":"1","noerr":0,"x":451,"y":104,"z":"4fcfe3e2.b0301c","wires":[["39ec969d.c6136a"]]},{"id":"80563945.7fa9c8","type":"mqtt in","name":"","topic":"plugwise2py/state/circle/#","broker":"81785a4a.7e87a8","x":123,"y":226.888916015625,"z":"4fcfe3e2.b0301c","wires":[[]]},{"id":"5ed90d80.2007e4","type":"mqtt in","name":"","topic":"plugwise2py/state/energy/#","broker":"81785a4a.7e87a8","x":128,"y":152,"z":"4fcfe3e2.b0301c","wires":[["97654dde.689ab"]]},{"id":"6f9f1ac2.6815dc","type":"debug","name":"","active":false,"console":"false","complete":"url","x":882.333251953125,"y":103.33333587646484,"z":"4fcfe3e2.b0301c","wires":[]},{"id":"39ec969d.c6136a","type":"delay","name":"Throttle for my slow pi","pauseType":"rate","timeout":"30","timeoutUnits":"seconds","rate":"3","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":593,"y":150,"z":"4fcfe3e2.b0301c","wires":[["c3de3125.3c21d"]]},{"id":"c4bcff5b.3b43","type":"http in","name":"","url":"/switch","method":"get","swaggerDoc":"","x":86,"y":301.25,"z":"4fcfe3e2.b0301c","wires":[["b97606f.f4689f8"]]},{"id":"b97606f.f4689f8","type":"function","name":"Domoticz switch to plugwise","func":"// Switch on and off the plugwise module\n// Make a virtual switch in domoticz and make this the on action:\n//   http://127.0.0.1:1880/switch?mac=0236943&cmd=switch&val=on\n// You can guess the off command.\n\n// No need to parse the output of the /switch payload.\n// It is already an object. That make this script pretty easy\n\nnode.log (\"Converteer Domoticz http:// requests naar Plugwise2py MQTT\");\nmsg.topic = \"plugwise2py/cmd/switch/000D6F000\"+msg.payload.mac;\n\nreturn msg;","outputs":1,"noerr":0,"x":381,"y":301,"z":"4fcfe3e2.b0301c","wires":[["a8555a3c.57aaa8"]]},{"id":"1d62cd01.e29d33","type":"debug","name":"","active":false,"console":"false","complete":"payload","x":892,"y":217,"z":"4fcfe3e2.b0301c","wires":[]},{"id":"a8555a3c.57aaa8","type":"mqtt out","name":"MQTT publish","topic":"","qos":"","retain":"","broker":"81785a4a.7e87a8","x":696,"y":336,"z":"4fcfe3e2.b0301c","wires":[]},{"id":"bf3d9a5b.40c268","type":"debug","name":"","active":false,"console":"false","complete":"topic","x":884,"y":258,"z":"4fcfe3e2.b0301c","wires":[]},{"id":"71ae23d9.8e51dc","type":"http in","name":"","url":"/schedule","method":"get","swaggerDoc":"","x":95,"y":369,"z":"4fcfe3e2.b0301c","wires":[["9fa9e5d4.605618"]]},{"id":"9fa9e5d4.605618","type":"function","name":"Domoticz schedule switch to plugwise","func":"// Switch the schedule on and off on the plugwise module\n// Make a virtual switch in domoticz and make this the on action:\n//   http://127.0.0.1:1880/switch?mac=0236943&cmd=schedule&val=on\n// You can guess the off command.\n\n// No need to parse the output of the /switch payload.\n// It is already an object. That make this script pretty easy\n\n\n// No need to parse the output of the /schedule payload.\n// It is already an object.\n// Only for switching on/off the schedule, not edit the shcedule.\nnode.log (\"Converteer Domoticz http:// requests naar Plugwise2py MQTT\");\nmsg.topic = \"plugwise2py/cmd/schedule/000D6F000\"+msg.payload.mac;\n\nreturn msg;","outputs":1,"noerr":0,"x":354,"y":369,"z":"4fcfe3e2.b0301c","wires":[["a8555a3c.57aaa8"]]}]
edit: some typo's and added a screendump

Re: Connecting Plugwise-2-py via MQTT to Domoticz

Posted: Monday 09 November 2015 8:16
by dorenberg
Very interesting. How did you manage to get faster updates for the circles? Setting the interval on less minutes? Or import the 8 second reading from MQTT. Ultimate goal of mine is to log the data of my plugs into domoticz, but I didn't succeed with this.

Re: Connecting Plugwise-2-py via MQTT to Domoticz

Posted: Monday 09 November 2015 18:59
by bloemkool
dorenberg wrote:Very interesting. How did you manage to get faster updates for the circles? Setting the interval on less minutes? Or import the 8 second reading from MQTT. Ultimate goal of mine is to log the data of my plugs into domoticz, but I didn't succeed with this.
If you did not succeed logging your plugwise devices... Try harder, it's right under your nose....

The plugwise2py plugwise2py/power/# MQTT stuff is updated every 10 seconds for all circles when monitoring is on. The original post was directing to plugwise2py/energy/# which is updated less often. If you paste my code into your NodeRed clipboard you'll see that I read both values into the one function. So somethings might be double and seems a bit hard on my raspberry. Therefore the throttling. This is no problem everything is time stamped.
Difference between te energy and power is that the power shows only the current value while reading and the energy shows the current power reading and the power consumption between updates.
The original code in the function is able to handle both values in the broker.
#correctmeifimwrong please.

Re: Connecting Plugwise-2-py via MQTT to Domoticz

Posted: Monday 09 November 2015 21:09
by dorenberg
Just replaced my code with your code and changed my devices. Thanks for your help, this really improves everything!!!!!!!

Re: Connecting Plugwise-2-py via MQTT to Domoticz

Posted: Tuesday 10 November 2015 7:33
by dorenberg
Just one more question. The plugwise circles don't seem to send the logging data into domoticz. Any ideas? I can see the usage at the moment and some graphs, but not the usage lets say per hour or something.

Re: Connecting Plugwise-2-py via MQTT to Domoticz

Posted: Tuesday 10 November 2015 9:06
by bloemkool
Can you post your plugwise2py config files? Probably monitoring is off there...

Re: Connecting Plugwise-2-py via MQTT to Domoticz

Posted: Tuesday 10 November 2015 9:39
by dorenberg
monitoring has been enabled in my config file for all circles. Interval set at 60 minutes.

Re: Connecting Plugwise-2-py via MQTT to Domoticz

Posted: Tuesday 10 November 2015 10:11
by bloemkool
Re-reading your question ... Ah, so values are stored in Domoticz but not shown the way you expect?
Try to play with the settings in settings -> meters and counters tab.

Personally I expect a bar graph of usage per hour and also a bar graph per hour for a week view. But in my case that is a line graph.
Which is far from ideal.

Re: Connecting Plugwise-2-py via MQTT to Domoticz

Posted: Tuesday 10 November 2015 10:15
by dorenberg
exactly. I only see a line with usage, and usage min/max per day. No bar graphs per hour/day etc.

Re: Connecting Plugwise-2-py via MQTT to Domoticz

Posted: Tuesday 10 November 2015 19:55
by dorenberg
switching kind of works, but command take sometimes 30-60 seconds to switch the circle. Let's see if we can improve that.

Re: Connecting Plugwise-2-py via MQTT to Domoticz

Posted: Wednesday 11 November 2015 0:15
by bloemkool
Does not take that long on my Rpi... usually a second or 2

Re: Connecting Plugwise-2-py via MQTT to Domoticz

Posted: Wednesday 11 November 2015 7:20
by dorenberg
OK. I will investigate. On my RPi the first command will directly switch the circle. If I want to switch it off again it can take up to 90 seconds to switch. Usually only when the second command is given seconds after the first.

Re: Connecting Plugwise-2-py via MQTT to Domoticz

Posted: Wednesday 11 November 2015 22:01
by dorenberg
Cannot seem to find the error. Maybe my RPi is too busy. But in the plugwise2py interface switches happen instantaneously. But that is not my main issue now. Logging of the data per hour perhaps would be the perfect fix!
Gizmocuz, maybe you can look into this to see what we need to do to fix this?

Re: Connecting Plugwise-2-py via MQTT to Domoticz

Posted: Wednesday 02 December 2015 13:17
by SevenW
I have been looking into setting the switch-state of a circle to domoticz, and switching it with domoticz using the on/off actions. As a starting point I took the Nov. 5th node-red scripts of bloemkool.

Where I stand:
- Virtual switches will be added automatically to the devices tab. Only enabled/use those who are actually used to switch. This is similar to automatically added energy-meters for the circles.
- Two way communication: Plugwise-2-web app can switch and update domoticz, and domoticz can switch the circle.
- This design causes infinite update loops, which I have stopped.
- Stopping the infinite loop can have a side effect in rare race-condition cases: When switched externally (e.g. through plugwise-w-web app), domoticz UI switches are not forwarded, but discarded for two seconds. This can result in domoticz being wrong in the actual state of the switch.

I do not have yet the killer idea to solve this. There is always a chance of the device getting an external update (due to external switching, plugwise schedule switching, a state-update heartbeat), and there is always the chance that at the same time the switch is operated form the domoticz UI(s). In the end, domoticz and plugwise need to agree on the actual switch state.

See also: viewtopic.php?f=11&t=2908&p=63910#p63910

SevenW