Use default map file if room.map file does not exist

I am trying in my light rules to use map files, which works fine. However I need to do some check if the map file exist: Livingroom.map

if does not exist I would like to use the default map file: “Default.map”

is there an easy way to do this in the rules? Some dir commands or something?

val sceneGroup = ScriptServiceUtil.getItemRegistry.getItem( "Group_"+roomName +"_Lights_" +transform("MAP", roomName+".map", ""+triggeringItem.state) ) as GroupItem
     

More info here:

Assuming you are running on Linux you can use executeCommandLine:

    val fileExists = executeCommandLine("ls /etc/openhab2/transform/"+roomName+".map", 1000)
    val mapFile = if(fileExists.endsWith("No such file or directory")) roomName.map else "Default.map"

In JSR223 Python you can use:

from os import path
...
    mapFile = ("{}.map".format(roomName) 
               if path.exists("/etc/openhab2/transform/"+roomName+".map") 
               else "Default.map")

You can use the native Java classes:

import java.io.File
...
    val f = File("/etc/openab2/transform/"+roomName+".map")
    val mapFile = roomName+".map" if(f.exists()) else "Default.map"

If using JSR223 Rules, you can instead encode the map files on the Items themselves as metadata and potentially sidestep this issue entirely.

1 Like

I will incoroperate the java version, because that is then platform independent in my light tutorial. I will update the tutorial with this new feature and let you know about it.

1 Like