Using a PIR to "do something"

This may not be the place to ask this, please feel feel to tell me if i’ve “broken the law”

My goal is to turn a group of lights on when a PIR is activated.

My set up is an Adafruit Huzza running Tasmota firmware sending ON and OFF messages to MQTT, but nothing happens when the messages are recieved.

This is my rule

rule "PIR"
    Item HallPIR changed



this is my item

String HallPIR  { mqtt=">[broker:cmnd/sonoff-21/power:command:*:default]" }

The messages are received OK in MQTT but the rules dont seem to process them.

I can’t say much about the setup, but I believe you have a mistake in your rules.

Item HallPIR changed

Should be:

Item HallPIR received update

I don’t think changed is correct here, but received update will trigger any time the value of the item is updated.

See here for an explanation of the difference between received update and received command

Use “<” instead of “>” as you want to act on INcoming messages.
(and use .sendCommand(ON), not “1” or similar)

Even though it is probably very simple I am still trying to get my head round < and >. When you say INcoming do you mean INcoming to openhab or INcoming to the device

IN to openhab. Your line is openHAB config , so it’s obviously from the POV of OH, and you had a “>” line defining how OH would talk to the device but no “<” part. You can have both, “>” AND “<” be part of the item config line.

thanks that makes a bit more sense, but I need to look at it a bit more because it doesn’t want to work for some reason

Ive been trying for a few days to get this working and its getting a bit frustrating.

For this test I just want to set another device on if the PIR is activated, this is what I have got so far

String   HallPIR  { mqtt=">[broker:cmnd/pir1:command:*:default],<[broker:cmnd/pir1:command:*:default]" }
Contact  HallPIRProxy  ( contact ) { mqtt=">[broker:cmnd/pir1:command:*:default],<[broker:cmnd/pir1:command:*:default]" }


var timer1 = null

