DateTime MQTT binding

My arduino sendors send their system time (timestamp format) to my MQTT server.

Is it possible to feed the DateTime Item directly with the MQTT values.

Something like that :slight_smile:

``csv
DateTime time_device1 “Date from device 1 [%1$td.%1$tm.%1$tY - %1$tH:%1$tM]” {mqtt="<[mymosquitto:MQTT/RF/D/1/1/1:state:default]"}

I think we can be of more help if you provide some output from the MQTT broker.

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.

1 Like

I wanna know exactly tha same.
Is it possibile to send the current time via MQTT???

Send it from OH or send it to OH?

From Openhab to a thing like arduino+esp8266+lcd

I got it with this rule:

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

pretty simple but efficent

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.

You can’t use JSONPATH for an outgoing. JSONPATH takes some JSON and extracts things. That’s all. It does not work in reverse and will not build JSON.

You’d better tell the whole story, show your Item and its contents and your MQTT channel and what MQTT payload you are trying to make.

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 have to use Rules.

Clear now. Thanks both!

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.

Good points, thanks for the support