[SOLVED] If Today is Monday

Hi All,

I’m currently using the following script provided by @watou, which provides the current day of the week.

// import org.openhab.core.library.types.StringType
// uncomment above on openHAB 1.x
import java.text.DateFormat
import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Date

rule SetDay
when
  Time cron "0 0 0/1 1/1 * ? *"
then
  var Calendar cal = Calendar::instance
  var DateFormat fmt = new SimpleDateFormat("EEEE")
  var String todayString = fmt.format(cal.time)
  Today.postUpdate(todayString)
  logInfo("SetDay","Today is " + todayString)

  cal.add(Calendar::DAY_OF_YEAR, 1)
  var String tomorrowString = fmt.format(cal.time)
  Tomorrow.postUpdate(tomorrowString)
  logInfo("SetDay","Tomorrow is " + tomorrowString)

  cal.add(Calendar::DAY_OF_YEAR, 1)
  var String dayAfterString = fmt.format(cal.time)
  DayAfterTomorrow.postUpdate(dayAfterString)
  logInfo("SetDay","Day After is " + dayAfterString)
end

What I would now like to is have a rule running so that

if today is Monday then post.Update(“Take bins out”)

If someone can point me in the right direction as I get confused with the format it needs to be in.

Thanks

Hi,
this should do the trick:

//dayofweek=1 = Monday
if (now.getDayOfWeek == 1){
BinreminderItem.postUpdate("Take bins out")
}

HTH,
-OLI

1 Like

A side note: I guess your rule says "normally " on Mondays take the bins out. Since there are some weeks in the year where this might get shifted I used a calendar object ( which could be filled with the data you get from the one who schedules the correct dates. This way you won’the get the reminder on a holiday! Which has happened to me.

Ah yes I see what you mean, luckily my collection day is actually a Wednesday so that public holidays don’t actually affect my collections.

I will probably have to do calendar events for trash collections as they are every other week.

In here the companies doing that provide an input file for the calendar! That helps!

1 Like

Wow that would be nice… Although they’s probably charge us a whole lot more for the service if they did.

Oli,

Same sort of question, tried a few variances of the script but cannot get an output of any sort.

I have an item: Number openhabCommunityResult "[%.0f]" that produces a selection of different set of numbered results from a rule with the following possibilities:

0 - paused
1 - not checked yet
2 - up
8 - seems down
9 - down

What I would like to do is convert the number to text. I have tried using transform and the maps but I cannot get them to work for me… (unsure if there is an ongoing issue). What I currently have but doesn’t work is:

import org.openhab.core.library.types.

rule “OpenHAB Community Transform 0"
when
Item openhabCommunityResult received update
then
if (openhabCommunityResult == 2) {
communityResult.postUpdate("Community is Up!")
}
end

I’ve tried openhabCommunityResult.state == 2. I can’t remember the others I’ve tried but if you could help it would be great!

Try:
(openhabCommunityResult.state == 2)
that should work. check what the value of the item really is(either checking log file or via karaf console)
Maybe your rule for setting the item is flaky and it stays uninitialised?
HTH,
-OLI

P.S.: “Days Until Pay Day” - you really concentrate on the important things! :smile:
Can’t stop smiling…i always knew something important was missing in my setup!

Hmm, I think my issue must lay within the value to the item as I keep returning NULL on HABPanel…

Rule I have running for value:
import org.openhab.core.library.types.*

rule "Check Openhab Community"
when
        Item UptimeRobotOpenhabCommunity_Output changed
then
        var String UROC = UptimeRobotOpenhabCommunity_Output.state.toString
        var String type = transform("JSONPATH", "$.monitors.[0]status", UROC)
        logInfo("Testing", "Parse Json result = " + UROC)
       
        openhabCommunityResult.postUpdate(new DecimalType(transform("JSONPATH", "$.monitors.[0]status", UROC)))
end

Which is giving me the result in the logs of:

2017-04-25 09:48:21.605 [INFO ] [lipse.smarthome.model.script.Testing] - Parse Json result = {“stat”:“ok”,“pagination”:{“offset”:0,“limit”:50,“total”:1},“monitors”:[{“id”:778712140,“friendly_name”:“Openhab Community”,“url”:“https://community.openhab.org”,“type”:1,“sub_type”:"",“keyword_type”:"",“keyword_value”:"",“http_username”:"",“http_password”:"",“port”:"",“interval”:300,“status”:2,“create_datetime”:1487826050}]}

Despite actually showing as 2 on the HABPanel…

Any ideas to get around this?

P.S. “Days Until Pay Day” is my way of stopping me from making too many impulsive purchases until the last week :joy:

Ugh…sorry, not using JSON at all so can’t help here.

:+1: It’s OK! Appreciate the help you’ve already provided! I’ll get another post up on here now and see where I can get.

Nice, works like a charm