CSV file reading


(stuart harvey) #1

is there any way of reading a CSV file in to openhab and changing a set point at the time and day ?
the CSV file looks like

week_no,w_day,t_time,set_point
1,Monday,4:45,17

i had a python script that looked like

if row[‘week_no’] == current_week and row[‘w_day’] == today_day and row[‘t_time’] == current_time:
heat_setting = row[‘set_point’]

i had tried to alter the DHT reading python script but it hung on connecting to MQTT and not sure why was look to see if there`s a better solution for this rather than asking for help with python script

Thanks Stuart


(Ohad Eytan) #2

You can do it with Node-Red:


(stuart harvey) #3

is it be possible to call a python program every minute ? maybe easier to alter 1 program that connects to the mqtt server already but it need to be done with sys arguments from a setting within openhab ie week number that`s the only one that needs to be passed in with set_point coming out


(Rich Koshak) #4

There are several ways. Assuming that the CSV always looks like this you can get the last value using:

val setpoint = executeCommandLine("cat /full/path/to/file.csv", 1000).split(",").get(7)

The “correct” solution though would be to change the python script generating this output to use something more parse-able like JSON or XML and then you can use standard transformations.


or


(Patrik Gfeller) #5

@stuart22
Hi Stuard,

not sure if I understood your question correctly. But it is also possible to read & process a CSV directly in a rule (xtend). The below example is not perfect, but may help you as a starting point:

import java.io.File
import java.util.Scanner

val String commaSeparatedFile = "C:\\Users\\Patrik\\Dropbox\\openHAB\\Tools\\Messungen.csv"
val String ruleFile = "MBus.rules"

rule "Update M-Bus Items"
  when
    Time is midnight 
 or Time cron "0 0/1 * * * ?" // Jede Minute
  then
    val File file = new File(commaSeparatedFile);
    
    try {
      var String line = ""
      val Scanner scanner = new Scanner(file);
      
      var double energy
      var double volume
      var double flow
      var double volume1
      var double volume2
      var double temperatur
      
      while(scanner.hasNextLine()){
        line = scanner.nextLine();
        val String[] fields = line.split(",");
        switch fields.get(6) {
          case "1":
            energy = Double.parseDouble(fields.get(7))
          case "2":
            volume = Double.parseDouble(fields.get(7))
          case "6":
            temperatur = Double.parseDouble(fields.get(7))
          case "8":
            flow = Double.parseDouble(fields.get(7))
          case "20":
            volume1 = Double.parseDouble(fields.get(7))
          case "23":
            volume2 = Double.parseDouble(fields.get(7))
        }
      }
      scanner.close()
      
      ItemWarmwasserEnergie.postUpdate(energy)
      ItemHeizwasserFlow.postUpdate(flow)
      ItemVolumen.postUpdate(volume)
      ItemWasserVolumen1.postUpdate(volume1)
      ItemWasserVolumen2.postUpdate(volume2)
      ItemHeizwasserTemperatur.postUpdate(temperatur)
      
    } catch (Exception e){
      logError(ruleFile, e.message)
    }
    
  end

with kind regards,
Patrik


(stuart harvey) #6

values to be imported will depend on system time
week_no,w_day,t_time,set_point
1, Monday,4:45,17

first column is week number is set within openhab as i work a 4 week shift pattern with different days off each week
i have a rule set for changing that so every Sunday it adds 1 to the week number and another for if it =5 then resets back to 1
second is day of week
third is time that will be taken from system time
4 is the desired set point for the heating
looking for ideas on how to do this

the python script is running the temp and humidity sensor from startup and dose not get called from within openhab