Contents
FHEM reads the sensor via MQTT. You connect FHEM to the broker the sensor is attached to (MQTT2_CLIENT), and create the sensor with the block below as an
MQTT2_DEVICE. The block creates readings for presence (on/off), number of people, one reading for each detection zone, availability, and temperature, humidity, air pressure and brightness. The zone readings arise from the sensor's messages, without you having to enter the zone names. Timeout, LED, alert, siren and playing a sound are included as set commands.
<Hauptthema> stands in this guide for the value entered on the sensor under
Connections → Main topic. It must be entered in FHEM in exactly one place, see step 2.
<ZONE1> … <ZONE4> stand for the name of a detection zone, exactly as it appears in the topic: in capital letters, e.g. FLUR for a zone named “Flur”.
Prerequisites
- KL MZ multi-zone presence sensor with firmware 1.4.3 or newer, on Wi-Fi, zones drawn
- FHEM 6.x with the built-in modules
MQTT2_CLIENTandMQTT2_DEVICE. No further Perl packages are required. - an MQTT broker on the network, e.g. Mosquitto.
Setting up the sensor
-
In the sensor's web interface, under Zones → Zone names name the detection zones in use before you set up FHEM: only letters, digits and
_, no spaces, e.g.Flur,Sofa. Do not rename them afterward. The reading in FHEM carries the name. -
Enter under Connections the following:
Field Value Connection type MQTT BrokerServer Name or IP address of the broker Port 1883Login / Password only if the broker requires them Client id unique for each device, only letters, digits and _Main topic freely selectable, only letters, digits and _Home Assistant off
FHEM does not evaluate the Home Assistant messages. Leave the switch off unless another system needs them. All topics and values are listed in the user manual.
Setup
-
Connect FHEM to the broker (skip if there is already an
MQTT2_CLIENTfor this broker). On the FHEM command line:define mqtt MQTT2_CLIENT <Adresse des Brokers>:1883Afterward, the device
mqttmust showopened. If the broker requires a login:attr mqtt username <Benutzer>andset mqtt password <Passwort>. -
Create the sensor. Open any device (e.g.
mqtt), scroll to the very bottom and click Raw definition, replace the contents of the text field with the following block and click Execute commands. Beforehand, in the second line, replace<Hauptthema>with your main topic (only there). If your broker device is not calledmqtt, change the last two lines. The device namePraesenzmeldercan be chosen freely, but then change it in all lines.define Praesenzmelder MQTT2_DEVICE attr Praesenzmelder devicetopic <Hauptthema> attr Praesenzmelder readingList tele/$DEVICETOPIC/LWT:.* LWT\ tele/$DEVICETOPIC/STATE:.* { json2nameValue($EVENT =~ s/:\s*true\b/: "on"/gr =~ s/:\s*false\b/: "off"/gr, "", {}, ".", "^(zone_names|zone_delay|sounds)_[0-9]") }\ stat/$DEVICETOPIC/[^/]+:.* { +{ lc(($TOPIC =~ m,([^/]+)$,)[0]) => ($EVENT eq "True" ? "on" : $EVENT eq "False" ? "off" : $EVENT) } }\ stat/$DEVICETOPIC/ZONES/.+:.* { $EVENT eq "" ? fhem("deletereading $NAME ".makeReadingName("zones_".lc(($TOPIC =~ m,/ZONES/(.+)$,)[0])), 1) : +{ "zones_".lc(($TOPIC =~ m,/ZONES/(.+)$,)[0]) => ($EVENT eq "True" ? "on" : "off") } } attr Praesenzmelder setList update:noArg cmnd/$DEVICETOPIC/UPDATE 1\ delay:slider,0,1,120 cmnd/$DEVICETOPIC/DELAY $EVTPART1\ leds:on,off cmnd/$DEVICETOPIC/LEDS $EVTPART1\ alert_mode:0,1,2,3,4,5 cmnd/$DEVICETOPIC/ALERT_MODE $EVTPART1\ siren:on,off cmnd/$DEVICETOPIC/SIREN $EVTPART1\ play:0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,STOP cmnd/$DEVICETOPIC/PLAY $EVTPART1 attr Praesenzmelder stateFormat { ReadingsVal($name,"LWT","") ne "Online" ? "nicht erreichbar" : ReadingsVal($name,"presence","") eq "on" ? "belegt" : "frei" } set mqtt disconnect set mqtt connectThe last two lines briefly reconnect FHEM to the broker. Only then does the broker deliver the sensor's latest state to the new device. Otherwise the readings fill up only gradually, with the next change or status message.
-
Save: at the top left Save config.
The most important readings:
| Reading | Meaning |
|---|---|
presence |
someone in the room (with timeout): on / off
|
detection |
Number of people detected, without timeout |
zones_<zone1> … |
someone in this detection zone: on / off
|
temperature, humidity, pressure
|
°C, %, hPa |
brightness |
Brightness in lx |
LWT |
Online / Offline
|
delay |
Room timeout in s |
leds |
LED switched on: on / off
|
alert_mode |
Alert: 0 off, 1 presence, 2 … 5 detection zone 1 … 4 |
siren |
Siren sounding: on / off
|
play |
sound currently playing, -1 = none |
The zone readings are called zones_ plus the zone name in lowercase, e.g.
zones_flur for the topic stat/<Hauptthema>/ZONES/FLUR. Spaces and other special characters become _: The factory name Detection 1 gives
zones_detection_1. For every zone the sensor reports, the block creates the reading by itself. If the sensor reports a zone as unused (empty message, also for the old name after a rename), the block deletes it again.
The remaining individual values under stat/<Hauptthema>/… and the JSON from
tele/<Hauptthema>/STATE end up in the same readings, written in lowercase.
True/False or true/false become on/off. Temperature, humidity, air pressure and brightness come only with the JSON, i.e. every
Publish interval (factory value 5 min) or on set … update. The device status shows belegt, frei or nicht erreichbar.
Quick test
Execute on the device Praesenzmelder set … update: after a few seconds presence, detection, the zones_… readings and
temperature carry the current time. Walk into a detection zone:
presence and the reading of the zone change to on, the status to
belegt. If the list stays empty, the main topic in the line
devicetopic is wrong. Mind upper and lower case.
Optional
-
Switching:
set Praesenzmelder delay 60sets the room timeout in s (0…120),leds on/offswitches the LED,alert_mode 0…5the alert,siren on/offthe siren,play 5plays a sound,play STOPends it. The sound numbers mean:0Standard,1Two-tone horn,2Alarm clock,3Evacuation,4Doorbell,5Gong,6SENVOLON,7Für Elise,8Mountain King,9Ode to Joy,10Lullaby,11Entertainer,12Rhapsody,13Boléro,14Fanfare. The sensor reports the accepted value back and discards an invalid one without a message. -
Light on presence (set the first line first, otherwise the notify triggers again with every message):
attr Praesenzmelder event-on-change-reading .* define n_Praesenz_an notify Praesenzmelder:presence:.on set Flurlicht on define n_Praesenz_aus notify Praesenzmelder:presence:.off set Flurlicht offFlurlichtstands for your device. For a single zone, replacepresencewith the reading of the zone, e.g.zones_flur. The delay when switching off is set on the sensor with the timeout, not in FHEM. -
Chart:
define FileLog_Praesenzmelder FileLog ./log/Praesenzmelder-%Y.log Praesenzmelder:(presence|detection|zones_.*|temperature|brightness):.*logs the values. From this, Create SVG plot on the FileLog generates the chart. -
Availability: The status shows
nicht erreichbar, as soon as the broker reports the sensor asOffline. The corresponding reading is calledLWT.
Limitations
- The block creates the zone readings under the name the sensor sends. After a zone is renamed, the old reading disappears immediately and the new one appears right afterward. Notifies, logs and charts that use the old name must be adjusted by hand.
- FHEM rewrites umlauts in the zone name in the reading name (e.g. to
ue). With names made up only of letters, digits and_you do not have this. - If the sensor cannot read an environmental value, FHEM keeps the last value.
- There is no automatic setup. On the
MQTT2_CLIENT, do not switch onautocreate: otherwise FHEM creates a collective device with all topics of the broker, without controls. The Tasmota templates (attrTemplate tasmota_…) do not fit despite similar topic names. - The commands are sent without acknowledgment and without protection against loss (QoS 0). Whether a value arrived is shown by the reading of the same name.
Verification status
Tested with FHEM 6.5 and sensor firmware 1.4.2.