Display blocks of text in UI received from MQTT or from executed pyhon script

Hello.
I am very new to openHAB 3 but have a fair amount of experience in programming
I have a number of MQTT devices that publish their data and I can pick them up in openHAB and diisplay the values in the UI. I can also publish from from openHAB to trigger events in my MQTT nodes
I also have a database on the RPI server (opeHAB and mosquitto broker live here too) that is accessed by a long running script to store all sorts of values from MQTT and other sources
What I want to do is to be able to extract data from the database and present that in the openHAB UI in a formatted way.
The first thing that springs to mind is to have openHAB trigger a python script that will do all the database queries and any other data gathering needed, then format that data in html, return it to openHAB and display that inside one the UI elements on openHAB.
I say html because that is a neat way to format the data easily and present it as well as being very flexible
The html displayed could run into many lines of output
I have searched for days but cannot see a way of doing this
Is there a way of doing this ?
Is there a better way to achieve what I want to do ?
Can I also achieve that by publishing this html block of code via MQTT and display it that way
I am really looking for guidance on the best way of achieveing the above and how to implement this
Many thanks in advance for your help

Sure, call your external script using the Exec binding or executeCommandLine Action.

Probably the easiest would be to have that external script write out to an HTML file and put that file in $OH_CONF/html. It can be accessed via http://<ip or host for OH>:8080/static/<filename.html>. Use a webview widget to show it.

But given that this is all pretty much external to OH anyway, I’d probably just use a system cron job to generate the HTML file periodically. I wouldn’t mess with doing this from inside OH unless OH where going to parse the results and put them into Items for some purpose. And if that were the case, I’d use JSON and an MQTT message instead of HTML.

You cannot send HTML to OH and show that as a webpage or part of a webpage. All you’d see is the raw text of the HTML, not the rendered HTML.

Hi Rich
Thank you ever so much for such a speedy response. Really appreciated
Yes, creating the html “snippets” via crontab or my long running python script is sort of what I had in mind
Thanks for the pointer to the webview widget. I will go and look that up
So are you saying that I could not send HTML via MQTT and display that ?
Could I send multiple lines of data via MQTT and display that as a “block of data” respecting the line ends somehow so that it is not just a long string that wraps around within the disply UI element ? (obviously html is better as yoiu can style that where as plain text is just flat without styling)
Using the webview widget, I assume it would use the CSS styling from openHAB itself ? If so, that is probably good as it will keeping things within the “theme” of what openHAB is using . If I chose to, I assume I could add my own CSS and if I did, would these be “restored” to the openHAB defaults when my webview widget ends ?
Many thanks Rich

Don’t forget, this is a home automation environment. It’s meant for “Fans ON, Temp 21.5C, Lights scene ‘E’”. UIs are set up for knobs, graphs and icons, not so good at publishing paragraphs of text.

Ultimately, you could accept blocks of random text via MQTT, treat as though it were HTML, write out a little HTML page, and insert that in the UI. But it’s not a thing many people want to do.

Is this block of text useful to users, like a weather report, joke of the day? Or just a developers tool, or …? Would guide how much effort you want to put into it.

Thanks Rich
Yes, I do appreciate that it is mainly for IOT not display of text
As an example, one of the things I would like to achieve is as follows.
On the home page I want to show a summary of all my nodes (mainly MQTT) and the last time they were active so that I can spot quickly if one of them has gone down.
I could build multiple items that subscribe and display that, but it seems simpler for my script to collate that summary data for all nodes, do all the formatting in HTLM as that is easy to do there and present that in OH. I also would only have to change all that in one place as things change rather than multiple places in OH.
Also, I would like to be able to create one item with formatted data . Say for example the mqtt data json packet contains temperature, last update time, etc… and I wanted to show that in the OH display item for that node as …
node temperature xxxx ==> perhaps slightly bigger font and then on next line
last updated on yyyyy ==> in a normal size font, and perhaps in red if it is older than say 5 minutes
I cannot see how I could do this with MQTT … probably because I am still very new at this. Is this possible to format like that with conditions, etc … ?~
Many thanks Rich

