Telegraf json v2 parsing - telegraf

I'm trying to pick out the local_temperature property from a Zigbee TRV.
Here is my section from /etc/telegraf/telegraf.conf
[[inputs.mqtt_consumer]]
servers = ["tcp://127.0.0.1:1883"]
topics = [
"zigbee2mqtt/Home/+/Radiator",
]
data_format = "json_v2"
[[inputs.mqtt_consumer.json_v2]]
measurement_name = "temperature"
[[inputs.mqtt_consumer.topic_parsing]]
topic = "_/room/_"
[[inputs.mqtt_consumer.json_v2.field]]
path = "local_temperature"
type = "float"
Error from /var/log/syslog
Feb 21 11:03:07 mini31 telegraf[23428]: 2022-02-21T11:03:07Z E! [inputs.mqtt_consumer] Error in plugin: metric parse error: expected tag at 1:36: "{\"battery\":97,\"boost_heating\":\"OFF\",\"boost_heating_countdown\":0,\"boost_heating_countdown_time_set\":300,\"child_lock\":\"UNLOCK\",\"current_heating_setpoint\":8,\"eco_mode\":\"OFF\",\"eco_temperature\":12,\"linkquality\":110,\"local_temperature\":17.5,\"local_temperature_calibration\":-2,\"max_temperature\":24,\"min_temperature\":8,\"position\":25,\"preset\":\"programming\",\"programming_mode\":\"09:00/19°C 12:00/13°C 14:00/19°C 17:00/8°C 06:00/8°C 12:00/8°C 14:30/8°C 17:30/8°C 06:00/8°C 12:30/8°C 14:30/8°C 18:30/8°C\",\"window\":\"CLOSED\",\"window_detection\":\"OFF\"}"
What does it even mean?

[[inputs.mqtt_consumer]]
servers = ["tcp://127.0.0.1:1883"]
topics = [
"zigbee2mqtt/Home/+/Radiator",
]
data_format = "json_v2"
[[inputs.mqtt_consumer.json_v2]]
measurement_name = "temperature"
[[inputs.mqtt_consumer.topic_parsing]]
topic = "zigbee2mqtt/Home/+/Radiator"
tags = "_/_/room/_"
[[inputs.mqtt_consumer.json_v2.field]]
path = "local_temperature"
rename = "temperature"
type = "float"
[[inputs.mqtt_consumer.json_v2]]
measurement = "valve"
[[inputs.mqtt_consumer.json_v2.field]]
path = "position"
rename = "valve"
type = "float"

Related

azure kql parse function - unable to parse ? using regex (zero or one time)

I'm trying to parse this line:
01/11/1011 11:11:11: LOG SERVER = 1 URL = /one/one.aspx/ AccountId = 1111 MainId = 1111 UserAgent = Browser = Chrome , Version = 11.0, IsMobile = False, IP = 1.1.1.1 MESSAGE = sample message TRACE = 1
using this parse statement:
parse-where kind=regex flags=i message with
timestamp:datetime
":.*LOG SERVER = " log_server:string
".*URL = " url:string
".*AccountId = " account_id:string
".*MainId = " main_id:string
".*?UserAgent = " user_agent:string
",.*Version = " version:string
",.*IsMobile = " is_mobile:string
",.*IP = " ip:string
".*MESSAGE = " event:string
".*TRACE = " trace:string
now the thing is that sometimes I got records that has one "key=value" missing but the order of the rest of the columns remains the same.
to match all kinds of rows I just wanted to add (<name_of_colum>)? for example:
"(,.*Version = )?" version:string
but it fails everytime.
I think parse/parse-where operators are more useful when you have well formatted inputs - the potentially missing values in this case would make it tricky/impossible to use these operators.
If you control the formatting of the input strings, consider normalizing it to always include all fields and/or add delimiters and quotes where appropriate.
Otherwise, you could use the extract function to parse it - the following expression would work even if some lines are missing some fields:
| extend
timestamp = extract("(.*): .*", 1, message, typeof(datetime)),
log_server = extract(".*LOG SERVER = ([^\\s]*).*", 1, message),
url = extract(".*URL = ([^\\s]*).*", 1, message),
main_id = extract(".*MainId = ([^\\s]*).*", 1, message),
user_agent = extract(".*UserAgent = ([^,]*).*", 1, message),
version = extract(".*Version = ([^,]*).*", 1, message),
is_mobile = extract(".*IsMobile = ([^,]*).*", 1, message),
ip = extract(".*IP = ([^\\s]*).*", 1, message),
event = iff(message has "TRACE", extract(".*MESSAGE = (.*) TRACE.*", 1, message), extract(".*MESSAGE = (.*)", 1, message)),
trace = extract(".*TRACE = (.*)", 1, message)

