String to DateTime item

Hi all,

I’m receiving a date via HTTP binding that is formatted as YYYY-MM-DD. I would like to save as DateTime item, so something like:

DateTime HTTP_Weatherbit_Fore24H_Date          "Forecast for [%1$tY-%1$tm-%1$td]"       (gOutdoor, gWeather)                { http="<[weatherbitFore:10000:JSONPATH($.data[1].valid_date)]"}

the problem is: if I use a String item, I can see the value (eg 2020-05-24). But when I try to convert automatically in DateTime item, using “[%1$tY-%1$tm-%1$td]”, the value of the item is “NULL”.

Any suggestion how to do that? Forced to use a parse rule, or any chance to keep it simpler?

That’s a date, not a datetime, so the default parsing won’t like it much. It probably tells you as much in your log.

If you don’t want to use a rule, you might write yourself a javascript transformation, that can do both the JSON extraction and string manipulation to make a string suitable for a datetime Item.
It’s no simpler than a rule, it does the same work after all.

1 Like

the difference is just 1 item vs 2 items (in a rule)?

This is my solution (dunno if the best, but it works :))

rule "Dates from Weatherbit"
    Item HTTP_Weatherbit_Fore24H_Date received update or
    System started
    val Weatherbit_Date = HTTP_Weatherbit_Fore24H_Date.state.toString
    val DateTimeType MyDateTimeTypeFromString = DateTimeType.valueOf(Weatherbit_Date)