You could, but it won’t render. You can display

<html>
  <header>....
</html>

as text from an Item. You can’t load up an Item with HTML and have OH render that.

You can do pretty much anything you want in that respect. But you’ll probably need to create a custom widget to display it reasonably. And by the time you’ve munged together a widget that can both parse JSON and use it as you want, you’ve done more work had you used Items in the first place.

No, I think it might be implemented as an iFrame or something like that. It’s not embedded in the page, it’s loaded into the page. It’s mostly used for things like showing a camera feed from an IP camera or the trash pick schedule from an external website and stuff like that.

I don’t know because this isn’t the sort of thing that OH is designed to handle. It works with its own Items and its own database information. It’s not designed to be a generic display to show stuff that is pretty much wholly outside itself. That’s not it’s job.

There are tons of ways to do this that do not involve an external Python script and a table of text. Presumably these are devices that are important to OH so it’s probably already subscribed to those topics so an expire timer on those Items could be used if it doesn’t report after a time. The devices should have a LWT so that an OFFLINE message is published when the MQTT Broker sees that it went offline. OH can see that and set the MQTT Thing as OFFLINE which can in turn update the Items as appropriate.

Rarely do such short cuts that seem simpler end up being simpler in the long run. When using Groups, the semantic model, and widgets like the oh_repeater widget, you’d only need to change it in one place in OH too.

The currency in OH is Items. If it’s something that is worth displaying in OH, it’s also probably something worth OH acting upon (e.g. send a Telegram message when a node goes offline, turn on a heater when the temp sensor gets too low, etc). To achieve that, each sensor and actuator needs to be its own Item. If all you want to do is display a bunch of information from various sources, some other product is going to be a better fit for you. If you want to do some automation around this information, it must be broken up into individual Items.

You can easily do this in OH. But you’ll need three different Channels for each piece of information, link those three pieces to three separate Items, and then create a custom UI widget that displays those three Items with the style and formatting you desire.

And once you’ve done that, not only can you display it, you can have OH actually do something with them. Remember the “A” in openHAB stands for “automation”.

Thank you Rich
A lot of good information for me in your response.
I will now go away and digest that and read up about all those things
It is very encouraging that all of that can be done via openHAB.
Obviously my knowledge of openHAB is still rather low, so I will study all those areas yo mehtioned a lot more.
Thank ou again for your time and effort. Much appreciated

Just a quick question …
I have started researching more based on your very informative response.
I get the impression that you can only use the webview widget in a sitemap and that a sitemap cannot be displayed on a browser web OH screen and that you need to use an OH app if you want to display webview widget. (ie like android OH app, etc…)
Have I missed something here and that I can use webview widgets in a normal layout page accessed via a web browser ?
Many thanks

Sitemaps can be displayed on phone apps or using BasicUI (a separate UI add-on that can be installed) in the browser. It’s a completely different UI from MainUI which is what you get by default.

MainUI has the oh-webframe widget used to display another web page in a frame in a MainUI layout page. MainUI can be displayed in a browser or the Android app. I think iOS app support is coming soon but MainUI is a PWA so adding it to your homescreen works nicely.

I don’t know what HABPanel has for displaying other web pages. It too is supported in the browser or the android app (don’t know about the iOS app).

Thanks Rich.
That oh-webframe card was in the card list and I did not spot it even though I looked at that list many times.
Yes, that works very well indeed
Many thanks for your help to point me in the right direction

Now that I have the webframe working, I would like to trigger the html to be created by publishing a MQTT topic that my python script will pick up and generate the html for the webframe.
Ideally I would like a “click button” of sorts, but there does not seem to be one in the list of cards
If I use a toggle switch, I can get the MQTT publish to happen, but not visually ideal as this is not really a toggle function, but a click button function
I have tried to use a “string” card but whatever I do I cannot get it to actually publish the MQTT message, although everything seems to be set up the same as the toggle setup.
Is there somewhere a proper “button” card that is available and if not, is there a way to use the “string” card to actually publish the MQTT message ?
Many thanks

See

From UI, you can send an arbitrary string to an Item as command.
What happens to it then depends on what bindings or rules it is linked with.

If you are looking to have your string embed details from other Items or calculations etc., it’s almost certainly better to process that in a rule rather than trying to build complex strings in the UI widget.

Thanks for that Ross
Good to hear that it should work with any of the cards
Perhaps me mentioning button confused what I was trying to explain
I just want to trigger an action, and it does not have to be a button as such
I have it working with a switch card but not with a string card

This is exactly what I was trying to do
I initially setup a switch card and that works fine and the MQTT message is published and everything works fine, but does not make visual sense as this is an on/off scenario but an MQTT push to control something at the other end, ie take a picture from the camera
If I change that from the switch to a string card, with everything setup up the same as for the switch, the MQTT publish does not happen when I click on the string and I cannot see why that is
Currently, the MQTT publish does not need a payload as it is just the subscribed topic that causes the camera node to take a picture and publish the image which is then picked up by my OH page in an image card

The only action you can trigger from UI is to command some Item.

Neither can we. Look in your events.log to see what command is sent to the associated Item.

I just checked the event log and nothing is sent when using the string card
When using the switch card it works fine and I get the following in the event log

  • openhab/items/RovingCameraTakeAPicture/statechanged

ItemStateChangedEvent

{“type”:“OnOff”,“value”:“ON”,“oldType”:“OnOff”,“oldValue”:“OFF”}

  • openhab/items/RovingCameraTakeAPicture/state

ItemStateEvent

{“type”:“OnOff”,“value”:“ON”}

  • openhab/items/RovingCameraTakeAPicture/statepredicted

ItemStatePredictedEvent

{“predictedType”:“OnOff”,“predictedValue”:“ON”,“isConfirmation”:false}

  • openhab/items/RovingCameraTakeAPicture/command

ItemCommandEvent

{“type”:“OnOff”,“value”:“ON”}

There is no such thing as a string card, so I assume that you mean an oh-label-card. Take a look at the label card doc page, you’ll see a section named Action. This is a list of all the things you can configure the label card to do when you push on it. You’ll note that most of them just impact the widget/interface (i.e., open popup, navigate, variable etc.). Only a few of these can actually directly impact something in the core system: command, options, toggle, and rule.

It sound like you want the command option which will send the specified command to the specified item (and it does not have to be the same item that the card is displaying in the label). You’ll need to only add two configuration options beyond Send Command: Action Item and Action Command:

Thank you Justin
Yes, I meant label card
That is pretty much what I have for both options, but with switch it works and with label it does not
Here is what I have defined

Edit label cards
image

take a photo item

Take a photo channel

What command are you sending to your item? That’s not shown in the top image.

If you have Action, Action Item, and Action Command all configured, now what does the event log show when you click the label card?

Does the state of the item have to reset to some other value between commands (does your item have to register a change instead of just receiving a command)? If so, how are you accomplishing this reset?

What’s the Action Command? What command should the widget send to the Item when the widget is clicked on? You’ve cut that field off in your screenshot. My understanding is that is not optional. You have to define the Action Command too, not just the Item.

Remember, commands are what causes openHAB to send messages out to the devices. But a command has to be something. There is no ‘’ command. In the MQTT case, a String Item would be linked to a text Channel so the command will be the actual contents of the message published to the MQTT topic.

Thanks Justin / Rich

In the case of the switch version that works, there is no place to provide an action command and that works
In the case of the label version that does not work there is an option for the action command but that I left blank as I did not need that for the switch version.
There is no text to be sent in the MQTT payload as the camera node just reacts to the topic and does not check for the content since receipt of the topic is sufficient to trigger the camera and indeed works fine with the switch version
I just tried to add some text to that action command but it made no difference and teh event log shows nothing and the generic MQTT client I use for testing shows nothing being published