How to create timers in rules

This is an international forum and therefore posts need to be in English. Please repost your question in English, please.
Also please say what you want to achieve. And post your configuration and the items definitions relevant to your question.
Thanks

I have been searching for timer related guidance, so rather than to open a new thread maybe I can jump on the back of this one…

I’m a relatively new OH user, I have been slowly and methodically crunching through each step. Now i have my Raspberry Pi server set up and running OH2 in home (a), while my automated house will be in home (b) and currently i’m sitting in another country entirely from a & b testing out the system.

I need to add a rule to OH that will change one of my the switch states after 10 seconds after the switch is turned on from either the actual switch or from OH or from Alexa. (the light circuit is physically wired up to an electrical relay and that relay is controlled from 3 different mechanical spring loaded "on/off"switches and it only requires a pulse to activate it and not permanent “on” signal).

So would it be possible to add this statement only in the MQTT statement in the default.items file or does it require a rule entry? ({ mqtt=">[broker:cmnd/kitchen/power1:command:ON:ON],>[broker:cmnd/kitchen/power1:command:OFF:OFF]" })

Next question that I will combine in this single post if that is OK:
So i have here with me two sonoff light switches T1 and T2 Ithink, one is a single and the other is a double touch switch. I have flashed them both with TASMOTA and connected them to my Raspberry Pi OH in Home (a). I can switch them on and off from OH interface on PC and mobile, everything looked good…until i try to manually activate the switches, they turn on but OH switch state does not change. My “default.items” entry for the MQTT part of the command is like this:

{ mqtt=">[broker:cmnd/kitchen/power1:command:ON:ON],>[broker:cmnd/kitchen/power1:command:OFF:OFF]" }

I think that i need to add to the end of this something like ,<[broker:cmnd/kitchen/power1:state1:state1:default]"}
but that does not seem to work and I have tried many different iterations of that statement.

Please could someone offer their expert advice on both of the above issues that I’m having.

Thank you in advance
Chris.

