[Solved] Send command to MQTT topic after defined time period

Hi,

i am just starting with OH3 and i am completely lost with a simple task and although i have been trying to find a solution on my own, i failed.

My current setup is pretty simple:

I have two magnetic valves that are connected to a relay and MQTT server running on a NodeMCU / RaspberryPi. In OH3 (running in a docker container on a separate home server) i have added the binding, things, items and added a switch to Pages through the PaperUI. This is working and i can turn on / off the valves by tapping on the switch.

What i was unable to figure out is how i can say that if the magnetic valve receives the mqtt command “on” a timer is started that turns the valve off after e.g. 30 minutes, meaning the mqtt command “off” is sent. I tried to add meta data / expiration timer on the item itself, but as i learned this is not working as the status is updated periodically and therefore the expiration timer is always restarted.

My next idea would have been to create a rule, but there i no dedicated action for such things. So i thought of adding a script as a rule action but also failed to define something with the Blockly editor.

Then i started to search and i have found some rules for OH2.x but i am not sure how to transform such manually created rules to the OH3 code syntax.

Eventually somebody could point me to the right direction i that i should investigate further… is my usecase even configurable over the PaperUI?

Thanks in advance.

There is no PaperUI in OH3. It is mainUI…

I just use expire and it works well.

You can just create a rule in the mainUI to turn on the item whenever you want and then the expire will take over.

What firmware are you running on your NodeMCU? If it’s flashed with Tasmota you can use PulseTime.

Doing it this way will ensure that the valve is switched off without having to rely on openHAB.

Thank you for your replies.

i meant MainUI … i just messed it up due to too much information, sorry. The nodemcu is sending its status to the topic every other minute, so expire timer is not triggered. As said i already tried that. Thanks for the conversion link, i will check it out.

I am note sure with what firmware the NodeMCU was shipped, but i definitely haven’t thought of flashing Tasmota to it. Another thing i can check out, thanks

As should be clear by now from T’s suggestion, something like this is often best implemented on the device itself. It’s a NodeMCU so you should have full control over its behavior. For example, why is it sending its status every two minutes? If the status isn’t changing why is it publishing these updates. That’s somewhat of an anti-pattern for MQTT. It should be using a retained message and only publishing when the status changes.

But for the original question about rules, only very simple things can be done using just the UI. Slightly more complex things can be done with a Script Action using Blockly. But if you create a Script Action using Rules DSL or ECMAScript (AKA JavaScript) there is very little you can not do. And for that very little bit you can use text based rules files instead.

Everything text based that applied to openHAB 2.x also applies to OH 3. There are a few minor changes to certain function calls and the like but beyond that nothing’s changed. So if you see a Rule example you should be able top put that into a .rules file in /etc/openhab/rules, as documented at Rules | openHAB.

If you want to stick with defining the rules in MainUI then often the only thing needed is to copy the contents between the then and end into a Script Action.

Where this approach fails is when the code depends on variables defined outside of that then and end. In that case you are best to stick to .rules files. There is a partial way around that when using JavaScript.

Ultimately what you need is a Timer. When the Item receives an ON command the script will create a Timer for 30 minutes to turn it off. But timers get complicated. Should the timer ever be cancelled? 30 minutes is a long time, what should happen if OH is restarted before the OFF command is sent? Dealing with valves where flooding and lots of money could be the result if OH crashed is one reason why something like this is better implemented at the device and not external to the device.

2 Likes

Hi,

thank you for your thoughts and information. Indeed it is best if the NodeMCU itself is closing the valves. I have now flashed Tasmota and am controlling the timer with pulseaudio directly on the node.

And again a thank you for the rules explanation, lot of usefull information that will help me when i extend my installation.

1 Like