[SOLVED] Rules execbindings DHT11 String

Hi everbody i’m new with rules in openhab2 and execbindings value return.

I would like that i can turn off a LED when mt temperature from my DHT11 reaches 24°C.

This is are my files.

weatherstation.py

    #!/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)

weatherstation.items

    Group  Weatherstation_Chart (System, Charts)
    Number Weatherstation_Chart_Period "Periode" (System)
    Number Weatherstation_Temperature "Temperatura [%.1f °C]" <temperature> (Weatherstation_Chart)
    String temperature_out { channel="exec:command:weatherstation_temperature:output" }
    Switch LED{ gpio="pin:26" }

This is my rule but doesn’t work sure i have written something wrong in the return of the TEMP variable

weatherstation.rules

rule "termostato"
  when
     Item temperature_out received update
  then
    val TEMP=executeCommandLine("python /etc/openhab2/scripts/weatherstation.py 
  temperature",5000)
    if (TEMP.toString().length > 24) LED.sendCommand(OFF)
   end 

Somebody can help?

Please fix your code fences. Your post is very difficult to read.

Here

1 Like

Thanks! It is all new for me, hope it’s correct

Thanks for fixing your code fences :+1:

Add some logging to debug your rule:

logInfo("termostato", TEMP)
1 Like

You try to trigger the rule, with a item which is never updated.

You also miss the item file. An example explaining the exec binding.

rule "termostato"
  when
     Item temperature_out received update
  then
    if (  Float.parseFloat( Temperaturen_out.state) > 24) { 
    LED.sendCommand(OFF)
    }
   end 

Something like this. Not tested typed in my phone, may contain typos.

@warlock Edit:
Your thing looks good and the Eule gets triggered as you geht the logging.

Now parse string to float, the output value of exec is a string, which can not be compared to a number.

Can you toggle the led with a switch? Does the led work?

Removed triggeringItem since it works only with at least openhab 2.2.

1 Like

@luckymallari this is what i get from the log

openhab> log:tail  org.eclipse.smarthome.model.script.termostato           
22:06:51.300 [INFO ] [pse.smarthome.model.script.termostato] - 
22:06:58.911 [INFO ] [pse.smarthome.model.script.termostato] - 26.0
22:07:09.805 [INFO ] [pse.smarthome.model.script.termostato] - 26.0
22:07:25.820 [INFO ] [pse.smarthome.model.script.termostato] - 25.0
22:07:36.503 [INFO ] [pse.smarthome.model.script.termostato] - 26.0
22:07:47.517 [INFO ] [pse.smarthome.model.script.termostato] - 25.0
22:07:58.411 [INFO ] [pse.smarthome.model.script.termostato] - 25.0
22:08:15.090 [INFO ] [pse.smarthome.model.script.termostato] - 

But the LED doesn't go off.

I forgot to post the 
weatherstation.thing , sorry @Josar

Thing exec:command:weatherstation_temperature “Temperatura” [command="/etc/openhab2/scripts/weatherstation.py temperature", transform=“REGEX((.*?))”, interval=10, timeout=10, autorun=true]

@Josar i have tried to use your rule with my thing file but still nothing.

I’ll study the Tutorials & Examples you posted.

Let you know.

Post your actual rule (latest you have after suggestions above)

Maybe it has to look somehow like that.

rule "termostato"
  when
     Item temperature_out received update
  then
    if (  Float.parseFloat( Temperaturen_out.state.toString)  as Decimal > 24) { 
    LED.sendCommand(OFF)
    }
end 
received update

NOT

receiver update
1 Like

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]
        }
}

@warlock so got time to sit down and make a quick test by implementing a DS18B20 oneWire Temperature sensor.

This works for me.

        if( Float.parseFloat(triggeringItem.state.toString) > 25){
            logInfo("------------", " bigger")
        }else {
            logInfo("------------", " smaller")
        }

Also have a look here for some more info on how to reduce your rules.

1 Like