Java Runtime Environment: which java platform is used and what version
openHAB version: 2.5.4
Hello All!
I am trying to display the date and time that something occurred and display said date and time on my sitemap. I have two items I want to do this with:
So when the motion detector triggers, or when the door sensor state is changed, I’d like to display this under a “Motion Last Detected …” or “Door Last Opened…” sitemap frame.
I have tried adapting other’s rules to my situation, with no luck so far. Here’s the current nonworking rule:
import java.text.SimpleDateFormat
import java.util.Date
rule "Multisensor 6 Motion"
when
Item MultiSensor_Motion changed
then
var epoch = new DateTime((MultiSensor_Motion.state as Number).longValue)
val DateTime datetime = new DateTime(epoch)
val String time = datetime.toLocalTime().toString("HH:mm")
val String date = datetime.toLocalDate().toString("dd-MM-yyyy")
logInfo("motion.detection.rules", "Trial Part 1" + time.toString + " op " + date.toString)
MultiSensor_Motion.sendCommand(datetime , new DateTimeType() )
end
Any help with this would be greatly appreciated. In addition, this is my first post, so I apologize if I broke any community rules.
The root of your problem here is that MultiSensor_Motion is a Switch type Item. You can’t send a Switch a datetime for a command. You especially cannot send it two datetimes for one command.
As you’ll find in design note @hafniumzinc suggested, you really need to use a separate Item.
The limitation with a profile method is that it always updates the timestamp. If you instead choose to use a rules based method, you get more control so that for example you can truly record when the door opens (and not when it closes).
Thank you both for this! The profile method worked flawlessly; I am now working to display the date and time in a more user-friendly format. Currently it’s “2020-07-19 20:11”, and I’d like it to be a bit more ‘friendly.’
@rossko57, what would a rule like that look like? Do you have a template or another forum post you could link me to?