Ninja Sphere and Ninja Blocks with openHAB

Hi @federic0

Yeah this is a known issue and I haven’t spent any time fixing it - its actually Issue #1 :smile:

Its a really simple fix - just need to add another level to the mqtt addressing - which will break everyone’s rules :frowning:

I didn’t realise that the ninja cape used the GID to identify items (i only have one temperature sensor!), so I hardcoded it to be ‘1’

OK so I literally just banged together what I think will work…

completely untested…

should be
ninjaCape/input/31/0202 for your first temp sensor and
ninjaCape/input/31/0301 for the second

test it and let me know!

1 Like

Hi @perrin, thanks a lot for the quick reply! Just tested your new code, it’s not working for me… I’m trying to see what’s going on but here’s an error I get when I try to actuate a wall plug:

ubuntu@ninjablock:/opt/openhab$ ./
Connecting...  /dev/ttyO1
Traceback (most recent call last):
  File "./", line 132, in <module>
  File "./", line 69, in mqtt_to_JSON_output
    json_data = '{"DEVICE": [{"G":"'+ topics[3] +'","V":0,"D":' + topics[2] + ',"DA":"' + mqtt_message.payload + '"}]})'
IndexError: list index out of range 

Otherwise, there’s no output generated from

The error I just cited is coming from my actuator’s topic missing the GID, the code is expecting an array of size 4. @perrin, in order not to break other’s rules and items you could probably test for the size of topics and when size is 3 go with your previous code.
The code is working, I had a typo in my item declarations for the thermometers. Sorry. However, you need to add a GID of ‘0’ and not ‘1’ to other items with the new script, i.e.:

mosquitto:ninjaCape/input/11:state:ON: 000000000000010101010001

needs to be:

mosquitto:ninjaCape/input/11/0:state:ON: 000000000000010101010001
1 Like

@federic0 Nice! thanks for testing this. Yeah, i’ll put a test in that if there is no GID specified then use zero - which should allow backwards compatibility.
Once I get around to it… :slight_smile:

Hi guys, I’m struggling with some sensors (cheap rf 433 Mhz PIRs and door sensors). When triggered they send a burst of signals (can be 4 signals within a second) and they seem to be activating my rules “in parallel”. The items are coming from the mqtt broker which receives them from the NinjaBlock. Anyone with NB with the same issue?
(I started a topic on this with more details here)

1 Like

my solution here :slight_smile:

Hi Everyone,

I have a working setup using OpenHAB and MQTT running on a RPi and a gateway on an Arduino as a receiver/transmitter for 433Mhz for FSK modulation. Now I want to my NB as a “gateway” for 433Mhz ASK modulation. If I got it right I just need to install MQTT client, shutdown Ninjablocks and install the script to collect the data from the receiver/transmitter and send through MQTT, is that right?

Perrin, thanks for the script.


1 Like

pretty much, yeah. The script just monitors the serial port and converts it to mqtt messages.

There’s some info on the wiki about how to configure items etc.

1 Like

Hi Perrin,

The script is working with some issues. The first time I run it I get an error that there is no route to route. If I just run the same command again (with no change) it works (see below). When I start it as service I have the same symptom and even when it starts as a service, it works for few minutes and stop sending messages to MQTT (running in another server). I have other gateways sending message to this MQTT server without problems and the Ninja blocks is connected to the same network and I monitored the connectivity and couldn’t find any network error. Any thoughts?

Thanks in advance!!!

root@ninjablock:/home/ubuntu# python /opt/openhab/
Connecting… /dev/ttyO1
Traceback (most recent call last):
File “/opt/openhab/”, line 117, in
mqttc.connect(broker, port, 60)
File “/usr/local/lib/python2.7/dist-packages/paho/mqtt/”, line 612, in connect
return self.reconnect()
File “/usr/local/lib/python2.7/dist-packages/paho/mqtt/”, line 734, in reconnect
sock = socket.create_connection((self._host, self._port), source_address=(self._bind_address, 0))
File “/usr/lib/python2.7/”, line 571, in create_connection
raise err
socket.error: [Errno 113] No route to host

