Use a JAVASCRIPT transform instead of the JSON transform:
See:
In the script you can check if uid exists before forwarding the info
Use a JAVASCRIPT transform instead of the JSON transform:
See:
In the script you can check if uid exists before forwarding the info
I already get the data from my controller, if the rfid tag is valid.
And i don´t have any skills in javascript… So i will stay inside openhab rules.
I already tried the date format examples from the tutorial thread, see my post with my rules file.
Here is what i tried also, but not working. First gives me an error
[ERROR] [ntime.internal.engine.RuleEngineImpl] - Error during the execution of startup rule 'RFID Haustür MQTT': Invalid format: "1542997079" is malformed at "9"
and second one gives me nothing, no error, no value.
val Number newValue_time = transform("JSONPATH", "$.time", rfid_all_haustuer.state.toString)
logInfo("RFID", "RFID Haustür -1- ---> MQTT --> number newValue_time: " + newValue_time)
val MyJodaFromEpoch = new DateTime(newValue_time)
logInfo("RFID", "RFID Haustür -1-0- ---> MQTT --> joda time: " + MyJodaFromEpoch)
var SimpleDateFormat df = new SimpleDateFormat( "dd.MM., HH:mm" )
var String timestamp = df.format( newValue_time )
logInfo("RFID", "RFID Haustür -1-1- ---> MQTT --> string time: " + timestamp)
This works, but only a workaround…
val String now_string = now.toString
logInfo("RFID", "RFID Haustür -1-0- ---> MQTT --> now_string time: " + now_string)
With this i get the time, when openhab receives the data, but not the time from my rfid-controller. But it is better than nothing…
** Install the JAVASCRIPT transformation**
Create a file called uid.js in the transform folder with the following contents:
(function(jsonString) {
var data = JSON.parse(jsonString);
if (data.hasOwnProperty("uid")) {
var uid = data.uid;
}
return uid;
})(input)
Your item then becomes:
String rfid1_uid { mqtt="<[mosquitto:esprfid:state:JS($.uid.js)]" }
Reading to learn more on js code and have a question. Should there be a extra “)” in the if statement like below?
(function(jsonString) {
var data = JSON.parse(jsonString);
if (data.hasOwnProperty("uid")) { // extra ")" added to this line
var uid = data.uid;
}
return uid;
})(input)
Thanks
Correct. Sorry for the typo.