Indeed, it should be easier if I explain what I want to do and why. Perhaps there is an easier path to solve 
So:
I have about 45 devices with tasmota software installed at home. Each of these devices reports its condition via MQTT to the mosquito at a specified teleperiod (300seconds).
As there were times when devices lost their network and restarted, I would like to be able to see when I received the last report from a given device. Reason of my post: a check whether my devices are still online or working properly.
Each of them, as I posted above, sends report that look like this:
08:37:09 MQT: tele / K / Hall / BME / SENSOR = {"Time": "2021-02-01T08: 37: 09", "BME280": {"Temperature": 24.7, "Humidity": 24.3 , "DewPoint": 2.9, "Pressure": 988.1}, "PressureUnit": "hPa", "TempUnit": "C"}
08:33:56 MQT: tele / K / Hall / Light / STATE = {"Time": "2021-02-01T08: 33: 56", "Uptime": "11T09: 10: 22", "UptimeSec": 983422, "Heap": 28, "SleepMode": "Dynamic", "Sleep": 50, "LoadAvg": 19, "MqttCount": 1, "POWER": "OFF", "Wifi": {... }}
08:53:44 MQT: tele / K / Hall / Dryer / SENSOR = {"Time": "2021-02-01T08: 53: 44", "ENERGY": {"TotalStartTime": "2020-12-13T16: 25:27 "," Total ": 25.004," Yesterday ": 0.007," Today ": 0.004," Period ": 0," Power ": 0," ApparentPower ": 0," ReactivePower ": 0," Factor " : 0.00, "Voltage": 241, "Current": 0.000}}
I imagined that I could extract part of report “Time:” 2021-02-01T08: 37: 09 " from these reports and then extract characters from 9 to 16, which return me “01T08:37” which is the day and time along with the minutes of the last message sent.
So for one item you solved the matter by helping above. Now it works like that:
String WeatherReportA "Weather report" {channel = "mqtt: topic: d1weather: read"}
so that string has one transformation already within Things configuration:
Type string: read “Weather” [
stateTopic = “tele / K / Hall / BME / SENSOR”,
transformationPattern = “JSONPATH: $. Time”
]
so every 5 minutes I receive string “2021-02-01T08: 37: 09”
then working with rules
rule “lastmessage”
when
Item WeatherReportA changed
then
// use the transformation service to retrieve the value
val newValue = WeatherReportA.state.toString.substring (09.16)
// post the new value to the Item
NEWread.postUpdate (newValue)
end
so receive my expected value “01T08:37” at
String NEWread "TransformedDataValue"
However, following a similar way of thinking to the remaining 44 devices would mean creating another 44 rules which could make my code messy.
POSSIBLE SOLUTION:
I thought that maybe we could create a general transformation function, e.g. in the form of Transform> javascript, which I would refer to the channel in the things text file
Type string: read2 “Weather” [
stateTopic = “tele / K / Hall / BME / SENSOR”,
transformationPattern = “JS: test.js”
]
String WeatherReportA "Weather report" {channel = "mqtt: topic: d1weather: read2"}
I even started writing a function test.js but it doesn’t work as expected and only returns UNDEF.
(function (xyz) {
if (xyz! == 0) {
// here is part of Time extraction missing cause first trying to do substring extraction
var newValue = xyz.state.toString.substring (09.16);
return newValue;
}
else {
return “UNDEF”;
}
}) (input)
Man that is longest post of my life. What do you think about? (solution not post itself
)