How do I format values in my lua table to be: t = {['foo'] = true, ['bar'] = true}?

This is in relation to a previous question: Checking values across multiple location and returning a match only if the sources are unique.
Essentially, the function relys on the data being in the format:
local vendors = {
Asda = {Kellogg = true, Cadbury = true, Nestle = true, Johnsons = true, Pampers = true, Simple = true},
Tesco = {Kellogg = true, Cadbury = true, Nestle = true, Johnsons = true},
Spar ={Nestle = true, Johnsons = true, Pampers = true, Simple = true}
}
However, I am collecting the data by cycling through path locations and adding them into a table, which just creates a list such as:
Asda = {"Kellogg", "Cadbury", "Nestle", "Johnsons", "Pampers", "Simple"}
There is another way I can add them:
local Asda = {}
for index = 1, 9 do
local pathAsda = factReference -- some path location which changes by index increasing
if pathAsda ~= "" then
Asda[#Asda+1] = {[Asda] = true} -- table.insert(Asda, pathAsda), for the previously mentioned format
end
Which would leave me with:
Asda= {{Kellogg = true}, {Cadbury = true}, {Nestle = true}, {Johnsons = true}, {Pampers = true}, {Simple = true}}
I'd then use:
table.insert(vendorSources,Asda)
Neither of these formats work with the function in the answer and I can't seem to figure out how to amend any section to enable this to work.
function intersection(s1, s2) -- finds out if two sets (s1 & s2) overlap
local output = {}
for key in pairs(s1) do
output[#output + 1] = s2[key]
end
return output
end
Is there a way to edit either list (Asda) to be in the correct format?
You'd need to use Asda[pathAsda] = true instead of Asda[#Asda+1] = {[pathAsda] = true}, but keep in mind that the order elements is not guaranteed in this case.

How to: New order Binance API via RStudio

I am trying to create a new order via the Binance API using RStudio.
I found the Binance Official API Docs and figured out that I should use
POST /api/v3/order (HMAC SHA256).
The following script doesn't work out for me:
url='https://api.binance.com/api/v3/account'
GET(url,
add_headers("X-MBX-APIKEY"= *[my API key]*),
query=list("symbol"="ETHBTC",
"side"="BUY",
"type"="MARKET",
"quantity"=1,
recvWindow=5000,
"timestamp"=1499827319559,
"signature"=**???**),
verbose())
Does anyone know what I'm doing wrong and how I can create an order via the Binance API using RSTUDIO and how I can create my signature?
library(httr)
timestamp <-
as.character(jsonlite::fromJSON(content(
GET("https://api.binance.com/api/v1/time"), "text"
))$serverTime + 999)
query <-
list(
"symbol" = "VENBTC",
"side" = "BUY",
"type" = "MARKET",
"quantity" = 1,
"recvWindow" = 5000,
"timestamp" = timestamp
)
signature <-
digest::hmac(
key = "*[my secret key]*",
object = paste(names(query), query, sep = "=", collapse = "&"),
algo = "sha256"
)
POST(
url,
add_headers("X-MBX-APIKEY" = "*[my API key]*"),
query = c(query, signature = signature),
verbose()
)

How to send email in R that read csv and send multiple email at once?

I have CSV file that contains email.
How to send multiple email in r ?
Error 1
send.mail function not taking data.frame values
Error in FUN(X[[i]], ...) :
Sorry, parameter type `NA' is ambiguous or not supported.
Error 2
function not taking input from read.table
Error in file.exists(body) : invalid 'file' argument
My code as below (credentials as an example only)
temp <- read.csv("E:/OneDrive/M.Tech/temp.csv",header = FALSE)
m <- data.frame(temp)
email <- m[2,14]
rollno <- m[2,1]
file <- read.table("C:/Mehul Katara/New folder (2)/131004.txt",header = TRUE,sep = ";")
send.mail(from = "user#gmail.com",
to = email,
subject = rollno,
body = file,
smtp = list(host.name = "smtp.gmail.com", port = 465, user.name = "user#gmail.com", passwd = "password", ssl = TRUE),
authenticate = TRUE,
send = TRUE)
There is just one error with this. Use:
file <- read.table("C://Mehul Katara//New folder (2)//131004.txt",header = TRUE,sep = ";")
So you need to change / in path to // because of R windows binary behavior. Above path may also not work but in that case you need to get rid of spaces as in "MehulKatara". Please put your file in another place where there is no space in filepath. Error 1 is coming just because of error 2 and should go as soon as you fix error 2.
From the documentation
body Body of the email as text. If the parameter body refers to an
existing file location, the text of the file is parsed as body of the
email.
so ideally you should change call of function to something like:
send.mail(from = "user#gmail.com",
to = email,
subject = rollno,
body ="C://Mehul_Katara//New_folder_(2)//131004.txt"
smtp = list(host.name = "smtp.gmail.com", port = 465, user.name = "user#gmail.com", passwd = "password", ssl = TRUE),
authenticate = TRUE,
send = TRUE)

Httr header returns invalid character '-' in numeric literal

I'm using the httr to access the API for stockfighter, a CTF style trading game.
The GET function is working without any problems, but when I try and authenticate using an API key in the headers it doesn't appear to be working. Here's my place_order function
place_order <- function(acct, exchange, stock, price, qty,
direction = c("buy", "sell"),
type = c("limit", "market", "fill-or-kill",
"immediate-or-cancel")) {
# Place a stock order
if (!exists("key")) stop("No authorisation key defined.")
direction <- match.arg(direction)
type <- match.arg(type)
bdy <- list("account" = acct, "venue" = exchange, "symbol" = stock,
"price" = price, "qty" = qty, "direction" = direction,
"orderType" = type)
rurl <- paste(burl, "/venues/", exchange, "/stocks/", stock, "/orders",
sep = "")
r <- POST(rurl, body = bdy, add_headers(`X-Starfighter-Authorization` = key))
return(content(r))
}
This is what I get in return:
$ok
[1] FALSE
$error
[1] "invalid character '-' in numeric literal"
It appears that the JSON is not escaping the dashes correctly.
This is the response I get when I post to httpbin instead of the API:
$args
named list()
$data
[1] ""
$files
named list()
$form
$form$account
[1] "RB34256134"
$form$direction
[1] "buy"
$form$orderType
[1] "limit"
$form$price
[1] "12400"
$form$qty
[1] "100"
$form$symbol
[1] "FOOBAR"
$form$venue
[1] "TESTEX"
$headers
$headers$Accept
[1] "application/json, text/xml, application/xml, */*"
$headers$`Accept-Encoding`
[1] "gzip, deflate"
$headers$`Content-Length`
[1] "751"
$headers$`Content-Type`
[1] "multipart/form-data; boundary=------------------------49a2e51c0c6926dd"
$headers$Host
[1] "httpbin.org"
$headers$`User-Agent`
[1] "libcurl/7.43.0 r-curl/0.9.4 httr/1.0.0.9000"
$headers$`X-Starfighter-Authorization`
[1] "OBFUSCATED KEY HERE"
$json
NULL
$origin
[1] "1.125.48.185"
$url
[1] "http://httpbin.org/post"
I feel like this is probably a really stupid simple error but I can't work it out.
EDIT:
Here's the python method using requests and json that works perfectly.
def sf_post(path, data, key, **kwargs):
base_url = "https://api.stockfighter.io/ob/api/"
r = requests.post("%s/%s" % (base_url, path), data = data, headers = {'X-Starfighter-Authorization': key}, **kwargs)
return(r)
def order(self, price, qty, direction, order_type):
data = dict(account = self.account, venue = self.exchange, symbol = self.symbol, price = price, qty = qty,
direction = direction, orderType = order_type)
r = sf_post("%s/orders" % self.rurl, data = json.dumps(data), key = self.key)
return r.json()
cph = Stock("CPH", "EXMBEX", account = "ACCOUNTCODEHERE", key = os.environ.get("SF_KEY"))
cph.order(5000, qty = 100, direction = "buy", order_type = "limit")
{u'direction': u'buy', u'ok': True, u'ts': u'2016-01-24T00:35:21.148877285Z', u'fills': [{u'price': 4694, u'ts': u'2016-01-24T00:35:21.148881279Z', u'qty': 100}], u'originalQty': 100, u'orderType': u'limit', u'symbol': u'CPH', u'venue': u'EXMBEX', u'account': u'SSM90915021', u'qty': 0, u'id': 754, u'totalFilled': 100, u'open': False, u'price': 5000}
I thought I was probably missing something stupid, and as #hadley pointed out in the comments I was. I needed to add encode = "json" to my POST call. For posterity here's the updated function code:
place_order <- function(acct, exchange, stock, price, qty,
direction = c("buy", "sell"),
type = c("limit", "market", "fill-or-kill",
"immediate-or-cancel")) {
if (!exists("key")) stop("No authorisation key defined.")
direction <- match.arg(direction)
type <- match.arg(type)
bdy <- list("account" = acct, "venue" = exchange, "symbol" = stock,
"price" = price, "qty" = qty, "direction" = direction,
"orderType" = type)
rurl <- paste(burl, "venues/", exchange, "/stocks/", stock, "/orders",
sep = "")
r <- POST(rurl, body = bdy,
add_headers(`X-Starfighter-Authorization` = key),
encode = "json")
return(content(r))
}

Resources