{ mqtt=">[broker:cmnd/kitchen/power1:command:*:default] } will be enough in your case

The syntax for inbound mqtt is slightly different than the outbound

{ mqtt=">[broker:cmnd/kitchen/power1:command:*:default],<[broker:stat/kitchen/power1:state1:default]", autoupdate="false" }

Hi and thank you for such a fast reply, however the first rule that you posted does exactly the same as my current statement to turn on or off the switch, its just shorter. I was looking for a statement that introduces the 10 second delay before switching off the switch again.

for the second part of switch state feedback from OH i copied and pasted in your final MQTT statement but it then breaks my UI and the icons are corrupted and the switch no longer functions in OH.

Would you have any other suggestions that I could try.

thank you for your assistance.

You need to install the expire binding and then do:
{ mqtt=">[broker:cmnd/kitchen/power1:command:*:default]", expire="10s,command=OFF" }

Second one, sorry, typo:

{ mqtt=">[broker:cmnd/kitchen/power1:command:*:default],<[broker:stat/kitchen/power1:state:default]", autoupdate="false" }

Thank you once again for your assistance, so I have installed the EXPIRE binding and that switch now works as required, switching off again withing the time limit set. How would I be able to leave the OH switch status to show “ON” even after the EXPIRE binding has reversed the setting to turn off the switch? (remember that although the command has turned off the switch, in reality the light is on but we just stopped providing a feed to the electrical relay that only needs a pulse for on, and another pulse for off. SO once the light is physically turned on, to turn it off I need to repeat the process of sending an “on” command again using the EXPIRE function but this time the OH switch state need to change to show the switch as “off”.

I’m still having some syntax issue with the second statement with the “<” feedback statement. As soon as I add that to the command in my “default.item” file the next line down switch item description turns green (which from previous experience means that there is something not quite correct with the preceding command line. Any other suggestions about that?

Thank you once again for your assistance.

Can you publish the items is full, please?

Yes of course, but how is the best way to paste it here so that it does not take up lists of space? I remember before I should use “<“code”>” or something like that but I don’t fully remember

Use the code fences (buttons just above the reply box) only paste the relevant items

Lets try this, here are a few items from my default.items file, you see the Entrance Light is the ne with the EXPIRE delay, and the Kitchen Downlights is the one that I have been trying to edit with your suggestions to get things working before I would then edit all the other mqtt command lines to provide STATUS feed back from the device to OH.

This is without trying the feed back entry, so with these commands everything works from OH to the device, but if the device switch is activated then it does not change the state in OH:

Switch   GF_Entryway_Light2                  "Entrance Ceiling"            <light>            (GF_Entryway, gLights, gLight2)                [ "Lighting" ] { mqtt=">[broker:cmnd/kitchen/power2:command:*:default]", expire="3s,command=OFF" }
Switch   GF_Kitchen_Light3                   "Kitchen Downlights"          <light>            (GF_Kitchen, gLights, gLight3)                 [ "Lighting" ] { mqtt=">[broker:cmnd/kitchen/power1:command:*:default]" }

THis is the entry that I made following your advise for the mqtt feedback part, but like this the next line item is highlighted in green indicating a problem with the previous line syntax:

Switch   GF_Entryway_Light2                  "Entrance Ceiling"            <light>            (GF_Entryway, gLights, gLight2)                [ "Lighting" ] { mqtt=">[broker:cmnd/kitchen/power2:command:*:default]", expire="3s,command=OFF" }
Switch   GF_Kitchen_Light3                   "Kitchen Downlights"          <light>            (GF_Kitchen, gLights, gLight3)                 [ "Lighting" ] { mqtt=">[broker:cmnd/kitchen/power1:command:*:default],<[broker:stat/kitchen/power1:state:default]", autoupdate="false" }

Sorry but I can’t see the binding configuration

please check again as I edited the post once i realised the mqtt binding part had not been copied over.

Also to add that with the binding shown in the second code fence above, all switche ICONs (light bulb) from the Kitchen down the list no longer show correct OH status but the one switch above and before the modified binding work as expected nd the icon is changes with switch activation.

Thank you again.

I can’t see any problems with you item definition…
Comment it out and then type it again. DON’T copy and paste it, you may have some hidden characters the OH don’t like

OK thank you for all your help, I’m at work now so will try something different tonight…I’m thinking that the local router that I have connected the switches to might be blocking the STAT traffic…a long shot but I will try by registering them on to another network and re test.

Thank you.
Regards Chris.

Have you got an MQTT tool like mqtt.fx or mqtttspy?
Use that to see it the messages are broadcasted.
I don’t think it’s your router

yes I’m using mqtt.fx…I don’t remember if i saw the state change in mqtt.fx when i manually changed the actual switch state…will test again tonight.

I have to figure out why the syntax is causing issues for the next line items…I will try typing out the whole command manually to test your suggestion of possible hidden characters from copy n paste.

Thanx again :slight_smile:

Hi vzorglub,

I’m back again! what a few nights of frustration but I think that i have figured it out…or at least lets say that it now works to report back the switch operations in OH2!

I have a question for you as I could not find much in the way of explanation for the “autoupdate” part of the statement, what does i do and what is the difference between having it as “false” or “true”?

So here are the two statements that work in my “default.items”:

Switch   GF_Entryway_Light2                  "Entrance Ceiling"            <light>            (GF_Entryway, gLights, gLight2)                  [ "Lighting"   ]   { mqtt=">[broker:cmnd/kitchen/power2:command:*:default], <[broker:stat/kitchen/POWER2:state:default]", expire="3s,command=OFF" }
Switch   GF_Kitchen_Light3                   "Kitchen Downlights"          <light>            (GF_Kitchen, gLights, gLight3)                   [ "Lighting"   ]   { mqtt=">[broker:cmnd/kitchen/power1:command:*:default], <[broker:stat/kitchen/POWER1:state:default]", autoupdate="true" }

Thank you for guiding me :):grinning:

PS. I did have the question of after the EXPIRE binding has switched off the switch, how could i have OH still display the status of the UI switch and icon still remain as ON?

For your second question, the answer is: You can’t. We will have to create another item like a toggle that goes on and off each time the item GF_Entryway_Light2 receives a command ON and you will need some rules to deals with that.

So Items:

// Updated Item
// Only sends command OFF to the relay and expire OFF after 3s
Switch   GF_Entryway_Light2                  "Entrance Ceiling"            <light>            (GF_Entryway, gLights, gLight2)                  [ "Lighting"   ]   { mqtt=">[broker:cmnd/kitchen/power2:command:OFF:OFF]", expire="3s,command=OFF" }

//This one doesn't change
Switch   GF_Kitchen_Light3                   "Kitchen Downlights"          <light>            (GF_Kitchen, gLights, gLight3)                   [ "Lighting"   ]   { mqtt=">[broker:cmnd/kitchen/power1:command:*:default], <[broker:stat/kitchen/POWER1:state:default]", autoupdate="true" }

// New Item
// Both command ON and OFF send ON to the relay
Switch   GF_Entryway_Light2_Toggle "Entrance Ceiling" <light>  (GF_Entryway, gLights, gLight2) [ "Lighting" ] { mqtt=">[broker:cmnd/kitchen/power2:command:ON:ON], >[broker:cmnd/kitchen/power2:command:OFF:ON]" }

Rules:

rule "Entryway light toggles"
when
    // Every time the switch is actionned on the sitemap a command is received
    // It doesn't matter if it's ON or OFF because the command triggered the relay ON
    // as per the binding
    Item GF_Entryway_Light2_Toggle received command
then
    GF_Entryway_Light2.sendCommand(ON) // After 3s an OFF command will be sent by the expire binding and trigger the relay OFF
end

In your sitemap, replace the item GF_Entryway_Light2 by the item GF_Entryway_Light2_Toggle

What happens is that every time you action that toggle on the sitemap, an ON command is sent to the relay and a timer is started in another item for 3s. When that timer expires, the second item (Timer one) will send a off command to the relay. But your first item remains ON or OFF.

Regards

That’s awesome, you are so helpful, thank you!

that works 90% for my situation and from OH UI everything works as you suggest, however when I try to operate the physical switch the UI does not correctly report the status. I played around for the past hour trying to add in the feedback statement to the GF_Entryway_Light2_Toggle command but have not got it correct yet. I will have to continue tomorrow night to try to get that part perfected.

Switch   GF_Entryway_Light2_Toggle "Entrance Ceiling" <light>  (GF_Entryway, gLights, gLight2) [ "Lighting" ] { mqtt=">[broker:cmnd/kitchen/power2:command:ON:ON], >[broker:cmnd/kitchen/power2:command:OFF:ON], <[broker:stat/kitchen/power2:state:default]", autoupdate="false" }

Good luck