struggle script (including json rule) >> update virtual sensor

Moderator: leecollings

Post Reply
Jan Jansen
Posts: 229
Joined: Wednesday 30 April 2014 20:27
Target OS: Raspberry Pi / ODroid
Domoticz version: Stable
Location: The Netherlands
Contact:

struggle script (including json rule) >> update virtual sensor

Post by Jan Jansen »

Hi all,

I connected an AM2302 (Temp/Hum sensor) on my rpi (running Domoticz). I first created a virtual sensor (idx 109). Then I tried viewtopic.php?f=12&t=9531#p79641, crontab 5 minutes.

Code: Select all

#!/bin/sh

# Domoticz server
SERVER="127.0.0.1:8080"
# DHT IDX
DHTIDX="109"

# DHTPIN
DHTPIN="17"

sleep 5

sudo nice -20 python /home/pi/Adafruit_Python_DHT/examples/AdafruitDHT.py 2302 $DHTPIN > /var/tmp/temp.txt

#TEMP=$(cat /var/tmp/temp.txt | grep "Temp" | awk '{ print $3 }')
#TEMP=$(cat /var/tmp/temp.txt | grep "Temp")

TEMP=$(awk ' /Temp/ {print substr ($0,6,4)}' /var/tmp/temp.txt)
#HUM=$(awk ' /Hudmidity/ {print substr ($0,0)}' /var/tmp/temp.txt)
#HUM=$(awk ' /Humidity/ {print 1$}' /var/tmp/temp.txt)
HUM=$(awk ' /Humidity/ {print substr ($0,22,4)}' /var/tmp/temp.txt)
echo $TEMP
echo $HUM

# Send data
curl -s -i -H "Accept: application/json" "http://$SERVER/json.htm?type=command&c=getauth&param=udevice&idx=$DHTIDX&nvalue=0&svalue=$TEMP;$HUM;2"


TEMP=""
HUM=""
In temp.txt I see every 5 minutes an update on temp and hum. I therefore think that the first part of the script works, but there is no update of the virtual sensor (color red). I took a look in the Wiki (json) but I found no solution.

Then I tried the following code based on: https://easydomoticz.com/raspberry-et-dht11/ .

Code: Select all

#!/bin/bash

# Domoticz server
SERVER="192.168.0.6:8080"

# DHT IDX
DHTIDX="109"

#DHTPIN
DHTPIN="17"

TMPFILE="/var/tmp/dhtsensor1.txt"

cpt=0
while [ $cpt -lt 6 ]
do
TEMP=""
sleep 5
sudo nice -20 python /home/pi/domoticz/scripts/Adafruit_Python_DHT/examples/AdafruitDHT.py 2302 $DHTPIN > $TMPFILE
TEMP=$(cat $TMPFILE|grep Temp |awk '{print $3}')
if [ $TEMP ]
then
TEMP=$(cat $TMPFILE|grep Temp |awk '{print $3}')
HUM=$(cat $ $TMPFILE |grep Temp |awk '{print $7}')
#echo $TEMP
#echo $HUM
# Send data
curl -s -i -H  "Accept: application/json"  "http://$SERVER/json.htm?type=command&param=udevice&idx=$DHTIDX&nvalue=0&svalue=$TEMP;$HUM;2"
TEMP=""
HUM=""
rm $TMPFILE
exit 0
fi
#echo $cpt
cpt=$(($cpt+1))
done
exit 1
In dhtsensor1.txt I see every 5 minutes an update on temp and hum. But again no update on the virtual sensor.

I'm stuck, I hope someone can help.

Thank you in advance!

Jan
stlaha2007
Posts: 370
Joined: Monday 05 October 2015 10:16
Target OS: -
Domoticz version:
Contact:

Re: struggle script (including json rule) >> update virtual sensor

Post by stlaha2007 »

Hi Jan,

Can you post the contents of the files in /var/temp and whats on your console when you run it yourself?

I'm doing simular things... And i think the wrong or no value at all for hum is generated...

And also try curl from commandline, to test its response. Perhaps you just getting ERR by feeding through JSON with nothing.
Jan Jansen
Posts: 229
Joined: Wednesday 30 April 2014 20:27
Target OS: Raspberry Pi / ODroid
Domoticz version: Stable
Location: The Netherlands
Contact:

Re: struggle script (including json rule) >> update virtual sensor

Post by Jan Jansen »

Hello stlah,

First thanks for your quick reply. Before I read your post i have tried next http://blog.briankmarsh.com/domoticz/ .

Code: Select all

#!/bin/bash

# Domoticz server
SERVER="localhost:8080"

# IDX DHT
# The number of the IDX in the list of peripherals
DHTIDX="109"

#DHTPIN
# THE GPIO or connects DHT11
DHTPIN="17"

# If you have a DHT22 change further down the line Adafruit_DHT 11 by 22 Adafruit_DHT

# TMPFILE: path for temporary file in place to avoid the RAMDRIVE
# Scriptures on the SD card
# Otherwise be written way or the file containing the temperature
# /tmp/temper.txt Is a good choice if not installed RAMDRIVE
# Www.easydomoticz.com visit to learn all

TMPFILE="/var/tmp/dhtsensor1.txt"

# Modified patrick from 03/08/15 to question 5 times max
cpt=0
while [ $cpt -lt 6 ]
do
TEMP=""
sleep 5
sudo nice -20 /home/pi/domoticz/scripts/Adafruit_Python_DHT/examples/AdafruitDHT.py 2302 $DHTPIN > $TMPFILE
TEMP=$(cat $TMPFILE|grep Temp |awk '{print $1}')
if [ $TEMP ]
then
TEMP=$(cat $TMPFILE|grep Temp |awk '{print $1}' |sed -r 's/^.*=//' |  sed -r 's/\*//')
HUM=$(cat $TMPFILE |grep Temp |awk '{print $2}' | sed -r 's/^.*=//' | sed -r 's/\%//')

if [ $(echo "$HUM < 30" |bc) ]
then
COMFORT=2
elif [ $(echo "$HUM < 40" |bc) ]
then
COMFORT=0
elif [ $(echo "$HUM < 60" |bc) ]
then
COMFORT=1
else
COMFORT=3
fi

echo "TEMP: $TEMP"
echo "HUM: $HUM"
echo "COMFORT: $COMFORT"
# send data
curl -s -i -H "Accept: application/json" "http://$SERVER/json.htm?type=command&param=udevice&idx=$DHTIDX&nvalue=0&svalue=$TEMP;$HUM;$COMFORT"
TEMP=""
HUM=""
#rm $TMPFILE
exit 0
fi
echo "CPT: $cpt"
cpt=$(($cpt+1))
done
exit 1
Running this one shows:
Output.PNG
Output.PNG (13.99 KiB) Viewed 1221 times
I hope you know a solution.

Thanks in advance!

Jan
Jan Jansen
Posts: 229
Joined: Wednesday 30 April 2014 20:27
Target OS: Raspberry Pi / ODroid
Domoticz version: Stable
Location: The Netherlands
Contact:

Re: struggle script (including json rule) >> update virtual sensor

Post by Jan Jansen »

Hi

After reading viewtopic.php?f=28&t=14383&p=105237&hil ... ed#p105237 I found a solution.

Jan
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest