Setting up Esp8266 PIR sensor in openhab2 with MQTT (im stumped)

Im trying to setup a PIR sensor(motion) in openhab2 item and sitemap .I have really only used esp8266’s with switches and relays before which all work fine .

I am using ESPeasy with my esp8266’s and pretty confident i have it correct on that side of things as in the espeasy log i am seeing it motion sensor going publishing 1.00 or 0.00 . I cant get it to show anything in openhab2 though .

Btw i am using mosquitto and its on the same raspberrypi as the openhab server .The name of the ESP8266 name is MTsensor ,device name is pir

My item file looks like this

Number sensors_frontdoor_pir "Front Door Motion [MAP(esp-pir.map):%s]" <motion> (all,sensors,contacts) {mqtt="<[mosquitto:/MTsensor/pir/contact:state:default]"}

In transform file which is the esp-pir.map file i have
0.00=still
1.00=motion

Sitemap i have:

Frame label="Sensor Module"{
                                Group item=sensors label="Front Door"
    }

No value is being shown in the GUI though.Any help greatly appreciated .I also want the state of the motion sensor to turn on a set of mqtt lights as well but havent got to that yet

Is this the first time you have used MQTT in OH? Have you set up the MQTT binding config details properly?

You have ‘contact’ written with small ‘c’ in your OH item, on espeasy you write it with capital ‘C’

Also, in your items file the MQTT topic has a leading slash.

Although not incorrect as such, it is not recommended as this implies an empty “root topic”, something that may not play well with all MQTT implementations.

More importantly, your ESP8266 client must use the same empty “root topic” for this to work. I am not familiary with ESPeasy, but if this is some kind of framework that handles the MQTT parts for you, then it is very likely that it is not using an empty “root topic”.

Try to modify you items file to the following (leading slash removed):

{mqtt="<[mosquitto:MTsensor/pir/contact:state:default]"}

And remove the leading slash in espeasy :slight_smile:
(But I’ve had it working with leading slashes)

It is ceratinly possible to make it work with a leading slash, although not recommended.

My suspicion, however, was that ESPeasy did not use a leading slash - and then it would not work with the items file above. If you can tweak ESPeasy to use (or not use) a leading slash, then the use of a leading slash may not be a problem.

I’ve learned that it’s always good to also look at the MQTT broker when dealing with MQTT. I can recommend http://mqttfx.jfx4ee.org/

The default template in ESPEasy includes a leading slash, although one can change it to not use a leading slash:

I have many items in openhab with the same setup (espeasy mqtt) but are light switches and they all work great . Its just the Pir and temp sensors i am having trouble with. The leading slash has been working on all my other items so i dont think thats the problem . It was just the default way to have the leading slash as posted above. I will start removing them in all my mqtt items though. I have like 10 to change and also has to be changed on the espeasy firmware for each one.

My GUI just looks like this with no sensor state or temp/humidy states.

I am changing contact to a capitol now and let you know if it changes anything . I also have a prob with temp sensor too though which isnt a contact

Changing to capitol C didnt do anything. I just tried MQTT.fx for the first time and tried subscribing to the pir sensor and it says sucessful but doesnt really show any data. Its my first time using mqttFX though .It says

addPublishTopic : /MTsensor/pir/contact
2017-03-01 00:36:33,010 INFO — MqttFX ClientModel : sucessfully subscribed to topic /MTsensor/pir/contact (QoS 0)

Just checked one of my mqtt light switches in mqtt.fx by subscribing to it and can see the switch change states when i turn the light on and off . So it seems like the pir sensor is connecting but not publishing any values to openhab as it just stays on 0

I don’t think your map is correct. A contact uses “OPEN/CLOSED” as states, so it should look like:

0.00=CLOSED
1.00=OPEN

or vica versa.

You may find out while subscribing to the topic # in your MQTT.fx.

Your item indeed needs to have the capital C in contact, the default template in ESPEasy uses:

