Hello everyone,
due to the fact that i lost some experimental stuff i did, i recently had to rework my rule to push values to Google Spreadsheet via IFTTT Webhook.
I wanted a slim solution only from Rule DSL and hat a bit a hard time regarding all the formatting (again).
Problems i had
- Slim conversion of Date-timestamp of yesterday
- Numeric value needs , instead of . for spreadsheets
- Correct usage of executeCommandLine for OH3
- Missing Values if multiple executeCommandLine are executed without sleep inbetween
So for others having the same intention → See my code below
Im not a very advanced openHAB user but i think the implementation is ok
ATTENTION: This code only works with OH3 since executeCommandLine needs each argument separately, divided by comma
I use the maximum of 3 values for
- Name/Tag
- Timestamp for provided value
- Value
→ this way i just push all numbers i want to into the spreadsheed
→ The filtering / searching / post-processing of the acquired data can then be done in spreadsheets directly
Improvements could be done:
- Create Date & curl-ingredients only once at start → For my approach i kept it individual for each value
- Somehow get it done to use the executeCommandLine as function with providing val* as parameter → Maybe someone has a solution for that
- If you see more, feel free to suggest some
I hope this is helpful and thanks for the informations in this topic
rule "Spreadsheets"
when
Time cron "0 0 3 * * ?"
then
// Variable declaration (Directly as String, because executeCommandLine needs strings)
var String val1
var String val2
var String val3
val1 = "PowerConsumptionLivingRoom" // Naming
val2 = String::format( "%1$te.%1$tm.%1$tY", now.minusDays(1)) // Timestamp for Value
val3 = (NUMBERITEM1.state as DecimalType).toString.replaceAll("\\.",",") // Numeric value, with replacing of . by ,
executeCommandLine("curl","-X","POST","-H","Content-Type: application/json","-d","{\"value1\":\"" + val1 + "\",\"value2\":\"" + val2 + "\",\"value3\":\"" + val3 + "\"}","https://maker.ifttt.com/trigger/oh_data/with/key/YOURKEY") // curl-command with right formatting
Thread::sleep(5000) // I discovered that executing multiple curls can cause lost data if there is not enough time inbetween
// Next curl (only as example)
val1 = "PowerConsumptionBedRoom"
val2 = String::format( "%1$te.%1$tm.%1$tY", now.minusDays(1))
val3 = (NUMBERITEM2.state as DecimalType).toString.replaceAll("\\.",",")
executeCommandLine("curl","-X","POST","-H","Content-Type: application/json","-d","{\"value1\":\"" + val1 + "\",\"value2\":\"" + val2 + "\",\"value3\":\"" + val3 + "\"}","https://maker.ifttt.com/trigger/oh_data/with/key/YOURKEY")
Thread::sleep(5000)
end