Save JPEG URL into item image

Hi Community,

I’m trying to get a JPEG from an URL into an item type image and I’m not able to find the correct way to perform this.
I was thinking that this is quite standard but looks like it is not.

I receive for example the following if I try to postUpdate the URL into the image item.

Cannot convert 'http://<picture-url>' to a state type which item 'pic_jpeg' accepts: [RawType, UnDefType]

Looks like I have to convert the data in base64 or so before but any idea how to perform this?

Best regards
Timmi

Which scripting language? RulesDSL?
If RulesDSL, you’ll need to grab the image using HTTP action first. If you need further help let us know.

In jruby:

Image_Item.update_from_url "http://<picture-url>"

this is a good question.
I use the old fashion rule files.
I guess this is the RulesDS, right.

Any help is really welcome. :wink:

import java.net.URL

rule "test1"
when
  // Time cron '* * * * * *'
then
  val imageUrl = "https://www.openhab.org/demo/openhab-logo-square.png"

  val url = new URL(imageUrl)
  val connection = url.openConnection()
  val contentType = connection.getContentType()
  val bytes = connection.getInputStream().readAllBytes()
  val image = new RawType(bytes, contentType)
  TestImage.postUpdate(image)
end
1 Like

Yes this might work. The only issue might be that I need to authenticate with the server. It is a basic authentication and it is not working if I just write user:password inside the URL.

Thank you already so much

I’m just googling this stuff :slight_smile:

import java.net.URL
import java.util.Base64

rule "test1"
when
  // Time cron '* * * * * *'
then
  val imageUrl = "https://www.openhab.org/demo/openhab-logo-square.png"
  val username = "username"
  val password = "12345"

  val url = new URL(imageUrl)
  val connection = url.openConnection()

  val userpass = username + ":" + password
  val basicAuth = "Basic " + new String(Base64.getEncoder().encode(userpass.getBytes()))
  connection.setRequestProperty("Authorization", basicAuth)

  val contentType = connection.getContentType()
  val bytes = connection.getInputStream().readAllBytes()
  val image = new RawType(bytes, contentType)
  TestImage.postUpdate(image)
end
1 Like

Thank you very much for your google help. :wink:

Best regards
Timmi