Save image item's image to disk

I have an image item which I want to send the image it contains via a pushover message to my phone. The pushover action will only take a URL (local URL only) and not image data directly to use as an attachment. Is there an easy way to save an image item’s data to a file in /tmp and get the path to put into my pushover message?

Hi,

I had a similar “problem” for the OpenWeatherMap current condition icon. I use a JavaScript rule (with helper libraries) …


/**
 * Save/ persist current condition weather icon to file ...
 * The icon is available as item of type Image. 
 * The Image type is a RawType item with according methods to read the data.
 * package: org.openhab.core.library.types;
 * link: https://github.com/openhab/openhab-core/blob/master/bundles/org.openhab.core/src/main/java/org/openhab/core/library/types/RawType.java 
 * 
 */
JSRule({
    name: "save current condition weather icon to filesystem",
    description: "persists the OpenWeatherMap current condition icon image to the local filesystem",
    triggers: [
        UpdatedEventTrigger("OpenWeatherMap_Current_Condition_Icon")
    ],
    execute: function( module, input) {

        logInfo( me  + " triggered ");

        var FileUtils = Java.type("org.apache.commons.io.FileUtils");
        var outFilePath = OPENHAB_CONF + "/icons/classic/" + "currentweatherconditionicon.png";
        var outFile = FileUtils.getFile(outFilePath);


        if (isUninitialized("OpenWeatherMap_Current_Condition_Icon")) {
            logInfo( me + " --- item " + "OpenWeatherMap_Current_Condition_Icon" + " not initialized");
            return;
        }


        var imageBytes = getItem("OpenWeatherMap_Current_Condition_Icon").state.getBytes();
        FileUtils.writeByteArrayToFile(outFile, imageBytes);


    }
});


Alexander

1 Like

Thank you, I was looking for this as I am doing this currently in node-red. Could you also share your rule code for writing the image from file back to an image item? I would like to do this on startup as images cannot be persisted using mapdb as far as I know.

Hi
how do I get this to work in OH3.2 or is there any other way to store an image locally when the image item is updated?
thanks

have a look at Script execution of rule failed - array Element type mismatch that thread uses a shell script in a rule once the image item is updated and stored the image under a fixed filepath.

Thanks a lot

1 Like