Contents
FHEM reads the presence 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. Afterward, presence (on/off), zone, distance, moving/stationary, brightness, temperature, humidity, air pressure and availability are available as readings. Siren, LED, alert and timeout can be switched from FHEM. The guide applies equally to KL LD and KL.
<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.
Prerequisites
- KL LD presence sensor with firmware 1.1.3 or KL with firmware 3.2.3, on Wi-Fi
- FHEM 6.x with the built-in modules
MQTT2_CLIENTandMQTT2_DEVICE(tested with FHEM 6.5). No further Perl packages are required. - an MQTT broker on the network, e.g. Mosquitto (tested with 2.0.22)
Setting up the sensor
In the sensor's web interface, enter the following under Connections:
| Field | Value |
|---|---|
| Connection type | MQTT Broker |
| Server | Name or IP address of the broker |
| Port | 1883 |
| Login / Password | only if the broker requires them |
| Client id | unique for each device, only letters, digits, _
|
| Main topic | freely selectable, only letters, digits, _
|
| 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). Enter 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), click Raw definition at the very bottom, 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. 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) }\ stat/$DEVICETOPIC/[^/]+:.* { +{ lc(($TOPIC =~ m,([^/]+)$,)[0]) => ($EVENT eq "True" ? "on" : $EVENT eq "False" ? "off" : $EVENT) } } attr Praesenzmelder setList update:noArg cmnd/$DEVICETOPIC/UPDATE 1\ siren:on,off cmnd/$DEVICETOPIC/SIREN $EVTPART1\ leds:on,off cmnd/$DEVICETOPIC/LEDS $EVTPART1\ buzzer:0,1,2 cmnd/$DEVICETOPIC/BUZZER $EVTPART1\ detection_timeout:slider,5,5,300 cmnd/$DEVICETOPIC/DETECTION_TIMEOUT $EVTPART1 attr Praesenzmelder stateFormat { ReadingsVal($name,"LWT","") ne "Online" ? "nicht erreichbar" : ReadingsVal($name,"presence","") eq "on" ? "belegt (".ReadingsVal($name,"zone","-").")" : "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.
-
Save: at the top left Save config.
The most important readings:
| Reading | Meaning |
|---|---|
presence |
Person detected (moving or stationary): on / off
|
zone |
Name of the zone the person is in. - = none |
detection_dist |
Distance of the detected person in cm. 0 = nobody there |
moving, stationary
|
moving or stationary target: on / off
|
moving_target_dist, stationary_target_dist
|
Distance of the moving or stationary target in cm |
brightness |
Brightness in lx |
temperature, humidity, pressure
|
°C, %, hPa |
siren |
Siren sounding: on / off
|
buzzer |
Alert: 0 off, 1 on presence, 2 without presence |
detection_timeout |
Timeout in s |
leds |
LED switched on: on / off
|
LWT |
Online / Offline
|
The 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. Brightness, temperature, humidity and air pressure come only with the JSON, i.e. every Publish interval (factory value 5 min) or on set … update. The device status shows belegt (<Zone>), frei or nicht erreichbar.
Quick test
On the device Praesenzmelder, execute set … update: after a few seconds presence, zone, brightness and temperature carry the current time. Walk back and forth in front of the sensor: presence changes 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 siren onoroffswitches the siren,leds on/offthe LED,buzzer 0/1/2the alert,detection_timeout 60the timeout in s. The sensor reports the accepted value back. It discards an invalid one without a message (timeout KL LD5…300, KL0…300). For the KL, replaceslider,5,5,300in the block withslider,0,5,300if you need0. -
Light on presence (set the next line first, otherwise the notify triggers again with every status 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. You set the delay when switching off on the sensor with the timeout, not in FHEM. -
Playing a sound and range (KL): Append a
\to the linedetection_timeout:…and add below it:play:0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,STOP cmnd/$DEVICETOPIC/PLAY $EVTPART1\ max_moving_gates:slider,1,1,8 cmnd/$DEVICETOPIC/MAX_MOVING_GATES $EVTPART1\ max_stationary_gates:slider,1,1,8 cmnd/$DEVICETOPIC/MAX_STATIONARY_GATES $EVTPART1The two
max_…lines exist only on the KL. On the KL LD, leave them out (and the\at the end of theplayline). The sound numbers are listed in the user manual. -
Chart:
define FileLog_Praesenzmelder FileLog ./log/Praesenzmelder-%Y.log Praesenzmelder:(presence|zone|brightness|temperature):.*logs the values. From this, Create SVG plot on the FileLog generates the chart. Do not include the distances andmovingin the log (see Limitations). -
Availability: The status shows
nicht erreichbaras soon as the broker reports the sensor asOffline. The corresponding reading is calledLWT.
Limitations
- 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, with Home Assistant switched on at the sensor more than a hundred readings, without controls. The Tasmota templates (attrTemplate tasmota_…) do not fit despite similar topic names. - As long as someone is moving, the distances change several times per second. Every change is an event in FHEM. For automations, use
presenceorzone, and log only selected readings. - Older firmware (KL LD up to 1.1.2, KL up to 3.2.2) sends presence only on a change: after a restart of the sensor, the old value may remain until the first change. In that case, update the firmware.
- 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 the KL LD with firmware 1.1.3.