AN13 STE2 thermometer (HW group) switches NETIO sockets based on temperature | NETIO products: Networked power sockets
Tags: 
3rd party HW-SW

STE2 by HW group is a LAN/WiFi thermometer/humidity meter. The NETIO AN13 Application Note presents a Lua script that connects NETIO 4x smart sockets with the STE2 thermometer. 230V electrical sockets are switched on or off according to the temperature (or humidity) measured by STE2. The devices communicate over a LAN, values are transferred in a .xml file. The script can be modified for other devices and xml data

Do you have any questions?

Every second, a Lua script running in the NETIO smart sockets device reads a .xml file over HTTP.

The script then parses the received XML file and based on the sensor ID, reads the current value into an internal variable.

This value can be a temperature (°C / °F / °K) or a humidity (%RH), depending only on the sensor ID – for the Lua script, it is only a number. If the STE2 thermometer is not online or its probe is disconnected, the value is invalid and the Lua script sets the value to the state defined in the “invalidState” variable at the beginning of the script.

 

The measured value is compared to the range (lowLimit, highLimit), and if it is within this “safe range”, the output is set to “validValue”.

 

Sometimes the measured value fluctuates around the safe range limits. To prevent frequent switching, which is undesirable e.g. for back-up A/C, hysteresis can be set.

For example, if the back-up A/C should be turned on whenever the temperature is outside the 0 to 30°C band and the hysteresis is 2°C, the A/C turns on as soon as the temperature exceeds 30°C but only turns off when the temperature drops below 28°C. 

 


STE2 Web Thermometer

Recommended 3rd party device

Manufacturer: HW group s.r.o. (Czech Republic)

Product: STE2: WiFi a Ethernet thermometer with Digital Inputs
Temperature and humidity sensor with WiFi and two digital inputs for dry contacts. Connects to the network via WiFi or Ethernet with PoE (802.3af) support. STE2 is supplied with a power adapter and a temperature probe. STE2 WiFi and Ethernet thermometer supports SNMP. STE2 online thermometer can be connected to the SensDesk online portal or to a SMS gateway


 

Supported devices: 

NETIO 4AllNETIO 4CNETIO 4

 

Communication between NETIO 4x power socket and STE2 thermometer

 

  1. NETIO 4x sends a CGI request to STE2
  2. STE2 sends a xml response
  3. NETIO 4x parses the required value from the xml response, compares it with the variable and switches the output if needed

 

Example of the STE2 xml response:

  • Komunikace mezi NETIO 4x a STE2

 

  • Highlighted is the sensor ID and its value. The value is only a number, without a measurement unit. The code will work the same way for any unit (°C, °F, %RH,...) 

 

Connecting NETIO 4x electrical power socket and STE2 thermometer

 

LAN connection

  1. Connect STE2 with an Ethernet cable to your Local Area Network.
  2. Connect NETIO 4x with an Ethernet cable to your Local Area Network.

 

NETIO power socket as Wi-Fi Access Point

  1. Connect STE2 with an Ethernet cable to your Local Area Network.
  2. Connect NETIO 4x with an Ethernet cable to your Local Area Network.
  3. Switch NETIO 4x to the Wi-Fi Access Point mode.
  4. Connect STE2 to the network created by NETIO 4x.

 

Determining HW Group sensor ID

  • After connecting STE2 to your network, find its IP address using the HWg-Config utility
  • Po zapojení STE2 do vaší sítě zjistíte jeho IP adresu pomocí HWg-Config
  • Sensor ID is shown in the STE2 web interface in the SENSORS section, in the ID column (see the picture).   
  • If a wrong sensor ID is specified, the error is written into the NETIO log and the action is terminated.

Creating the Lua scripting rule

To create and run the Lua script in NETIO inteligent power socket, do the following:

1) In the Actions section of NETIO 4 web administration, click Create Rule to add a rule.

  • Funkční Lua script, kterým lze propojit chytré zásuvky NETIO 4x a teploměr STE2

 

2) Fill in the following parameters:

  • Enabled: checked
  • Name: STE2 (user-defined)
  • Description: Temperature monitoring (user-defined)
  • Trigger: System started up
  • Schedule: any (does not affect the function in this configuration)

 

3) Copy the following code and paste it into the field for the Lua script:


------------Section 1------------
local output = 2 -- select output number (1 - 4)
local validValue = 1 -- select state for value in safe range
local invalidState = 0 -- select state for invalid values
local lowLimit = 25 -- select lower limit
local highLimit = 30 -- select upper limit
local hysteresis = 2 -- select hysteresis
local interval = 1 -- choose the period between controls (seconds)
local sensorID = 36951 -- sensor id
local ipSTE2 = "192.168.101.165" -- ip of your device
---------End of Section 1---------

local temp = 0 -- inner variable, do not change

local function call() -- send request on STE2
  cgiGet{url="http://" .. ipSTE2 .. "/values.xml", callback=getTemperature}
end


local function control() -- checking temperature
  local state = ''
  if validValue == 0 then
    state = 'off'
  else
    state = 'on'
  end
  if temp == -1000 then
    devices.system.SetOut{output=output, value=invalidState}
    --devices.system.SetOut{output=4, value=true}
  else
    --devices.system.SetOut{output=4, value=false}
    if devices.system["output" ..output.. "_state"] == state then
      if (temp<lowLimit or temp>highLimit) then
        devices.system.SetOut{output=output, value=1-validValue}
      end
    else
      if (temp>=(lowLimit+hysteresis) and temp<=(highLimit-hysteresis)) then
        devices.system.SetOut{output=output, value=validValue}
      end
    end
  end
  delay(interval,function() call() end)
end

function getTemperature(o) -- parse temperature info from xml response
  if o.result == 0 then
    local myxml = o.buffer
    if xml.check(myxml) then
      local parsed = xml.parse(myxml,xml.STRIP_PREFIXES)
      local nmbOfSensors = # (parsed.get("SenSet").children()) -- gets number of sensors
      local sensor = parsed.get("SenSet","Entry")
      for i=1,nmbOfSensors do   -- iterates through sensors
        idVal = tonumber(sensor.get("ID").text) -- gets sensor id
        if sensorID  == idVal then -- check sensor
          temp = round(sensor.get("Value").text)
          control()
          return
        else -- next sensor
          sensor = sensor.next
        end
      end
        logf("Can´t find sensor with ID: %d, shutting down protocol", id)
    else
      log("Invalid XML, next attempt in 10s.")
      devices.system.SetOut{output=output, value=invalidState}
      --devices.system.SetOut{output=4, value=true}
      delay(10, function() call() end)
    end
  else
    log(string.format("CGI get failed with error %d: %s. Next attempt in 10s.", o.result, o.errorInfo))
    devices.system.SetOut{output=output, value=invalidState}
    --devices.system.SetOut{output=4, value=true}
    delay(10, function() call() end)  
  end
end

local function round(s) -- converts string to number and rounds
  local n = 0
  local tenth = 0
  for i=1,string.len(s) do
    local c = s:sub(i,i)
    if c == "." then
      tenth = tonumber(s:sub(i+1,i+1))
      n = tonumber(s:sub(1,i-1))
    end
  end
  if tenth >= 5 then
    if s:sub(1,1) == "-" then
      n = n-1
    else
      n = n+1
    end
  end
  return n
end

log("Temperature monitoring started")
call()

 

4) To finish the rule creation, click Create Rule at the bottom of the screen.

  • Script lze upravit i pro jiná zařízení a data ve formátu xml.

 

Method of operation

  • The temperature and humidity values measured by the STE2 and its digital input states are available in its web interface. There they can be checked.
  • NETIO sends an http request and STE2 sends back a values.xml file with sensor values.
  • The Lua script looks for the configured sensor ID and processes its value.
  • The script parses the sensor reading for the given sensor ID and rounds it to an integer.
  • If the sensor reading is within the safe range interval, as specified by the lowLimit and highLimit variables, the output specified in the output variable is set to the validValue variable (set in the script header).
  • If the sensor reading is outside of the safe range, the specified output (socket) is set to the opposite of validValue.
  • The output is set to validValue again as soon as the value returns to the safe range. 
  • To avoid frequent switching when the value oscillates around one of the thresholds, use the hysteresis variable to set a hysteresis within the interval.

 

Special situations:

  • If the STE2 online thermometer or the network connection fails, a timeout occurs after 30 seconds, the output is switched to invalidState and NETIO tries to reconnect every 10 seconds.
  • When the probe is disconnected (STE2 reports a value of -999.9), the output is switched to invalidState (just as in case of timeout). When the connection is re-established, the script continues to run normally.
  • The action is started whenever the NETIO device is powered up.

 

Hysteresis

  • The hysteresis variable in the script sets the hysteresis. The hysteresis defines the width of the insensitivity band when one of the limits is exceeded. It thus prevents frequent switching when the temperature fluctuates around the limit. The following is an example for limit temperatures of -10°C and 25°C respectively and a hysteresis of 5. For setting the output, blue parts indicate that the value considered to be within the allowed range. Red parts indicate that the value is considered to be outside of the safe range.
  • Hystereze se nachází uvnitř intervalu a definuje velikost pásma necitlivosti při překročení jednoho z limitů.

 

