Upload image to Item of type Image via REST API

I’m using openHAB version 3.3.0 and want to upload a image to a item of type ‘image’ using python and the REST API. I searched the forum, but couldn’t find the solution.

What I learned, is that the image has to be base64 ecnoded, I did that. And I tried via command and state to transfer the image.

my_stringIObytes = BytesIO()
plt.savefig(my_stringIObytes, format='jpg')
my_stringIObytes.seek(0)
my_base64_jpgData = base64.b64encode(my_stringIObytes.read())
url = 'http://192.168.178.3:8080/rest/'

res=requests.put(url+'items/NewItem/state', my_base64_jpgData,headers= {'content-type': 'text/plain'})
print(res)
res=requests.post(url+'items/NewItem', my_base64_jpgData,headers= {'content-type': 'text/plain'})
print(res)
res=requests.put(url+'items/NewItem/state', my_base64_jpgData,headers= {'content-type': 'image/jpeg'})
print(res)
res=requests.post(url+'items/NewItem', my_base64_jpgData,headers= {'content-type': 'image/jpeg'})
print(res)

For that I get 400, 400, 415, 415 HTTP response codes =(

What did I miss?

Often there will be additional information in openhab.log.

According to the REST API Docs (in MainUI go to Developer Tools → API Explorer) the content-type should be text/plain. That’s why you get the 415 error code when you try to use image/jpeg.

Beyond that :person_shrugging: . It’s somewhat unusual of a use case to push an image to an Item through OH’s REST API. Usually if you have an image, instead of using an Item you’d drop that file in $OH_CONF/html and access the image in your widgets using /static/<name of image>.

The image is already in $OH_CONF/html and I link to it via my local ip - but I also want to access the image via home.myopenhab.org so from outside of my network! Therefore I thought this is the right way to push the image to an item and the items should be available also from remote.

Am I off course here?

PS_ I did this to the logs, log:set INFO org.openhab.core.io.rest but I do not see any useful logs. Not sure if that is the right module either.

I have an image in my $OH_CONF/ html folder. On a page or sitemap I refer to it by:
http://myopenHABserver:8080/static/mypicture.png’.

That way I do get the image when calling it from within the LAN and via internet ( using myopenhab).

Yes. Because you’ll be accessing the image through openHAB itself, myopenhab.org can serve it. It’s only if that image were served by some other service on the LAN that you’d run into problems. As long as the image is in $OH_CONF/html, you can get to it through myopenhab.org.

1 Like

a got it, I changed my urls from
http://<localip>/static/my.jpg
to this
/static/my.jpg
now myopenhab.org supports my use case.

I am curious, for me it works outside the LAN only with the above posted URL. Can’t say why.

To use a relative path that will work both locally and through myopenhab.org, make sure to use /static/<nameoffile>. Nothing more. No “http://” or anything like that. The browser will fill in that part for you.

1 Like

:thinking:
Weird, using the short version (static/mypicture.png) isn’t working for me, not even in the LAN. I tried it on an oldstyle filebased sitemap as well as a OH3 UI-created sitemap.
The used line in the sitemap-file is:
Image url="http://opuspi4:8080/static/meteogramm.png"
Which looks the same as stated in the example in the Sitemap Documentation.

Am I missunderstanding something completly or is my system somewhat corrupted?

It must be /static/mypicture.png. It has to start with the /.

Ups, stupid typo in the post! I tested with /static/mypicture.png.

Then I’m not sure why it wouldn’t work. I know for sure it can work that way, at least in MainUI. I used to use something similar in my sitemaps back on OH 2 as well (I’d generate snapshots of my Grafana charts as images).

All the images you see here used as the background of the locations cards come from the $OH_CONF folder and are configured using /static/imagename.png/

component: oh-location-card
config:
  backgroundImage: /static/home.jpg
  backgroundImageStyle:
    filter: brightness(60%)
  badges:
    - presence
    - temperature
    - humidity
  invertText: false
  subtitle: Wuthering Heights
  title: Home

I’m not sure how this could be going wrong for you but I’ve not use sitemaps for quite some time so perhaps it works differently now.

My usecase is a MainUI created Sitemap, the code is:

sitemap page_7dc56fe3dd label="Wetterkarte" { Image icon="temperature" label="Wetter" url="http://opuspi4:8080/static/meteogramm.png" refresh="60000" }
Which gets sort of parced by the system to:

Your sitemap definition looks valid.
{
  "uid": "page_7dc56fe3dd",
  "component": "Sitemap",
  "config": {
    "label": "Wetterkarte"
  },
  "slots": {
    "widgets": [
      {
        "component": "Image",
        "config": {
          "icon": "temperature",
          "label": "Wetter",
          "url": "http://opuspi4:8080/static/meteogramm.png",
          "refresh": "60000"
        }
      }
    ]
  }
}

The difference seems to be the use of quotes for the url ( removing them didn’t do the trick).

Regarding the oldstyle sitemap files, I was never able to make it work in the short version.

To answer the original question:

You have to send the image like this:

data:image/png;base64,BASE64_ENCODED_IMAGE

or use HABApp if you want to do python stuff - I figured most of the stuff out there . :wink: