Thought so too but didn’t work for me
-
ShellSensor running linux command with pimatic variable
-
@mwittig
https://github.com/pimatic/pimatic/blob/26b81a38e477a9ef32cc985a8d1452b014cac099/lib/matcher.coffee#L392I guess we would only need to add
%
here ore is it used for something else within pimatic as the matcher is used everywhere? -
@thex said in ShellSensor running linux command with pimatic variable:
For the second point. As I said this can’t handle days yet end expects the input in seconds.
"timeFormat(($test * 60))"
Should at least calculate correct hours/minutes of your uptime
I currently have 1122 minutes which is 18 hours 42 minutes.
When I use thattimeFormat($cv-control-uptime * 60)
I do get67320:00:00
which is ridiculous.If I look at the function
args: number: type: "number" exec: (number) -> hours = parseInt(number) decimalMinutes = (number-hours) * 60 minutes = Math.floor(decimalMinutes) seconds = Math.round((decimalMinutes % 1) * 60)
So it expects hours meaning I need to do:
timeFormat($cv-control-uptime / 60)
which indeed delivers me (now):18:49:00
-
Oh sorry, it expects it as decimal hours so
timeFormat(($test / 60))
should be right but it doesn’t really help you as it doesn’t currently support days -
If you want to displays days as well you need something like:
args: number: type: "number" exec: (number) -> days = parseInt(number/24) hours = parseInt(number - (days*24)) minutes = parseInt(number -((days*24)+hours))
and perhaps display it as
2d 12:13:14
or something else “nice” -
And coming back to the other part of the question with the script:
#!/bin/bash VARNAME=$1 # Variable to post back UPTIME=$2 # value (=calculated) to post back CALCULATED=$(printf '%d days %02d hours %02d minutes' $((UPTIME/1440)) $((UPTIME%1440/60)) $((UPTIME%60))) curl -s -X PATCH --header "Content-Type:application/json" --data '{"type": "value", "valueOrExpression": "'$CALCULATED'"}' --user "$USERNAME:$PASSWORD" http://localhost/api/variables/$VARNAME > /dev/null
When I run that script from the command line or from my shellsensor I get:
error [pimatic]: Error on incoming http request to /api/variables/cvcontroluptcalc: Unexpected end of input
This script is a 100% copy of another “calculate time” script apart from the fact that the calculation is different. If I add an echo $CALCULATED the uptime is nicely echoed. The other script works fine.
Somehow the $CALCULATED is “wrong” but I have no idea why.
Anyone? -
I now know why the error occurs: Due to the spaces!
I send something like
7 days 23 hours 40 minutes
in the variable. Then I get theerror [pimatic]: Error on incoming http request to /api/variables/cvcontroluptcalc: Unexpected end of input
.This makes a lot of sense as you have to urlencode such a spaces containing string so I tried:
curl -s -X PATCH --header "Content-Type:application/json" --data-urlencode '{"type": "value", "valueOrExpression": "'$CALCULATED'"}' --user "$USERNAME:$PASSWORD" http://localhost/api/variables/$VARNAME > /dev/null
Note the--data-urlencode
instead of--data
.Now the http request does not fail but pimatic barfs on the string as it encounters a % sign.
error [pimatic]: Error on incoming http request to /api/variables/cvcontroluptcalc: Unexpected token %
This becomes a vicious circle.
-
@Harry-van-der-Wolf I am confused. Isn’t the patch data sent as an Json object in the request body? So, why should this be URL-encoded. I think you need to further escape quotes to get it right.
"It always takes longer than you expect, even when you take into account Hofstadter's Law.", Hofstadter's Law
-
@Harry-van-der-Wolf The following should work:
curl -s -X PATCH --header "Content-Type:application/json" --data '{"type": "value", "valueOrExpression": "'"$CALCULATED"'"}' --user "$USERNAME:$PASSWORD" http://localhost/api/variables/$VARNAME > /dev/null
Here is my test run for setting a variable ‘xy’:
pi@raspi3:~ $ CALCULATED="7 days 23 hours 40 minutes" pi@raspi3:~ $ VARNAME=xy pi@raspi3:~ $ curl -XPATCH --header "Content-Type:application/json" --data '{"type": "value", "valueOrExpression": "'"$CALCULATED"'"}' --user "$USERNAME:$PASSWORD" http://localhost/api/variables/$VARNAME {"variable":{"name":"xy","readonly":false,"type":"value","value":"7 days 23 hours 40 minutes","unit":""},"success":true}
"It always takes longer than you expect, even when you take into account Hofstadter's Law.", Hofstadter's Law
-
Thank you.
I had already tried the same but as"'\"$CALCULATED\"'"
as I thought I had to escape the second ".Your version works indeed.