Use default map file if 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:

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

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")) else ""

In JSR223 Python you can use:

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

You can use the native Java classes:

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

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