Parse Shell Variable to Text Sensor

All kinds of 'OS' scripts

Moderator: leecollings

Post Reply
michaelb
Posts: 14
Joined: Wednesday 25 January 2017 19:37
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Parse Shell Variable to Text Sensor

Post 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?
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Parse Shell Variable to Text Sensor

Post 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?'
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
erem
Posts: 230
Joined: Tuesday 27 March 2018 12:11
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Location: Amsterdam/netherlands
Contact:

Re: Parse Shell Variable to Text Sensor

Post 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" "
Regards,

Rob
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Parse Shell Variable to Text Sensor

Post 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 1755 times
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
erem
Posts: 230
Joined: Tuesday 27 March 2018 12:11
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Location: Amsterdam/netherlands
Contact:

Re: Parse Shell Variable to Text Sensor

Post 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:
Regards,

Rob
User avatar
waltervl
Posts: 5369
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: Parse Shell Variable to Text Sensor

Post 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"
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Parse Shell Variable to Text Sensor

Post 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 1724 times
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests