[HOW-TO][Android] Create a custom widget and populate it with real-time data from your openHAB System

Hello together,

I have been working on a custom widget for my openHAB instance to display data in realtime on my android homescreen without having to open another app. After much trial and error and testing I found a solution that works great for me and I want to share it with you.

What you’ll need

  1. An Android phone obviously, in my case a Galaxy S10 with Android 11 running.
  2. The app ‘Automate for Android from Llama Labs’ (free for a certain amount of running automation blocks, didn’t hit the limit, purchased premium anyway because the app rocks)
  3. KWGT Kustom Widget Creator
  4. Pushover to send Push messages from openHAB to Android (one time payment of about 5€, totally worth it imho, per API-key 7500 msg/month free, you can generate multiple API keys)

How it works - Overview
When an Item in my openHAB that I want real-time updates from changes, I send a push message with a certain title and message to my phone. The App Automate checks if there is a new notification from Pushover, parses the info, sends a broadcast to my KWGT widget, which updates its info and then Automate deletes the notification again.
The part with Automate is very energy saving, because it awaits the transition of the event ‘there is a new push message’ and doesn’t poll it. The fiber (basically a program loop in Automate) sleeps until the event occurs. The pushing happens pretty much instantaneously.

How it works - Detail
Let’s assume you have a working API key from pushover, installed Automate and created a basic widget with KWGT which can display a number, let’s say the temperature.
The order of explanation is openHAB → Automate → KWGT.

openHAB
First, you’ll have to install the Pushover Action and configure it. There’s enough documentation, so I’ll won’t go into detail here. My rule for an item change is as follows:

rule "temp bathroom"
when
	Item temperature_bathroom changed
then
    pushMSG.apply("tempBathroom",temperature_bathroom)
end

and the pushMSG is defined as follows:

val pushMSG = [ String title, GenericItem item|
	if(now.getHourOfDay()>=6 && now.getHourOfDay()<=23)
    {    		sendPushoverMessage(pushoverBuilder(item.state.toString).withApiKey(<Your_API_Key>).withTitle(title).withPriority(-1))
    }
]

The Priority(-1) let’s the notification stay in the background and be quiet, you wouldn’t want a sound every time a temperature gets an update. The time case makes the system only push messages when I am actually awake. The title in this example is “tempBathroom”. This is what the Automate Script on the Android device will be looking for.

Automate
I have attached an exported PDF of the flow. If there is interest, I can export the flow as it is for importing, but for visualizing what it does, the PDF should suffice.Get Openhab Infos.pdf (261.4 KB) As you can see, Automate waits for a new notification, checks the title (separated in “temp”, “hum”, “tempBath” for temperature/humidity values, stLR, stFlur etc. for states of controllable lamps and winWC, winBathroom etc. for window sensors).
Then it sends a broadcast (inter-app-communication on Android) to KWGT with the corresponding title and value:


KWGT can receive these broadcasts, store them in so-called globals and change values according the these globals.
In Automate, the notification is then cancelled and, in case of temperature and humidity,another broadcast with the time of the value update is sent. With that I can see if the sensors are working correctly (if there was no update for too long, there would be a problem).

KWGT
How to create a widget in KWGT would go beyond the scope of this post. Lets just say you have widget with some text and the text should display your temperature. The steps to achieve this are:

  1. Create a global, type text, name it something you will find again later (e.g. tempBath)
  2. Click on the checkbox on the right and then on the three dots on top. There, you can select Formula (in German it’s ‘Formel’)
  3. Click on the global and in the Formula Editor write
$br(tempBathroom,val)$

. This means any Broadcast with the title “tempBathroom” will have the value of its variable “val” written into this global.
4. on your text that should display the value, you’ll have to enable formula as well and there you need to write

$gv(tempBath)°C$

This means the value of the global “tempBath” is written into this text field and a °C is appended.

There you go, let’s try it out. Just use Pushover from their website to send a message with the needed parameters and check if Automate is doing what it should and if KWGT is receiving the boradcast.

Concerning KWGT: there is much, much more that you can do design-wise with KWGT. I’ve attached my current widget (the white rectangles are the names of my children, don’t want that on the internet)

if you have any questions, feel free to ask, I’d be glad if someone has use for this.

Best wishes,

Christoph

2 Likes

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.