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()