|
Post by camblonie on Dec 15, 2016 13:18:07 GMT
I wonder if anyone wants to share some time based rules. I tried to switch some relays with cron with no luck. I followed some examples online but I wonder if I need to call a library? Also I'd like to record and report the last time a motion sensor was tripped. There's so much to do I figured these are probably common tasks many people have already done so why not ask. I just restarted with OH2 and ESP8266 nodes after setting this aside for a year. OH2 isn't much value in my opinion but the ESP wifi nodes without a gateway are super easy.
|
|
|
Post by papa on Dec 15, 2016 18:06:03 GMT
Greetings, camblonie Here's some examples that work for me for switching on a relay Node04 is a computourist DHT node with a pushbutton & also a solid state relay controlled by the signal that had gone to the LED in the schematic Related items: Away is an item whose state changes when I close or open a physical switch dedicated to that. String Node04_LED_Toggle {mqtt=">[mosquitto:home/rfm_gw/sb/node04/dev16:command:*:default]"} What I import ( for ALL purposes) at top of the rules file import org.openhab.core.library.types.* import org.openhab.core.persistence.* import org.openhab.model.script.actions.* import org.openhab.model.transformation.actions.* import java.lang.Math import java.util.Calendar import java.util.Date import java.util.TimeZone import java.text.SimpleDateFormat import java.util.Random rule "Random timer for Node04 Output ON about 7:30 pm every day" when Time cron "0 25 19 * * ?" then if (Away.state==CLOSED) { // Schedule a random ON event up to 15 minutes from now (19:25) createTimer(now.plusMinutes((new java.util.Random).nextInt(15)) ) [|sendCommand(Node04_LED_Toggle, "ON") ] } end .... sendCommand(Node04_LED_Toggle, "OFF") would schedule the relay to be turned off ====================== or instead of the pseudo-random rule above you could use this simpler one rule "Timer to turn ON Node04 Output at 2:28 pm every day" when Time cron "0 28 14 * * ?" then sendCommand(Node04_LED_Toggle, "ON") end ========================= Did you see these rule samples? In there, notice the line "The rules file probably needs the includes listed above." The list pretty much matches the includes / imports at the top of my rules file.
|
|
|
Post by greginkansas on Dec 15, 2016 23:28:32 GMT
I wonder if anyone wants to share some time based rules. I tried to switch some relays with cron with no luck. Also I'd like to record and report the last time a motion sensor was tripped. Working cron rule "Send Hum to wall display" when Time cron "35 0/11 * * * ?" // on minutes then Wall_guage_Hum.sendCommand(new DecimalType(outHumidity_acurite.state.toString)) logInfo(" Wall_guage_Hum "," sending wall Hum" + Wall_guage_Hum.state) end This does in out time for a rommba rule "front room Rommba_in_out_time" when Item node9_Rommba_status changed //received update then if (node9_Rommba_status.state == ON) { postUpdate(node9_Rommba_leave_time, new DateTimeType()) } if (node9_Rommba_status.state == OFF) { postUpdate(node9_Rommba_back_onbase, new DateTimeType()) } end
|
|
|
Post by camblonie on Dec 16, 2016 23:26:33 GMT
Papa, Maybe I'm not thinking about this correctly. My item is: Switch OUT40A <PushBtn> {mqtt=">[mymosquitto:home/esp_gw/sb/node40/dev16:command:ON:ON],>[mymosquitto:home/esp_gw/sb/node40/dev16:command:OFF:OFF]"} The switch on my sitemap functions fine. I'm trying to change that state with: sendCommand(OUT40A,"ON") Greg, what does your item look like for node9_Rommba_status?
|
|
|
Post by greginkansas on Dec 17, 2016 0:09:55 GMT
Papa, Maybe I'm not thinking about this correctly. My item is: Switch OUT40A <PushBtn> {mqtt=">[mymosquitto:home/esp_gw/sb/node40/dev16:command:ON:ON],>[mymosquitto:home/esp_gw/sb/node40/dev16:command:OFF:OFF]"} The switch on my sitemap functions fine. I'm trying to change that state with: sendCommand(OUT40A,"ON") Greg, what does your item look like for node9_Rommba_status? Switch node8_Rommba_status "Rommba_status" DateTime node8_Rommba_leave_time "Rommba left at [%1$tA, %1$tI:%1$tM:%1$tS %1$tp %Tp]" DateTime node8_Rommba_back_onbase "Rommba got back at [%1$tA, %1$tI:%1$tM:%1$tS %1$tp %Tp]" no quotes? from one of my rules sendCommand(Insteon1, OFF) postUpdate(Insteon1, OFF)
|
|
|
Post by papa on Dec 17, 2016 2:30:46 GMT
As I said, I gave what works for me: I create a string item for most of what's inside a sendcommand parentheses & then add the ON or OFF in quotes for the rule. String Node04_LED_Toggle {mqtt=">[mosquitto:home/rfm_gw/sb/node04/dev16:command:*:default]"} Then in the rule: sendCommand(Node04_LED_Toggle, "ON")
I have two other items related to Node04 This one is the switch for the User Interface Switch Act_Node04 {mqtt=">[mosquitto:home/rfm_gw/sb/node04/dev16:command:ON:ON],>[mosquitto:home/rfm_gw/sb/node04/dev16:command:OFF:OFF]"} This one keeps track of the switch state Switch Node04_Output { mqtt="<[mosquitto:home/rfm_gw/nb/node04/dev16:state:default]" }
Greg may have a different working method that could be useful. Though I wonder what greg's Insteon1 item looks like
|
|
|
Post by greginkansas on Dec 17, 2016 2:49:45 GMT
Papa, do you use string items for switch items? is there a advantage to doing it this way? Dimmer Insteon1 "Wall Light [%d %%]" (Christmas_lights) {insteonplm="2E.72.96:F00.00.19#dimmer"}
another one Switch node29_TVlight "TV stand light "{ mqtt="<[mosquitto:home/rfm_gw/nb/node29/dev16:state:default::]", mqtt=">[mosquitto:home/rfm_gw/sb/node29/dev16:command:*:default]" }
sendCommand(node29_TVlight, OFF)
|
|
|
Post by papa on Dec 17, 2016 15:27:51 GMT
Greg, you asked, "Papa, do you use string items for switch items?"
papa: In a rule's sendCommand(), I put a string item's name to "switch" a relay off or on.
Greg: "is there a advantage to doing it this way?"
papa: This is what I learned (stumbled into?) to get things to work. There could be other ways (like yours) that work, maybe better ones. I may experiment with what I understand of your way. I may be using more items & coding than are needed
With the following, I'm able to toggle the relay with 1) the node button, 2) the User Interface (UI) virtual switch, & 3) a rule. For all the above, the virtual switch on User Interface will also accurately record whether the relay is on or off.
This Node04 item is the switch for the User Interface Switch Act_Node04 {mqtt=">[mosquitto:home/rfm_gw/sb/node04/dev16:command:ON:ON],>[mosquitto:home/rfm_gw/sb/node04/dev16:command:OFF:OFF]"} This item keeps track of the switch state Switch Node04_Output { mqtt="<[mosquitto:home/rfm_gw/nb/node04/dev16:state:default]" } This is the string item used for sendCommand() in a rule (says "LED" but now a relay is on that device) String Node04_LED_Toggle {mqtt=">[mosquitto:home/rfm_gw/sb/node04/dev16:command:*:default]"}
rule "Timer to turn ON Node04 Output at 2:28 pm every day" when Time cron "0 28 14 * * ?" then sendCommand(Node04_LED_Toggle, "ON") end
rule "Match UI & Act_Node04" // On user interface, updates local state of relay caused by node button, no errors thrown when Item Node04_Output changed then if(Node04_Output.state==ON) { postUpdate(Act_Node04, ON) } if(Node04_Output.state==OFF) { postUpdate(Act_Node04, OFF) } end
|
|
|
Post by camblonie on Dec 18, 2016 0:08:42 GMT
Got side tracked migrating from RPi SD to USB and resulted in total reload .. doh!
This should fire every minute right? Time cron “0 1 * * * ?” I don't see anything going north on MQTT with: sendCommand(Node40OUTA,ON) my item is: String Node40OUTA {mqtt=">[mymosquitto:home/esp_gw/sb/node40/dev16:command:*:default]"}
My time is permanently off even though its correct on the RPi command line and OH2 shows it correctly. WTH?
|
|
|
Post by greginkansas on Dec 18, 2016 0:15:24 GMT
Ok it looks like the string might not cause much overhead compared to a switch. On using a "proxy item" for a north/south bound node I do this Switch node29_TVlight "TV stand light "{ mqtt="<[mosquitto:home/rfm_gw/nb/node29/dev16:state:default::]", mqtt=">[mosquitto:home/rfm_gw/sb/node29/dev16:command:*:default]" }
in and out on one item
|
|
|
Post by greginkansas on Dec 18, 2016 0:26:34 GMT
Got side tracked migrating from RPi SD to USB and resulted in total reload .. doh! This should fire every minute right? Time cron “0 1 * * * ?” I don't see anything going north on MQTT with: sendCommand(Node40OUTA,ON) my item is: String Node40OUTA {mqtt=">[mymosquitto:home/esp_gw/sb/node40/dev16:command:*:default]"} My time is permanently off even though its correct on the RPi command line and OH2 shows it correctly. WTH? I think you need the 0/ to fire every minute This fires every 15 minutes ON the 45 second mark Time cron "45 0/15 * * * ?" // on minutes
|
|
|
Post by papa on Dec 18, 2016 1:35:54 GMT
Though not related to the thread theme ... greg, you wrote, Thanks for that. You use only one item where my way has had 2 items & a rule to do the same things. For multiple nodes, it mounts up. I tried using one item with 2 mqtt bindings in this thread, but my version only partly worked. After testing a bit, I found greg's version works great & is shorter code than what I had.
I updated this thread & will update other locations accordingly.
|
|
|
Post by greginkansas on Dec 18, 2016 15:13:48 GMT
To me it looks like your outbound MQTT was not right in that thread. [mosquitto:home/rfm_gw/sb/node29/dev16:command:*:default]" } ^ ^
|
|
|
Post by papa on Dec 18, 2016 21:51:47 GMT
Heading back towards this thread's topic (sorry for the diversions, camblonie, though they were productive) ...
greginkansas, you wrote above:
I had been creating a string item version of a command & then (in a cron rule) was executing it with sendCommand() It worked, but greg's way just above needs no string item: Greg has a 2 binding switch item that 1) reports the node's device 16 state (relay) to gateway/OpenHAB & then 2) uses a User Interface virtual switch via OpenHAB/gateway to toggle the node's device 16 state. [This 1) then 2) order is essential.] Then as desired, greg executes that 2 binding switch item with sendCommand(). Greg's way does not seem to work better than mine, but is shorter coding & may be easier to understand.
OK, camblonie, now I'll offer some time based rules, basically expanding what I got from greg above... (blue color used below to help visibility, not functional)
items file entry (get a relay's device 16 state & use changes in a User Interface virtual switch to toggle the relay): (The following Switch item is one continuous line.) Switch Act_Node02 "A Light on a Relay" { mqtt="<[mosquitto:home/rfm_gw/nb/node02/dev16:state:default::]", mqtt=">[mosquitto:home/rfm_gw/sb/node02/dev16:command:*:default]" }
PS NO string item is needed in this approach
rules file entries: Again what I import (for ALL purposes) at top of the rules file import org.openhab.core.library.types.* import org.openhab.core.persistence.* import org.openhab.model.script.actions.* import org.openhab.model.transformation.actions.* import java.lang.Math import java.util.Calendar import java.util.Date import java.util.TimeZone import java.text.SimpleDateFormat import java.util.Random
rule "Timer for Node02 Relay & Light ON at 2:57 pm every day" when Time cron "0 57 14 * * ?" then sendCommand(Act_Node02, ON) end
rule "Timer for Node02 Relay & Light OFF at 3:24 pm every day" when Time cron "0 24 15 * * ?" then sendCommand(Act_Node02, OFF) end
sitemap file entry: Switch item=Act_Node02
Thanks, greg. Your posts above help me (for EACH node), reduce 3 items to one & omit one rule. What other tricks do you have up your sleeve? ;-)
|
|