Okay, I realized your are triggering on numbers, so forget this part …

Yea i dont think thats it . I have a DHT11 temp sensor and the pir sensor on the same esp8266 and its not sending the values of either to openhab .Not sure what im doing wrong . The sensors are both working as in the espeasy config it is displaying values for both sensors showing they are working fine . That leaves the problem being in the mqtt or items setup

The whole code in my items file looks like this

Number period_livingroom_temp "Living Room Temperature Period"
Number period_livingroom_hum "Living Room Humidity Period"
Number sensors_livingroom_temp "Living Room Temperature[%.0f °F]" <temp_icon> (all,sensors,temp,chart_livingroom_temp,chart_all_temp) {mqtt="<[mosquitto:/mtsensor/dht/temperature:state:default]"}
Number sensors_livingroom_hum "Living Room Humidity[%.0f %%]" <hum_icon> (all,sensors,hum,chart_livingroom_hum,chart_all_hum) {mqtt="<[mosquitto:/mtsensor/dht/humidity:state:default]"}
Number sensors_frontdoor_pir "Front Door Motion [MAP(esp-pir.map):%s]" <motion> (all,sensors,contacts) {mqtt=">[mosquitto:/mtsensor/pir/Contact:state:default]"}

Your items setup looks fine, I guess you have subscribed to the wrong topic.

Easiest way to find out on what topic your sensor data is coming in:
use mosquitto_sub or any other mqtt client (mqtt.fx, mqtt-spy) and subscribe to the wildcard topic #

example: mosquitto_sub -d -v -t /#

You will get something like

Client mosqsub/19305-raspberry received PUBLISH (d0, q0, r1, m0, '/esp8266three/temperature/water', ... (3 bytes))
/esp8266three/temperature/water 5.5

Take the complete folder structure (in my case /esp8266three/temperature/water) and use that in your item setup. Don’t forget any leading or trailing slashes.

iv done that as i posted above and its successful but getting no values. Im 99.9% sure i am subscribing to the correct topic which is MTsensor/dht/Temperature in my case or for the motion sensor MTsensor/pir/Contact

Sorry, did not take a look at the top posts.
If your picture from above is still your current setup, your topic is wrong:
you did not use the upper/lower case:

should be /MTsensor/dht/Temperature

Also one more hint for the mqtt.fx client: after you subscribed to the wildcard topic you will see in the lower right corner the messages coming in:

THAT is definitely the topic you have to subscribe to … :slight_smile:

Sorry. i didnt mention i changed it up from the original topic .My last post is now correct . I removed the / and changed it to capitols in my item file to match the esp.

Ok so a little update. I got the temp sensor working .Not sure what i did but i think it had something to do with the group it was in and the pir sensor in the same group. Is that possible?

Im still having a problem with the pir sensor .I have narrowed down the problem though. It is working testing it in Mqttfx and im receiving a 1 when theres motion and 0 when no motion. So the MQTT is working fine but im still not getting anything in openhab gui. The problem is in openhab for sure either in the items , sitemap or map file .

Is the map file i created in the tranformation folder correct ? im not sure how this works exactly. I would think that even if im not receiving a value shouldnt it still show as still or motion in the GUI ? In the GUI the value area is just blank.My map file below name of file under transformation is esp-pir.map

0=still
1=motion

Heres some screenshots of erything (mqttfx test ,GUI and items)

Number sensors_frontdoor_pir "Front Door Motion [MAP(esp-pir.map):%s]" <motion> (Gcontacts) {mqtt=">[mosquitto:mtsensor/pir/Contact:state:default]"}

Should be only having an impact when you try to trigger on that group. For just assigning incoming values to the item it should not have an impact.

Yes, those .map files belong to the /transform folder. AND you need to install the map transformation service!

Here is a nice explanation:

I would recommend using a Switch (ON/OFF) or Contact (OPEN/CLOSED) item and replacing your Number item with one of that options (and using mapping to convert the numbers to the states a switch or contact item is able to understand).