Page 1 of 1

Parse Shell Variable to Text Sensor

Posted: Thursday 29 April 2021 11:40
by michaelb
Hello all,

I want to update a custom text sensor with the expiration date of my free Lets Encrypt certificate on my Synology.

So far I've been able to find and store the date in a Shell Variable using Bash but updating the text sensor is not working (in various use of brackets and parenthises)..

The code I use is the following:

Code: Select all

export expdate=$(openssl x509 -noout -dates -in /usr/syno/etc/certificate/_archive/******/cert.pem | grep 'notAfter=' | sed 's/.........//')
curl -s "http://domoipaddress:8080/json.htm?param=udevice&username=******=&password=******==&type=command&idx=365&nvalue=0&svalue='$expdate'"
The text in the sensor gets updated but only with the text $expdate instead of its value...

What's going wrong?

Re: Parse Shell Variable to Text Sensor

Posted: Thursday 29 April 2021 12:12
by waaren
michaelb wrote: Thursday 29 April 2021 11:40 What's going wrong?
I tried this with just a text value in expdate and the text sensor then gets updated with the expected value. Can you repeat that test and if that works share the value / format of your expdate date var?'

Re: Parse Shell Variable to Text Sensor

Posted: Thursday 29 April 2021 13:10
by erem
Inside single quotes everything is preserved literally, without exception.

That means you have to close the quotes, insert something, and then re-enter again.

Code: Select all

curl -s "http://domoipaddress:8080/json.htm?param=udevice&username=******=&password=******==&type=command&idx=365&nvalue=0&svalue=" $expdate" "

Re: Parse Shell Variable to Text Sensor

Posted: Thursday 29 April 2021 13:47
by waaren
erem wrote: Thursday 29 April 2021 13:10 Inside single quotes everything is preserved literally, without exception.
Without exception?

Code: Select all

export expdate=test

curl -s "http://127.0.0.1:8080/json.htm?param=udevice&type=command&idx=12&nvalue=0&svalue='$expdate'"
{
        "status" : "OK",
        "title" : "Update Device"
}
text sensor.png
text sensor.png (8.76 KiB) Viewed 2181 times

Re: Parse Shell Variable to Text Sensor

Posted: Thursday 29 April 2021 15:16
by erem
from here: https://www.gnu.org/savannah-checkouts/ ... gle-Quotes

Sorry, it seems i was misinformed.
Mea Culpa, Mea Maxima Culpa. :oops: :oops:

Re: Parse Shell Variable to Text Sensor

Posted: Thursday 29 April 2021 16:28
by waltervl
But bash is not the same as running commands from the command line
@waaren did you run a bash script or ran the commands from a command line?


@michaelb I think you can skip the single quotes in your bash script

Code: Select all

curl -s "http://domoipaddress:8080/json.htm?param=udevice&username=******=&password=******==&type=command&idx=365&nvalue=0&svalue=$expdate"

Re: Parse Shell Variable to Text Sensor

Posted: Thursday 29 April 2021 18:11
by waaren
waltervl wrote: Thursday 29 April 2021 16:28 @waaren did you run a bash script or ran the commands from a command line?
From a bash command line

Code: Select all

ps -p $$
    PID TTY          TIME CMD
1331003 pts/0    00:00:00 bash
waltervl wrote: Thursday 29 April 2021 16:28 @michaelb I think you can skip the single quotes in your bash script
The reason I asked to share the value / format of your expdate date var is that there might be spaces and other chars in the var that cannot be send 'raw' to domoticz.

Code: Select all

#!/bin/bash
#
rawDate=$(date)
echo $rawDate
#
#This will fail
#
curl -s "http://127.0.0.1:8080/json.htm?param=udevice&type=command&idx=12&nvalue=0&svalue=$rawDate"
echo;echo;echo

# Oneliner to URL encode a string
URL_EncodedDate=$(date |  curl --get --silent --output /dev/null --write-out %{url_effective} --data-urlencode @- ""  | sed -E 's/..(.*).../\1/'i)
echo $URL_EncodedDate
curl -s "http://127.0.0.1:8080/json.htm?param=udevice&type=command&idx=13&nvalue=0&svalue=$URL_EncodedDate"

result

Code: Select all

./curlTest.sh

Thu 29 Apr 2021 06:06:47 PM CEST
<html><head><title>Bad Request</title></head><body><h1>400 Bad Request</h1></body></html>


Thu%2029%20Apr%202021%2006%3A06%3A47%20PM%20CEST
{
        "status" : "OK",
        "title" : "Update Device"
}


text sensor.png
text sensor.png (18.16 KiB) Viewed 2150 times