Hi watou,
Apologies, I’m coming to this topic a little late, however, I wanted to ask if you could elaborate a little further on the reply you gave Thierry?
I am also new to openHAB and am not that familiar with Java (though have been learning a lot of late) and wanted to achieve something similar, in so far as I wanted to generically extract the payload from an incoming MQTT topic, which I will later use to form an extensible solution.
I have had no trouble binding to incoming floats using the many examples on updating temperatures, humidity etc. even states (with mapping) and ints are fine.
However I am at a loss as to how to extract freeform text in the MQTT payload within a rule.
In this first instance I want to get hold of a 5 byte string representing a logging period in ‘HH:MM’ 24hrs format ie 00…24 for HH, and use this to update the openHAB UI in overall minutes. In effect at start up ‘System started’ I am pinging some remote IoT devices to read a selection of current values such that I can synchronise the UI controls.
I’ve listed out below as far as I’ve got (though suspect some of the rule may be wrong which I’ll correct later), the area I’m have issues with is the line;
var Date dt = formatter.parse(EthernetLoggingPeriodUpdateConfirm.state)
You mention in your reply String.split can be used. I checked out the Xtend Movies example but am still none the wiser.
I would appreciate any help or pointers you could give me.
I copied the code below, apologies for flattened format.
Regards
Steve
.sitemap
Setpoint item=EthernetSetLoggingPeriod minValue=10 maxValue=1440 step=10
.items
Number EthernetSetLoggingPeriod “Logging Period [%d min]” (GC_Config,ConfigGroup,ConfigGroupLoggingPeriodSet)
String EthernetLoggingPeriodUpdateConfirm “[%s]” (GC_Config,ConfigGroup,ConfigGroupLoggingPeriodSet) {mqtt="<[mosquitto:/EthernetDevice/GetLoggingPeriodConfirm:state:default]"}
.rules
rule "Get Current Logging Period"
when
Item EthernetLoggingPeriodUpdateConfirm changed
then
var int hours
var int minutes
var DateFormat formatter = new SimpleDateFormat(“HH:mm”)
var Date dt = formatter.parse(EthernetLoggingPeriodUpdateConfirm.state)
var Calendar cal = Calendar.getInstance()
cal.setTime(dt)
hours = cal.get(Calendar.HOUR)
minutes = cal.get(Calendar.MINUTE)
logDebug(“stevequinnhousehold”, “GetLoggingPeriod ==>”+String::format("%02d", hours)+":"+String::format("%02d", minutes))
EthernetSetLoggingPeriod.sendCommand( (hours * 60) + minutes)
end