rule "Handle PIR Events"
    Item HallPIR received update
     if(timer1 === null || timer1.hasTerminated) {
        timer1 = createTimer(now.plusMinutes(5), [ |
          timer1 = null
else {

i am using an Adafruit Huzzah to send MQTT OPEN & CLOSED messages to the MQTT server with topic cmnd/pir1 and they are getting to the MQTT server OK

But I dont think the rules file is processing them (no messages in the events log)

Any ideas?

I am pretty sure your item definitions are not correct. Have you read the documentation here?
(Look at the sections called “Item Configuration for Inbound Messages” and “Item Configuration for Outbound Messages”)

Yes I looked at that, but when i followed an example ithe items file couldnt be parsed properly so went back to my original configuration

That might be so, but I am still pretty sure your curent items are incorrectly configured.

It may be easier to give you some suggestions if you could explain how your PIR device works. It looks a bit funny to send commands towards the PIR device. Normally a PIR device does not accept commands - it will only send status updates whenever it detects motion (or lack thereof; usually determined by a timeout).


The PIR is a HC-SR501, one of the basic ones used a lot in the “Arduino World”

I may (or may not) be doing this the correct way, so i am not averse to (a bit) of criticism. After all i have learnt a lot from you guys

If we take it one step at a time, HallPIRProxy is supposed to be an indication on the OH2 UI as to the state of the PIR. Below is the item file that I now think accepts messages from the PIR and sets the state of HallPIRProxy. I’m not 100% sure what * and default does, according to the docs it is transformation and regex_filter respectively.

Contact   HallPIR  { mqtt="<[broker:cmnd/pir1:state:*:default]" }
Contact  HallPIRProxy  ( contact ) { mqtt=">[broker:cmnd/pir1:state:*:default]" }

Ok, now I understand your setup a bit better. Thanks!

Contact   HallPIR  { mqtt="<[broker:cmnd/pir1:state:*:default]" }

This is looking closer to what I’d expect, but I still it is not entirely correct, :slight_smile:

Tasmota firmware sending ON and OFF messages to MQTT

I am not familiar with Tasmota, but if it is correct that it sends ON and OFF messages, then you need a Switch item and not a Contact item.

I suggest you try the following:

Switch   HallPIR  { mqtt="<[broker:cmnd/pir1:state:default]" }

This will basically take the message received on the MQTT topic and try to use that to post an update to the openHAB item. This means that if your MQTT message is ON (or OFF) and your item is a Switch, then openHAB should be happy.

If your MQTT message is ON (or OFF) and you would like your item to be a Contact (as in your item file) then you would have to use the ‘transformation’ property to convert from “ON” to “OPEN” (and “OFF” to “CLOSED”).

I hope this brings you one step closer to a working solution.

Ps! I still don’t undertand the HallPIRProxy item and why you would like to send a command to a PIR.

The HallPIRProxy, is just supposed to be a visualization on the UI to the state of the PIR, so when the PIR is ON, then the HallPIRProxy displays as ON in the UI. A “Proxy” was suggested in a previous thread and my understanding was it was just an “indication” type of item. I will try your suggestions and get back to you

thanks again

Hi I tried your suggestion but it unfortunately didnt work

Hmm… this is strange.

Time to take a few steps back:

I suggest you do the following:

  1. install some kind of MQTT sniffer (e.g. mqtt-spy :
  2. connect to your MQTT broker and subscribe to “#” (multi-level wildcard topic) to get all messages published to all topics.
  3. activate your PIR and check what is actually sent to the MQTT broker

Please provide information about the exact topic and message that is sent by your PIR device, along with the configuration of openHAB for your broker connection (mqtt,cfg) and I’ll try to help you further.

Would the command mosquitto-t /# show everything in real-time. I’m hoping
you say say no because nothing is actually being displayed. However I have
mqtt.fx on my PC connected to the mosquitto server and can see lots of
topics. I will also try your suggestion but am in a rush at the moment to
get to an appoint with the dreaded accountant.


OK, so i have moved on a little further, the topic i was subscribing to was cmnd/pir1/ the topic i was publishing to was cmnd/pir1 (School boy error i guess)

As i say we have progressed, but now i get an error in openhab log

2017-12-06 10:20:23.529 [WARN ] [] - given new state is NULL, couldn't post update for 'HallPIR'

So i guess we need to be able to handle NULL someway

For clarity here is my items and site map file

Switch   HallPIR  { mqtt="<[broker:cmnd/pir1:state:default]" }
Switch  HallPIRProxy  ( contact ) { mqtt="<[broker:cmnd/pir1:state:*:default]" }
Frame label="Everything" {
        Switch item=HallPIRProxy
        Switch item=HallPIR

First of all, I suggest you get rid of the HallPIRProxy item because it is 1) simply a copy of the HallPIR item (meaning there is no more information to be gained) and 2) incorrectly defined (the same error that you had in the HallPIR item before).

This will not solve your problem, but only serve to focus the search for the root cause of your problem.

Using MQTT.fx towards your broker, you say that you see lot’s of topics, but do you find the topic and message that you are interested in?

For your openHAB item definition to work, you will need to find the following being published to your broker:

topic: cmnd/pir1
message: ON

From the warning message that you receive in the opehab log, it sounds like a message is indeed published to the correct topic, but with the incorrect content (i.e. it is not ON or OFF). Please check this!

Just a generic collection composed of snippets of advice learned the hard way;

  • If you don’t know what a rule is doing (or whether it’s firing at all), add debug

    sendXMPP(“”,“Warning: L2 High Temp. Alert!”)
    pushover(“Warning L2 High Temp. Alert!”, “Samsung-XXX”, 0, “”, “”, “pushover”)
    executeCommandLine("/bin/sh@@-c@@echo date - Warning: L2 High Temp. Alert! >> /var/log/ha.log")

The STDOUT of the OpenHAB JVM can also provide hints, together with openhab.log which generally contains the STDERR. As you most likely know, the above are just three “media types”, you can use email or syslog or anything else you want.

  • Check whether it works if you manually execute part of the process. You can for example send commands and update statusses via XMPP, I personally use that a lot. Try sending an update to your item, and see whether your rule fires.

  • Check the startup logs for any issues regarding the bindings you’re using. Some will spit out a couple of errors and never complain again until the process is restarted, reloaded or the JAR is renamed/moved/replaced.

  • There is a tool called strace on Linux which will show you the executed system calls. Often, this is way more than you wanted to see, so some filtering is often required. I don’t think this is the right place for a discussion on how to use it, but PM me if you can’t figure it out :wink:

This is all rather generic, as I’ve completely avoided MQTT with OpenHAB, but it might help in debugging.



Thanks guys for the replies.

I removed HallPIRProxy and changed the MQTT messages from OPEN/CLOSED to ON/OFF and it all seems to work.