Need String item for Date and day

Hi i need to create 2 string item

first on how told me witch day of the week we are ( in french if possible )

Second the date like 20.12.2016 or something like that

Thanks

.items

DateTime Today

.rules

// import org.openhab.core.library.types.DateTimeType
// uncomment above on openHAB 1.x

rule SetDay
when
  System started or
  Time is midnight
then
  Today.postUpdate(new DateTimeType())
end

.sitemap

sitemap default label="Home Automation"
{
  Frame label="aujourd'hui" {
    Text item=Today label="Aujourd'hui nous sommes [%1$tA]"
    Text item=Today label="la date est [%1$d.%1$tm.%1$tY]"
  }
}
1 Like

Wow nice ! Thanks for the reply !

I guest i can do : DateTime Tomorrow ?

but if i want the day after tomorrow or the day after that is there a trick ?

Sure (but not tested!)

DateTime Today
DateTime Tomorrow
DateTime DayAfterTomorrow
// import org.openhab.core.library.types.DateTimeType
// uncomment above on openHAB 1.x
import java.util.Calendar

rule SetDay
when
  System started or
  Time is midnight
then
  val Calendar cal = Calendar::getInstance()
  Today.postUpdate(new DateTimeType(cal))

  cal.add(Calendar::DAY_OF_YEAR, 1)
  Tomorrow.postUpdate(new DateTimeType(cal))

  cal.add(Calendar::DAY_OF_YEAR, 1)
  DayAfterTomorrow.postUpdate(new DateTimeType(cal))
end

it’s working well thanks !

Now my issue is i want use that in other thing (not sitemap (habpanel)) so I would need that the item is really like “Lundi” without string transform … can i make a rule for that ? like tomorrow = tomorrow [%1$tA]

Items can have default labels like

DateTime Today "aujourd'hui [%1$tA]"
DateTime Tomorrow "demain [%1$tA]"
DateTime DayAfterTomorrow "après-demain [%1$tA]"

But I don’t know how HABPanel formats items otherwise. You could add String items and use String::format using the same formatting strings, but ideally that’s not necessary.

yeah … thanks but I made some test and search for info and I rely need a rule that say Today = Today [%1$tA]

so the real value of Today will be “Jeudi” not “2016-12-22T22:20:00.140-0500”

because like i said i’m not gonna use it in sitemap so it need to be already formatted and maybe latter i’m gonna make some rule that like like if Today == Friday send ON command to PARRTTTYY ! ( it’s just an exemple :stuck_out_tongue_winking_eye: )

OK, how about this (also untested)…

String Today "aujourd'hui [%s]"
String Tomorrow "demain [%s]"
String DayAfterTomorrow "après-demain [%s]"

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

rule SetDay
when
  System started or
  Time is midnight
then
  val Calendar cal = Calendar::getInstance()
  val SimpleDataFormat day = new SimpleDateFormat("EEEE");
  Today.postUpdate(new StringType(day.format(cal.time)))

  cal.add(Calendar::DAY_OF_YEAR, 1)
  Tomorrow.postUpdate(new StringType(day.format(cal.time)))

  cal.add(Calendar::DAY_OF_YEAR, 1)
  DayAfterTomorrow.postUpdate(new StringType(day.format(cal.time)))
end
1 Like

So i change the code reboot Openhab and …

item state is null (from the rest api OH 2.0 )

"link": "http://localhost:8080/rest/items/Tomorrow",
"state": "NULL",
"stateDescription": {

I’ve edited the example above to post a StringType and not a string. Also check your log to see if there is anything useful there. You can log:tail at the openhab> prompt and press Control-C when you want the prompt back.

Still “NULL”

BUT this is what i found in the log :

[ERROR] [.script.engine.ScriptExecutionThread] - Rule ‘SetDay’: An error occured during the script execution: The name ‘.format()’ cannot be resolved to an item or type.

Typo!

Here is a fresh copy tested on OH 1.8.3 and 2.0 Build 644. Change your when/then block to trigger when you want instead of every five seconds.

// 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
  System Started or
  Time cron "0/5 * * * * ?"
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
1 Like

Wow thanks !

Little question : why not leave midnight refresh?

My time is not getting update in real time, my site map is as follows

sitemap jackshome label="Jacks-Smarthome"
{

  Frame label="Today"
 {
    Text item=Today label="Today [%1$tA]"
    Text item=Today label="The Date is [ %1$tA, %1$td/%1$tm/%1$tY %1$tH:%1$tM ]"
  }

  Frame label="My South West Room"
  {
   Switch item=LEDTB
   Switch item=PSRBSTR
   Switch item=BDLT
   Switch item=FAN
  }
}

rule SetDay
when
  System started or
  Time is midnight
then
  Today.postUpdate(new DateTimeType())
end
1 Like