Assuming your timestamp format is either epoc (milliseconds since midnight 1970) or ISO8601 format (YYYY-MM-DDTHH:mm:SS) then yes. If not you will need to reformat the String either in your sender or in a Rule.
A JS transform can be used to take the epoch value and output an ISO string value that can be used for a DateTimeType. I think I offered such an example quite recently here somewhere.
rule "Data e ora LCD"
when
Time cron "0 * * * * ?"
then
var anno = (now.getYear).toString
var mese = (now.getMonthOfYear).toString
var giorno = (now.getDayOfMonth).toString
var ore = (now.getHourOfDay).toString
var minuto = (now.getMinuteOfHour).toString
var ora = ore + ":"+ minuto
var lcd_data = giorno + "/" + mese + "/" + anno + "-" + ora
publish("mosquitto","Int/N_S_in/N",lcd_data)
end
Hi, do you think it is possible to append datetime in ISO8601 format as string (%s) in MQTT payload in a sending command topic? Using for instance “formatbeforepublish” or tranformation output?
This is to avoid using Rules.
Any help will be appreciated.
Is the Item a DateTime? If so I think it already outputs in ISO8601 format by default. So if you create a DateTime Channel it should just output in ISO8601 by default, nothing needs to be done.
Hi, sorry I omitted the item type. The item is a Switch and I´m publishing plain JSON format as payload (field to modify is like {… ,“LT”:DateTime, …} ). When trying to modify Outgoing Value Transformation with JSONPATH:$.LT with the ISO8601 datetime information a WARN moes up “Executing the JSONPATH-transformation failed: Invalid path ‘$.LT’ in ‘Off’”. If something like this is not possible, then I guess it is mandatory to use Rules?
Thanks in advance.
A Switch Item doesn’t have a DateTime state. You can’t get it to publish a DateTime. Period. You will need to create a new Channel linked to a new Item.
Hi, thanks, I misunderstood the function of outgoing transformation. I just want to send the following JSON everytime I turn on/off a swicth {“SId”:“UE*”,“DId”:“RD-ESP01-EFBB”,“OxS”:"%s",“Dn”:“Ventilador”,“LT”:“2020-01-26T11:31:07Z”,“CMD”:“CHG”,“TYP”:“SW”} , the variable fields are “OxS” which I fill in with Switch state using Outgoing Value Format inside the item configuration, and “LT” which I need to fill in with current datetime in ISO8601 format (i.e. now function formated to ISO8601). Then the question is : can I solve the issue with Item configuration? or need to use Rules?
You could do this in a single outgoing transform, as there is only one Item state involved.
I think it is beyond the capabilities of just formatting.
I would write a javascript that takes the switch state, finds out the time, builds a JSON object with whatver you want, and serializes it.
Ok. Since I´m a beginner I wonder what is preferable: solving it via Rules(getActions+
publishMQTT) or by using javascript. Can you recommend when to use one or another method? , and/or is there any difference in the execution performance between both methods? Thanks !
I have no recommendation. Many things in openHAB can be achieved with alternate methods. Performance differences don’t matter much unless you are repeating hundreds of times.
As a beginner you might choose the method that gets you results quickest, or the one you are most confident in understanding,or the one that gives more opportunity to learn.
You can change your mind later.