root@ninjablock:/home/ubuntu# python /opt/openhab/
Connecting… /dev/ttyO1
Subscribed. mid: 1
line to decode: {“DEVICE”:[{“G”:“0”,“V”:0,“D”:1007,“DA”:“000000”}]}

looks like its the MQTT client thats crashing out, which is odd. Make sure you have the latest version from here:
or do a

pip install paho-mqtt

The “no route to host” suggests an issue with your network - could you have a duplicate IP address or something simple thats interfering with it?

Hi Perrin,

I did a full update/upgrade of the ubuntu and updated paho-mqtt as well. I’ll run some tests later today but it is working fine as a background job with a &.

I don’t have any other device with the same IP and I started a ping session from the Ninja to the MQTT server before I had run the script meaning that there is connectivity between both devices and it is still showing the same error. I’ll let you know how it will behavior after the upgrade.


Sorry for the huge delay, I’ve been distracted by a bunch of other projects. I’m still having problems with my OpenHab config, I’m pretty sure it’s my items file that’s wrong.

Here’s my default.items:

Group All
Group mainFloor (All)
Switch Doorbell "Doorbell" (All) <bell>	{mqtt="<[mosquitto:ninjaCape/input/11:state:ON:010001000101010101010011],
Switch smoke_1 "Lower Smoke Detector" (All) {mqtt="<[mosquitto:ninjaCape/input/11:state:ON:010101000101000100110000],
Group frontroom "Front Room" (mainFloor)
Switch TV_Socket "TV Socket" (frontroom) {mqtt="<[mosquitto:ninjaCape/input/11:state:ON:000101010101010001010101],
Color NinjaEyes (frontroom) {mqtt="<[mosquitto:ninjaCape/input/1007:state:JS(ninjaEyesColor.js)],

Color StatusLight (frontroom) {mqtt="<[mosquitto:ninjaCape/input/999:state:JS(ninjaEyesColor.js)],
Group kitchen "Kitchen" (mainFloor)
Switch DW_Socket "Dishwasher Socket" (kitchen) {mqtt="<[mosquitto:ninjaCape/input/11:state:ON:000101010001010101010101],

And my starter default.sitemap:

sitemap default label="Main Menu"
	Frame label="TV" {
		Switch item=TV_Socket
		Switch item=DW_Socket
	Frame label="Ninja" {
		Colorpicker item=NinjaEyes

Hi Perrin,

After the update/upgrade it is stable running as a background job (&) but still not working when I start it as a service. I’ll try to troubleshoot it more this weekend.


great, thanks. I didn’t write the service config file, and tbh I don’t know a lot about init.d, so it’ll be a learning experience for me too!

If it works running by hand but not as a service the problem is almost certainly one of permissions. When you are running it by hand I assume you are running it as the root user. But when it starts as a service (assuming you installed using apt-get) it runs as the openhab user. So you have to give openhab access to read and write to all the things it needs access to.

For example, a common problem with z-wave is that the openhab user needs to be added to the dialout group so it can read and write to /dev/ttyUSB0 (or whereever the z-wave controller appears). Another problem might be openhab doesn’t have permission to write to /var/log/openhab or read from /etc/openhab.

On a Raspberry Pi one problem can be that you need to be root to access the GPIO pins. If this is the case, you may need to update the openhab script in /etc/init.d so it starts with the root user and group instead.

1 Like

Hi rlkoshak,

Thanks for your reply. I’m running OpenHAB in a different RPi. The problem is to start the Python script wrote by Perrin. The script starts it as ubuntu user (sudo) so I don’t believe that it’s related to permissions. I’ll write a new start/stop script to test. I’ll let you know the outcome.


@perrin any advice?

From first glance it looks OK to me.

What’s not working exactly?
Can you see the inputs and outputs using mosquitto_sub -t “#” ?

if you monitor the openhab events.log do you see the items changing state or receiving commands?

No errors in openhab.log related to mqtt?

1 Like

Sorry for beeing slow, but can I install openhab on the sphere and be able to control zigbee/zwave devices using the spheres hardware?