Setting the variables

  • output
    • Specifies the socket to control.
    • Example – to control socket 1: output = 1
  • validValue
    • Specifies the socket state to set when the value is within the specified interval.
    • To switch off the output when the value is within the interval, set validValue = 0; to switch it on, set validValue = 1
  • invalidState
    • Specifies the socket state to set if there is an error (e.g. probe torn off, STE2 power failure, network failure etc.).
    • To switch the output off, set invalidState = 0; to switch it on, set invalidState = 1.
  • lowLimit
    • Specifies the lower limit of the SafeRange interval. If the value (temperature) is lower, the output is set according to validValue.
    • For example, to specify 20(°C,°F,%RH,...): lowLimit = 20
  • highLimit
    • Specifies the upper limit of the SafeRange interval. If the value (temperature) is higher, the output is set according to validValue.
    • For example, to specify 35(°C,°F,%RH,...): highLimit = 35
  • hysteresis
    • Specifies the hysteresis. For more details, see the Hysteresis section.
    • For example, to specify a hysteresis of 1(°C,°F,%RH,...): hysteresis = 1
  • interval
    • Specifies how often to check the value from the STE2 device.
    • For example, to check every 2 seconds: interval=2
  • sensorID
    • Specifies the sensor ID for reading the value (see above).
    • For example, to read the sensor with ID 2730: sensorID = 2730
  • ipSTE2
    • Specifies the IP address of the STE2 device. The IP address can be found with HWg-Config (see above).
    • It is a string, so it has to be in double quotes.
    • If the STE2 device uses a HTTP Port different from the default 80, the port needs to be specified after a colon.
    • Example (port 80): ipSTE2 = "192.168.101.165"
    • Example (port 8080): ipSTE2 = "192.168.101.165:8080"

 

 


STE2 – 3rd party product

STE2 web thermometer with LAN or WiFi connectivity is produced by HW group s.r.o. (Czech Republic).

The STE2 package from the manufacturer includes a temperature probe that is used in this Application Note. The probe port can be also used to connect a humidity probe; this example will work with it as well.

The green digital inputs are used to connect a button or a switch. These inputs can be connected to a “dry contact” (a voltage-free contact) such as a mechanical button or a switch; HW group calls such devices  “detectors”. NETIO 4x can react to the activation or deactivation of a digital input; for details, see NETIO AN14.

For reacting to STE2 failures, see NETIO AN09 – the network presence of the device can be checked by sending PING requests to its IP address and a NETIO socket can be switched on if there is no response. 


 

FAQ

 

1) Is it possible to connect the devices even without an Internet connection?

Yes, STE2 can connect to a WiFi network created by a NETIO4x in the Wi-Fi Access Point mode (NETIO Configuration mode). See Connecting NETIO 4x and STE2.

 

2) Is it possible to control several outputs of a NETIO device with this method?

Yes. All output control takes place in the control function. There, any output behavior with respect to the value of the temp variable can be programmed.

 

3) Is it possible to check the temperature more often than once per second?

No, one second is the minimum.

 

4) Is it possible to monitor other values besides temperature? Such as air humidity or digital input states (0/1)?

Yes. The temperature is used only as an example. Any value provided by the STE2 can be read. The script only takes the value and not its unit; it makes no difference if the value is in °C, °F or %RH. See the xml example. For working with digital inputs, see NETIO AN14.

 

5) Can I read several probes connected to the STE2 at the same time?

Yes. However, to keep things simple, we recommend creating another script with a different sensor ID. Values can be passed between scripts within a single device using global variables. Reading several variables in a single script is also possible but requires more changes to the code.

 

6) Can I use this rule to read the temperature from a Poseidon2 device of the same manufacturer (HW group s.r.o.)?

Yes, the code will also work for Poseidon2 devices, the xml files are very similar. Tested with Poseidon2 3266, firmware version 1.4.8.
 

7) Is it possible to detect a torn off / malfunctioning STE2 probe?

Yes, the output is set according to the invalidState variable. If a probe is not working, STE2 reports -999.9 as its value (NETIO works with a rounded value of -1000). If you want to change the behavior when a sensor fault is detected, you can do so in the control function below the if temp == -1000 condition. 

 

8) What happens if I restart one or both devices?

When the STE2 is restarted, the output is set according to the invalidState variable. When the device is switched on again, the temperature monitoring will continue. When the NETIO 4x smart sockets device is restarted, the action is started again after power up. When the NETIO device is off, the script does not run and the temperature is not monitored. When both devices are turned off (e.g. due to power outage) and then back on, the sockets will again start reacting to the sensors.

 

9) Is it possible to react to STE2 digital inputs?

Yes. For the code and a detailed description, see NETIO AN14.

 

10) Is it possible to detect faults (STE2 failure, torn off probe, ...) and signal them by switching another outlet on or off?

There are 4 commented-out commands in the code. These commands turn on socket no. 4 in precisely these situations. They are:

--devices.system.SetOut{output=4, value=true} and --devices.system.SetOut{output=4, value=false}. Simply uncomment these commands. Two of them are in the control function and two other in the getTemperature function. By changing the number in the output parameter, the socket no. 4 can be changed to any other socket.

 


Supported NETIO 4x firmware versions:

3.0.0 and higher (Firmware archive)


Keywords:

XML, cgiGet, XML API, HTTP request, xml parser, temperature, HW GroupSTE2, WiFi, NETIO controlled by temperature

 

Ask for a price or technical parameters

For device testing use name/password demo/demo