DDorch / Rthingsboard

R package for interacting with the API of ThingsBoard open-source IoT platform.
GNU Affero General Public License v3.0
3 stars 0 forks source link

TS output '1970-01-01 01:00:00' and no value #7

Closed smjacques closed 2 years ago

smjacques commented 3 years ago

Hi everyone, Trying to reproduce the example from README, I get UNIX starting time and no value as shows the documentation.

Also tried with my thingsboard device and got the same error.

The code I first used is this:

library(ggplot2)
library(Rthingsboard)
library(curl)
library(knitr)

url = "http://scada.g-eau.fr"
publicId = "299cedc0-f3e9-11e8-9dbf-cbc1e37c11e3"
entityId = "18d56d50-f3e9-11e8-9dbf-cbc1e37c11e3"
startDate = as.POSIXct("2020-11-19 15:00:00", tz = "Europe/Paris")
endDate = as.POSIXct("2020-11-19 18:00:00", tz = "Europe/Paris")

# Connection to the API
tb_api = ThingsboardApi(url = url, publicId = publicId)

# Get list of keys
keys = tb_api$getKeys(entityId = entityId)

df <- tb_api$getTelemetry(entityId,
                          keys = keys[grep("^Y", keys)],
                          startTs = startDate,
                          endTs = endDate)

knitr::kable(head(df))

The output is this:

"";"key";"ts";"value"
"1";"temperature";1970-01-01 01:00:00;1
"2";"temperature";1970-01-01 01:00:00;1
"3";"temperature";1970-01-01 01:00:00;1
"4";"temperature";1970-01-01 01:00:00;1
"5";"temperature";1970-01-01 01:00:00;1
"6";"temperature";1970-01-01 01:00:00;1
"7";"temperature";1970-01-01 01:00:00;1
"8";"temperature";1970-01-01 01:00:00;1
"9";"temperature";1970-01-01 01:00:00;1
"10";"temperature";1970-01-01 01:00:00;1
"11";"temperature";1970-01-01 01:00:00;1
"12";"temperature";1970-01-01 01:00:00;1

Should I add a previous step to update the date and access the value?

pedro-nonfree commented 3 years ago

putting on the beginning of the file options(stringsAsFactors = FALSE) (cancelling the usage of factors), makes it work. But then it is a little bit slower. Why not using data.table ? :rocket:

I would be happy to know how to edit a package, right now I copied all the functions at the beginning of my script and did it with print debugging

with

      cat('\n\n')
      cat(paste(str(dfV)))
      stop('here')

I got

'data.frame':   800 obs. of  3 variables:
 $ key  : Factor w/ 8 levels "Y0","Y1","Y2",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ ts   : Factor w/ 100 levels "1605804700329",..: 100 99 98 97 96 95 94 93 92 91 ...
 $ value: Factor w/ 31 levels "2.6398","2.7493",..: 4 2 1 2 2 2 2 2 4 1 ...

https://github.com/DDorch/Rthingsboard/blob/main/R/thingsboard_api.R#L210

dfV$ts <- EpochMilli2Date(dfV$ts, timezone = attributes(startTs)$tzone)

this function, for some reason, takes the factor values instead of the long integers

it happens somehow the same with the resulting value, it is the factor instead of its double value. The result is a nonsense for the ts and value columns of the dataframe.

> df
    key                  ts value
1    Y0 1970-01-01 01:00:00     4
2    Y0 1970-01-01 01:00:00     2
3    Y0 1970-01-01 01:00:00     1
4    Y0 1970-01-01 01:00:00     2
5    Y0 1970-01-01 01:00:00     2
6    Y0 1970-01-01 01:00:00     2
7    Y0 1970-01-01 01:00:00     2
DDorch commented 2 years ago

I was not able to reproduce this bug, but anyway I've added a stringsAsFactors = FALSE argument in the building data.frame instructions. Hope this will definitely solve this issue.