Detect if basicui is connected trigger action?

Hai,

Another question:

  • Is it possible to detect when a basicui connects (from phone / tablet / pc) and then trigger certain actions?

Use case: I have 2 solar systems of which I generate graphs as PNG files. Generating these graphs every “N” minutes ofcourse works, but there will be days when no-one ever sees these graphs. Would be nice to “generate on demand” and stop generating when no client is connected.

Ofcourse I could add a button like “generate” but that is a fall back.

Any ideas?

Greetings Matthijs

Just read a post (by @rossko57) with the info you may be looking for.

“sitemaps (which control rendering in BasicUI) do have the visibility= feature.
You can show or hide a line with visibility=[someItemstate == “alarm”] for instance. It’s a bit clumsy but can achieve something like you want.
Whole frames can appear or disappear depending on n Item state, although it is really still the same sitemap throughout.”

openhab.org

Sitemaps

a vendor and technology agnostic open source automation software for your home

Don’t think that helps, that’s about showing/hiding sections of sitemap n response to Item state changes.

openHAB has no idea if anyone is looking or not. There is so far as I know no means to tell.

I think generating every minute or pressing a button is your only choice. However, you can make it a little more efficient if you do charting periods. For example, I statically generate charts from Grafana but the charting period is controllable from my sitemap. I can choose last hour, last day, last week, last month, and last year. This goes into an Item.

Then I have a Rule that triggers when that Item changes and sets up a looping timer to regenerate the chart periodically. How often it generates the chart depends on the chosen chart period. When I’ve selected the last hour, I regenerate the chart once a minute. When last day I generate the chart every 5 minutes. When last week I generate the chart once every 15 minutes. For last month every hour and and last year I generate the chart once a day.

Only the selected time period chart is generated.

Sitemap

				Switch item=ChartVisibility mappings=[h=Hour,d=Day,w=Week,M=Month,y=Year]
				Image url="http://argus:8080/static/temp.jpg" refresh=1000

Items

String ChartVisibility "Period"

Rules

from core.rules import rule
from core.triggers import when
from core.actions import ScriptExecution
from org.joda.time import DateTime
from configuration import grafanaHost
from core.actions import Exec
import subprocess
import traceback

timer = None

# Mapping between the chart time periood and how often to poll for the chart
polling = { "h": 60,    # one minute
            "d": 300,   # five minutes
            "w": 900,   # 15 minutes
            "M": 3600,  # one hour
            "y": 86400} # one day

# Mapping between Grafana panel ID and file names for the chart
panals = { "1": "temp",
           "2": "hum",
           "8": "light",
           "4": "power"}

def pull_charts(log, period, poll_time):

   # Get the image for each chart
   for pid, fname in panals.items():
       fr = "now-1{}".format(period)
       log.debug("Grabbing chart for {} and period {}".format(pid, period))
       results = subprocess.check_output(['/usr/bin/wget',
           'http://{0}/render/d-solo/000000001/home-automation?orgId=1&from={1}&to=now&panelId={2}&width=1000&height=500&timeout=10000'.format(grafanaHost, fr, pid),
           '-O',
           '/openhab/conf/html/{0}.jpg'.format(fname)])
       log.debug("Results from wget for {} and period {}\n{}"
                 .format(pid, period, results))

   # Reschedule the timer
   if period != str(items["ChartVisibility"]):
       log.warning("Chart polling period changed but timer is still running, "
                   "not rescheduling")
       return
   else:
       global timer
       timer = ScriptExecution.createTimer(DateTime.now().plusSeconds(poll_time),
                                    lambda: pull_charts(log, period, poll_time))


@rule("Chart polling period",
      description="Change the polling period to generate new charts.",
      tags=["admin"])
@when("Item ChartVisibility changed")
@when("System started")
def chart_poll(event):
    chart_poll.log.info("Kicking off/modifying chart polling to {}"
                        .format(items["ChartVisibility"]))

    period = ("h"
              if isinstance(items["ChartVisibility"], UnDefType)
              else str(items["ChartVisibility"]))

    # Cancel the timer if there is one
    global timer
    if timer is not None and not timer.hasTerminated():
        timer.cancel()

    # Start a looping timer to pull new charts
    pull_charts(chart_poll.log, period, polling[period])

def scriptUnloaded():
    global timer
    if timer is not None and not timer.hasTerminated():
        timer.cancel()

1 Like

@rlkoshak thanks for the solution. Looks good.

Have been playing around with the netstat command. From that output one can determine a connection has been made.

tcp6 0 0 192.168.3.130:8080 192.168.3.46:48116 ESTABLISHED 4884/java

In this case, from “46” a connection has been establisched to the openhab basic UI on port 8080. That was my mobile phone. Once I closed openhab, it was removed from the Netstat results.

But I have no idea how to trigger an action from that :frowning:

So for now I think I will add the “generate” button. There are multiple graphs on my sitemap, so pressing the button once on one of those pages will just generate all graphs.

Show the results later.
Thanks,
Matthijs

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