Platform information::
- Hardware: Raspberry PI 3
- OS: raspbian
- Java Runtime Environment: 1.8.0_191
- openHAB version: 2.4.0
- use MQTT embedded (but had the same issue with Mosquito days ago with openhab 2.1)
Context:
I would like to turn on bathroom lights (sonoff) when someone enter (philips hue motion sensor)
I wrote a small rule for that:
rule "Movement detected in bathroom"
when
Item BathroomPresence changed
then
// presence detected
if(BathroomPresence.state == ON) {
if(BathroomPresenceTimer.state != ON){
logInfo("bahtroom.presence", "Movement detected in bathroom (timer were not started so start it)");
BathroomPresenceTimer.sendCommand(ON);
BathroomPresenceTimer.postUpdate(ON); // start the timer if not already active
} else {
logInfo("bathroom.presence", "Movement detected in bathroom (timer already started)");
}
}
// probably left the room
else if(BathroomPresence.state == OFF){
if(BathroomPresenceTimer.state != OFF){
logInfo("bathroom.presence", "No more mouvement detected in bathroom but timer active");
} else {
logInfo("bathroom.presence", "No more mouvement detected in bathroom and timer inactive");
}
}
end
rule "Bathroom presence timer started"
when
Item BathroomPresenceTimer received command ON
then
logInfo("bathroom.presence", "movement detected turn the light on and start a timer for x minutes");
BathroomLight.postUpdate(ON)
BathroomLight.sendCommand(ON)
BathroomMirrorLight.postUpdate(ON)
BathroomMirrorLight.sendCommand(ON)
if(now.getHourOfDay() >= 6 && now.getHourOfDay() < 22){
logInfo("bathroom.presence", "It is " + now.getHourOfDay() + "h, musicmaestro!");
if(now.getHourOfDay() >= 9 && now.getHourOfDay() < 20){
BathroomSonosVolume.sendCommand(new PercentType(25));
} else {
BathroomSonosVolume.sendCommand(new PercentType(10));
}
sendCommand(BathroomSonosRadioTuneIn, "2109"); // 2109 funradio
if(now.getHourOfDay() >= 9 && now.getHourOfDay() < 20){
BathroomSonosVolume.sendCommand(new PercentType(25));
} else {
BathroomSonosVolume.sendCommand(new PercentType(10));
}
} else {
logInfo("bathroom.presence", "no music it's too late");
}
end
Unfortunately it takes up to 5 seconds sometimes just to turn on the lights.
I investigated the following points:
1. Maybe the sonoff reacts too late to MQTT bus ?
No it’s very fast
I connected MQTT.fx (mqtt client) directly to the mqtt bus and tried to publish manually some messages to turn the lights on (sonoff) → it reacts immediatly. This exclude the sonoff potential issue.
2. Rule engine or presence detection is slow ?
It looks like it’s not the problem based on following tests:
- I tried to trigger OpenHab UI switch to see if it’s long to publish on MQTT and… it is unfortunately quite slow 2-3 seconds.
** 3. Is it related to raspberry pi resources ? **
I won’t say it’s not but if I run htop most of the time cpus are not all working and memory: 488/927
Question: Can we improve this writing delay ?
I would like to increase this time so we reach max 1 second between the click on the UI and the MQTT publish. It should drastically improve the user experience.
Any help is really appreciated thank you in advance