I’ve built a garden sprinkler system with Wemos and relay, attached to a solenoid, using MQTT to/from OpenHAB to control. It works well, however I’m using a fairly basic number format for the “Next run hour”. Instead, I want the “Next run hour” to appear in a friendly format rather than just a number.
As per screenshot below, you can see the “Next run hour” is just a Number in 24 hour format, not that user friendly. I want to convert to, for example, “Next run hour = 5:00PM” I’ve read the datetime (joda) vs datetimetype (java) - and the screeds of confusion between them, yet have had a heap of log errors on type conversions etc, while trying to work out how to do using a DateTime item.
(Note: I haven’t done the “Check Weather” portion yet, that will simply check weather binding if its raining, and not run if it is)
The items file as follows (the 2nd “Next run hour” is what I want to move too)
/* Sprinkler System */
Number sprinklerState "Sprinkler" <settings> { mqtt="<[mqttbroker:openhab/sprinkler/status:state:default]" }
String sprinklerIPAddress "Sprinkler IP [%s]" <network> { mqtt="<[mqttbroker:openhab/sprinkler/ipaddress:state:default]" }
Number sprinklerNextHour "Next run hour [%.0f]" <faucet>
DateTime sprinklerNextHourFriendly "Next run hour [%1$tl:%1$tM %1$tp]" <faucet>
Switch sprinklerSwitch "Enable schedule" <lawnmower>
Switch sprinklerIncludeWeather "Check weather" <sun_clouds>
Switch sprinklerRunNow "Run now / running" <rain> (gSprinkler) { mqtt=">[mqttbroker:openhab/sprinkler/trigger:command:ON:START], >[mqttbroker:openhab/sprinkler/trigger:command:OFF:STOP], <[mqttbroker:openhab/sprinkler/trigger:state:ON:START], <[mqttbroker:openhab/sprinkler/trigger:state:OFF:STOP]" }
Number sprinklerRuntime "Runtime remaining [%.0f mins]" <time> { mqtt="<[mqttbroker:openhab/sprinkler/runtime:state:default]" }
Number sprinklerRunEvery "Run every (hours) [%.0f]" <flow>
Number sprinklerDuration "Run duration (min) [%.0f]" <time> { mqtt=">[mqttbroker:openhab/sprinkler/duration:command:*:${command}]" }
Relevant sections in the rules file:
rule "Calculate Uptime and Sprinkler status every 1 minute"
when
Time cron "0 * * * * ?"
then
// ...does other stuff here...
// Check sprinkler next run, if enabled...
if (sprinklerSwitch.state == ON && boolSprinklerRunning == false)
{
// To check weather if enabled...
// Check next hour to run
var hourNow = now.getHourOfDay()
var hourNext = sprinklerNextHour.state as Number
// If hours match, time to run sprinkler
if (hourNow == hourNext)
{
// Increment and store next run time 'hour'
val RunEvery = sprinklerRunEvery.state as Number
val RunNext = now.getHourOfDay + RunEvery
if (RunNext > 23)
{
RunNext = RunNext - 24
}
sendCommand(sprinklerNextHour, RunNext)
// Start sprinkler
boolSprinklerRunning = true
publish("mqttbroker", "openhab/sprinkler/duration", sprinklerDuration.state.toString)
publish("mqttbroker", "openhab/sprinkler/trigger", "START")
logInfo("sprinkler", "Sprinkler started. Next start hour " + sprinklerNextHour.state.toString)
}
}
end
rule "Sprinkler runtime complete"
when
Item sprinklerRuntime changed
then
if (sprinklerRuntime.state == NULL || sprinklerRuntime.state == 0)
{
logInfo("Sprinkler", "Sprinkler run cycle complete or cancelled")
boolSprinklerRunning = false
sendCommand(sprinklerRunNow, OFF)
}
end
rule "Sprinkler run every hours changed"
when
Item sprinklerRunEvery changed
then
val RunEvery = sprinklerRunEvery.state as Number
val RunNext = now.getHourOfDay + RunEvery
if (RunNext > 23)
{
RunNext = RunNext - 24
}
sendCommand(sprinklerNextHour, RunNext)
end
So, should I persist with using only a DateTime item for the RunNext hour, or should I use a String item instead and format it myself, or should I use sprinklerNextHour simply as a hidden proxy item and utilise the other item to format the user friendly date time view?
What would be the easiest method here? Any suggestions appreciated.
Cheers