Hi to all,
@Josar no the code you posted doesn’t produce results and let rules file not accepted.
I found some code in
’https://tutorials-raspberrypi.de/raspberry-pi-wetterstation-bauen-openhab2/’
So i made some upgrades.
I use a DHT11 (for temperature and humidity) and a BMP180 (for pressure)
Number Weatherstation_Chart_Period "Periode" (System)
Number Weatherstation_Temperature "Temperatura [%.1f °C]" <temperature> (Weatherstation_Chart)
Number Weatherstation_Humidity "Umidità [%.1f %%]" <humidity> (Weatherstation_Chart)
Number Weatherstation_Pressure "Pressione [%.1f hPa]" <pressure> (Weatherstation_Chart)
String temperature_out { channel="exec:command:weatherstation_temperature:output" }
String humidity_out { channel="exec:command:weatherstation_humidity:output" }
String pressure_out { channel="exec:command:weatherstation_pressure:output" }
Switch LED{ gpio="pin:26" }
Switch RELE1{ gpio="pin:20" }
Switch RELE2{ gpio="pin:21" }
Thing exec:command:weatherstation_temperature "Temperatura" [command="/etc/openhab2/scripts/weatherstation.py temperature", transform="REGEX((.*?))", interval=2, timeout=10, autorun=true]
Thing exec:command:weatherstation_humidity "Umidità" [command="/etc/openhab2/scripts/weatherstation.py humidity", transform="REGEX((.*?))", interval=2, timeout=10, autorun=true]
Thing exec:command:weatherstation_pressure "Pressione" [command="/etc/openhab2/scripts/weatherstation.py pressure", transform="REGEX((.*?))", interval=2, timeout=10, autorun=true]
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import Adafruit_BMP.BMP085 as BMP085
import Adafruit_DHT
import sys, time
# constants
DHT_PIN = 4 # GPIO nr
DHT_SENSOR = Adafruit_DHT.DHT11
if __name__ == '__main__':
if len(sys.argv) > 1:
call = sys.argv[1].lower()
if call == 'temperature':
temperature = None
while temperature == None:
_, temperature = Adafruit_DHT.read_retry(DHT_SENSOR, DHT_PIN)
if temperature == None:
time.sleep(1.0)
print(temperature)
elif call == 'humidity':
humidity = None
while humidity == None:
humidity, _ = Adafruit_DHT.read_retry(DHT_SENSOR, DHT_PIN)
if humidity == None:
time.sleep(1.0)
print(humidity)
elif call == 'pressure':
sensor = BMP085.BMP085()
print(sensor.read_pressure() / 100.0)
And here we are , i managed to get rules to work ( code under), I realy don’t know what the code means
( ( Float::parseFloat(temperature_out.state.toString) as Number ) * 10 ) / 10
and how to use it with sense i tried a cut and paste and now it works.
I would only like to know ho to get the value as a number o decimal so to be used in any rule, i don’t think the by 10 and the division by 10 are necessary. Plus like this i get temperatures in decimal it would be nice to have even to have values with one digit after the comma, like
24,1 24,5 …
I know I have to get more knowhow, would you like to give me some advice and interpretation of the code? Or improvements
rule "Weatherstation Temperatura"
when
Item temperature_out received update
then
Weatherstation_Temperature.postUpdate(
( ( Float::parseFloat(temperature_out.state.toString) as Number ) * 10 ) / 10
)
end
rule "Weatherstation Umidità"
when
Item humidity_out received update
then
Weatherstation_Humidity.postUpdate(
( ( Float::parseFloat(humidity_out.state.toString) as Number ) * 10 ) / 10
)
end
rule "Weatherstation Pressione"
when
Item pressure_out received update
then
Weatherstation_Pressure.postUpdate(
( ( Float::parseFloat(pressure_out.state.toString) as Number ) * 10 ) / 10
)
end
rule "RELE 1 ON"
when
Item LED changed from OFF to ON
then
RELE1.sendCommand(ON)
end
rule "RELE 1 OFF"
when
Item LED changed from ON to OFF
then
RELE1.sendCommand(OFF)
end
rule "termostato"
when
Item temperature_out received update
then
if (((( Float::parseFloat(temperature_out.state.toString) as Number ) * 10 ) / 10)>24) {
LED.sendCommand(OFF)
}
end
rule "termostato"
when
Item temperature_out received update
then
if (((( Float::parseFloat(temperature_out.state.toString) as Number ) * 10 ) / 10)<=24) {
LED.sendCommand(ON)
}
end
sitemap weatherstation label="Raspberry Pi Stazione Meteo"
{
Frame label="LED"
{
Switch item=LED
}
Frame label="Relè"
{
Switch item=RELE1
Switch item=RELE2
}
Frame label="Meteo" {
Text item=Weatherstation_Temperature
Text item=Weatherstation_Humidity
Text item=Weatherstation_Pressure
}
Frame {
Switch item=Weatherstation_Chart_Period mappings=[0="1h", 1="4h", 2="8h", 3="12h", 4="24h"]
Chart item=Weatherstation_Chart period=h refresh=60000 visibility=[Weatherstation_Chart_Period==0, Weatherstation_Chart_Period=="Uninitialized"]
Chart item=Weatherstation_Chart period=4h refresh=60000 visibility=[Weatherstation_Chart_Period==1]
Chart item=Weatherstation_Chart period=8h refresh=60000 visibility=[Weatherstation_Chart_Period==2]
Chart item=Weatherstation_Chart period=12h refresh=60000 visibility=[Weatherstation_Chart_Period==3]
Chart item=Weatherstation_Chart period=D refresh=60000 visibility=[Weatherstation_Chart_Period==4]
}
}