sensor data to shell script

Moderator: leecollings

Post Reply
Robban
Posts: 15
Joined: Monday 09 November 2015 6:55
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

sensor data to shell script

Post by Robban »

Hi

I wonder how i retreive a sensordata (ex temperature) to a shell script?

i want my pi to say the temperature in a scenario

Robert
User avatar
Egregius
Posts: 2582
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: v2024.7
Location: Beitem, BE
Contact:

Re: sensor data to shell script

Post by Egregius »

Did you look at the json commands in the wiki?

Maybe this is a starting point. I use this script to let my iMac go to sleep depending on 3 switches in Domoticz:

Code: Select all

#!/bin/bash
function jsonValue() {
	KEY=$1
	num=$2
	awk -F"[,:}]" '{for(i=1;i<=NF;i++){if($i~/'$KEY'\042/){print $(i+1)}}}' | tr -d '"' | sed -n ${num}p
}
DOMOTICZ=`curl -s --connect-timeout 2 --max-time 5 "http://192.168.0.10:8084/json.htm?type=devices&used=true&plan=7"`
if [ -z "$DOMOTICZ" ]
then
	exit 1
else
	WEG=$(echo $DOMOTICZ | jsonValue Status 1)
	SLAPEN=$(echo $DOMOTICZ | jsonValue Status 2)
	IMACSLAPEN=$(echo $DOMOTICZ | jsonValue Status 3)
	if [[ -z "$WEG" || -z "$SLAPEN" || -z "$IMACSLAPEN" ]] ; 
	then
		exit 1
	fi
	if [ $WEG == "On" ] || [ $SLAPEN == "On" ] || [ $IMACSLAPEN == "On" ]; then
		/Applications/MyApps/imacsleep.command
	fi
fi
The script grabs the sensors from roomplan 7 (so it only get the 3 switches instead of all used devices).
The litle jsonValue function decodes the json data and puts the status in variables.
Is one of the variables can't be set, the scripts exits.
If one of 3 statusses is 'On' a second script is called.
Robban
Posts: 15
Joined: Monday 09 November 2015 6:55
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: sensor data to shell script

Post by Robban »

i can call a webpage to get the data from a sensor, but i want to extract the Data value in a sh script
cant find it anywhere
Robban
Posts: 15
Joined: Monday 09 November 2015 6:55
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: sensor data to shell script

Post by Robban »

can´t figure out how to mask out a ex temperature data from a shell script.
lika i said i can call a website with the temperatue status, adress etc
User avatar
Egregius
Posts: 2582
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: v2024.7
Location: Beitem, BE
Contact:

Re: sensor data to shell script

Post by Egregius »

Maybe you should post your code? And explain better what you want?
Do you want to grab a temperature from a website and push it to domoticz? Or do you want to rab a temperature from Domoticz and use it in a shell script?
Robban
Posts: 15
Joined: Monday 09 November 2015 6:55
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: sensor data to shell script

Post by Robban »

yes, i have voicerss working .
and i want a shell script that says ex " Godmorning, the outside temperature is xx degress.
the temperature is taken from a sensor inside the domoticz
User avatar
Egregius
Posts: 2582
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: v2024.7
Location: Beitem, BE
Contact:

Re: sensor data to shell script

Post by Egregius »

This should get you starting:

Code: Select all

#!/bin/bash
function jsonValue() {
   KEY=$1
   num=$2
   awk -F"[,:}]" '{for(i=1;i<=NF;i++){if($i~/'$KEY'\042/){print $(i+1)}}}' | tr -d '"' | sed -n ${num}p
}
DOMOTICZ=`curl -s --connect-timeout 2 --max-time 5 "http://192.168.0.10:8084/json.htm?type=devices&rid=278"`
if [ -z "$DOMOTICZ" ]
then
   exit 1
else
   TEMP=$(echo $DOMOTICZ | jsonValue Temp 1)
   echo $TEMP
fi
The variable $TEMP contains the temperature of thermometer with idx 278.
Value is for ex 20.40 so you should trim that a bit I guess.
Robban
Posts: 15
Joined: Monday 09 November 2015 6:55
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: sensor data to shell script

Post by Robban »

THANKS ALOT!. worked fine
Robban
Posts: 15
Joined: Monday 09 November 2015 6:55
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: sensor data to shell script

Post by Robban »

noticed that i got temperature like 18,80 , how do i get it to 18.8?
User avatar
Egregius
Posts: 2582
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: v2024.7
Location: Beitem, BE
Contact:

Re: sensor data to shell script

Post by Egregius »

Do you get 18 comma 80 or 18 point 80?
Wouldn't it be better to just remove the decimal and just say the whole degrees?
Then you could use something like this:

Code: Select all

#!/bin/bash
function jsonValue() {
   KEY=$1
   num=$2
   awk -F"[,:}]" '{for(i=1;i<=NF;i++){if($i~/'$KEY'\042/){print $(i+1)}}}' | tr -d '"' | sed -n ${num}p
}
DOMOTICZ=`curl -s --connect-timeout 2 --max-time 5 "http://192.168.0.10:8084/json.htm?type=devices&rid=278"`
if [ -z "$DOMOTICZ" ]
then
   exit 1
else
   TEMP=$(echo $DOMOTICZ | jsonValue Temp 1)
   TEMPCLEAN=${TEMP%.*}
   echo $TEMP $TEMPCLEAN
fi
Change TEMPCLEAN=${TEMP%.*} to TEMPCLEAN=${TEMP%,*} if comma is your decimal separator.
Robban
Posts: 15
Joined: Monday 09 November 2015 6:55
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: sensor data to shell script

Post by Robban »

that did it! THANKS!
:-D
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest