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

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).

Should my item line have the map file at the end ?

Right now is

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

Should it be this?

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

Also can someone explain what is the difference between %d and %s ?

I was also reading in the openhab2 documentation and came across this below .Should there be 2 seperate lines in the item file?

No. In that example they do show you just the difference between a transformed item and a default item.

%s: use item value as string
%d: use item value as integer

So i tried using just %d instead of the map to see if i can get it to display just a 0 or 1 but this isnt working either .Its just displaying - I cant see why this is not working .The Mqtt is definitly working as per my mqttfx test and im sure the topic is correct. Maybe a glitch in openhab2? The temp sensor is also on the same esp device and that is working fine. Everything is working except for displaying a value in openhab . I did install the map transform in paperui as well. It just seems like openhab is not accepting a 0 or 1 .

I would like to get it working with the current 0 or 1 values but if not i may have to change it to a contact item. If i do how do i transform a 0 or 1 into a open / closed ? Do i just change number to contact and in map write
0=closed
1=open

Also i have a idea that openhab2 is looking for a decimal value like 0.00/1.00 instead of what the sensor is putting out which is 0/1 .Would it be possible to change the value in espeasy so it sends the decimal values instead of 0 or 1? Maybe a rule or something?

BTW i appreciate you taking the time to help

Oh man ! i found the problem and it was super simple. I put a publish arrow instead of a subscribe by accident .It was > . Im surprised nobody here caught it since i posted the item line a bunch of times. lol All good now.

Now to make another mqtt lightswitch turn on when there is motion .I do this in rules correct?

Thanks .Thats what i had in mind. My only question is with the motion sensor im using that sends a value of 0 or 1 and then is transformed to still or motion , what do i use in the rule ? Can i use the tranformed variable of still or motion or do i use 0 or 1 ? I cant seem to find anything about this in the openhab2 documentation

You need to use the map transformation in the channel definition, not in the label, then your are able to trigger on that state in rules:

item:

Switch MQTT_Item "Test MQTT item trigger" {mqtt="<[mosquitto:/esp8266two/test/switch:state:MAP(test.map)],<[mosquitto:/esp8266two/test/switch:command:ON:1.00],<[mosquitto:/esp8266two/test/switch:command:OFF:0.00]"}

map:

0.00=OFF
1.00=ON
-=undefiniert
NULL=undefiniert

rule:

rule "test"
when   
		Item MQTT_Item changed from OFF to ON
then
		//do stuff