Transforms on Items are run on every update of the Item. The result of the transform becomes the Item’s state.
So how does it look in a rule, then?
(I like the centralized approach)
At a high level, the full text gets set to a String Item. There is a Rule that triggers when this String Item changes. In this rule parse out the data needed and postUpdate those values to the Items that represent that value.
Thanks, @rlkoshak,
I thought there is already a solution available to save some time
I will start formatting it myself then the next days.
@NCO did you come up with the formatting that you can share?
I am using habpanel as UI and my current uptime related settings are:
.items file
Number CPU_Uptime { channel="systeminfo:computer:work:cpu#uptime" }
.widget file
<div class="valueGroup"><div class="value">{{itemValue('CPU_Uptime') | number:0}} min</div></div>
Assuming there is a javascript that does the conversion, the .widget file will have to change to show the item’s string, but I don’t know how to do that…
I tried changing the .item file to the line below but the uptime is still reported in minutes
updated .item file
Number CPU_Uptime "Uptime [JS(duration.js):%s]" <clock> (gSystem) { channel="systeminfo:computer:work:cpu#uptime" }
Should I place the javascript file in /etc/openhab2/transform or /etc/openhab2/scripts or …
What will be the settings in the .items .widget files?
Thanks
The file goes in transform folder.
Make sure you have the JS transform installed.
For the record, you could use the .js
file in a rule something like this:
val String uptime = '' + transform("JS", "duration.js", System_CPU_Uptime.state.toString)
Thanks @rlkoshak and @rtvb . I am not seeing any difference when using the js script (I’m still seeing the upstart in minutes)
@rtvb I don’t have a rule for displaying the upstart. It basically refreshes every e.g. 30 secs based on systeminfo.things
cat /etc/openhab2/things/systeminfo.things
systeminfo:computer:work [interval_high=30, interval_medium=60]
Maybe my duration.js script is not called at all?
The only place I have it mentioned is in the .items file
cat /etc/openhab2/items/systeminfo.items
...
Number CPU_Uptime "Uptime [JS(duration.js):%s]" <clock> (gSystem) { channel="systeminfo:computer:work:cpu#uptime" }
and in the habpanel widget I have
...
<div class="valueGroup"><div class="value">{{itemValue('CPU_Uptime') | number:0}} min</div></div>
Would itemValue(‘CPU_Uptime’) in the widget, trigger the item state (i.e. “Uptime [JS(duration.js):%s]”) that will trigger the js script?
@rlkoshak, I saw your recommendation to use designer to flush out any syntax errors in rules. I’ll start using it when I have first chance (I haven’t used it before and I don;t want to open another front here)
I’m trying to add log statements within it to see what is going on there but I don’t see anything in openhab.log…
I did the following:
- Turned on DEBUG logging for package org.openhab.io.net.exec in karaf (per the guidelines for openhab1 here (I’m using openhab2 but did not find documentation for openhab2))
log:set DEBUG org.openhab.io.net.exec
log:list
...
org.openhab.io.net.exec | DEBUG
- Added a log statement in the duration.js
logger.error( "foo2" );
But I don’t see anything in /var/log/openhab2/openhab.log…
You don’t need too. My code was just an example how to use it when needed. I use this to send myself a daily email with some general system statistics.
I dont’t have any experience with HABpanel, so I can’t help you with this one.
In general, if you use the duration javascript, the actual item value is still kept as a Number by opennHAB but only transformed at runtime when displayed in the sitemap.
In a karaf console you could use smarthome:status CPU_Uptime
to show the actual value.
A dummy rule to test your javascript could be (untested):
rule "Test Duration Script"
when
Item CPU_Uptime received update
then
val String duration = '' + transform("JS", "duration.js", CPU_Uptime.state.toString)
logInfo('Test', 'Number ==> ' + CPU_Uptime.state.toString)
logInfo('Test', 'String ==> ' + duration)
end
Just watch your log file to see what’s listed. If your log file is flooded, replace the event based trigger for the rule with a a time based trigger.
I am not aware that you can log from javascript transformations, so can’t help you with this one, either.
Your example rule helped me solve the problem. The conversion showed ok in openhab.log.
I ended up updating habpanel from a string item, which is updated automatically via the rule every time the uptime number item is updated (thanks @rlkoshak for your advice).
See my solution here
sure:
rule "format uptime"
when
Time cron "32 0/5 * * * ?"
then
var int days = (uptime.state as DecimalType).intValue / (24 * 60)
var int hours = ((uptime.state as DecimalType).intValue / 60) - (24 * days)
var int minutes = ((uptime.state as DecimalType).intValue - 60 * (24 * days + hours))
var String result = ""
// days
if (days > 0) {
result = result + days + " Tage / "
}
// hours
if (hours > 0) {
result = result + hours + " Std. / "
}
// minutes
if (minutes > 0) {
result = result + minutes + " Min."
} uptimeFormatted.postUpdate(result)
logInfo("system.rules", "Uptime updated to " + uptimeFormatted.state)
end
I used your rule and changed it to my Items.
Rule:
rule "format uptime"
when
Time cron "32 0/5 * * * ?"
then
var int days = (Sys_OH2_Uptime.state as DecimalType).intValue / (24 * 60)
var int hours = ((Sys_OH2_Uptime.state as DecimalType).intValue / 60) - (24 * days)
var int minutes = ((Sys_OH2_Uptime.state as DecimalType).intValue - 60 * (24 * days + hours))
var String result = ""
// days
if (days > 0) {
result = result + days + " Tage / "
}
// hours
if (hours > 0) {
result = result + hours + " Std. / "
}
// minutes
if (minutes > 0) {
result = result + minutes + " Min."
} Sys_OH2_UptimeFormatted.postUpdate(result)
logInfo("system.rules", "Uptime updated to " + Sys_OH2_UptimeFormatted.state)
end
My Items:
Number Sys_OH2_Uptime "Uptime [%s]" (gSys) { channel="systeminfo:computer:openhab2:cpu#uptime" }
Number Sys_OH2_UptimeFormatted "Uptime Formatted [%d]" (gSys)
But I get an error in the logs:
2018-11-11 11:00:32.124 [WARN ] [rthome.model.script.actions.BusEvent] - Cannot convert '7 Std. / 59 Min.' to a state type which item 'Sys_OH2_UptimeFormatted' accepts: [DecimalType, QuantityType, UnDefType].
2018-11-11 11:00:32.134 [INFO ] [.smarthome.model.script.system.rules] - Uptime updated to NULL
2018-11-11 11:05:32.099 [WARN ] [rthome.model.script.actions.BusEvent] - Cannot convert '8 Std. / 4 Min.' to a state type which item 'Sys_OH2_UptimeFormatted' accepts: [DecimalType, QuantityType, UnDefType].
2018-11-11 11:05:32.106 [INFO ] [.smarthome.model.script.system.rules] - Uptime updated to NULL
2018-11-11 11:10:32.138 [WARN ] [rthome.model.script.actions.BusEvent] - Cannot convert '8 Std. / 9 Min.' to a state type which item 'Sys_OH2_UptimeFormatted' accepts: [DecimalType, QuantityType, UnDefType].
2018-11-11 11:10:32.146 [INFO ] [.smarthome.model.script.system.rules] - Uptime updated to NULL
What is the problem here?
Any Ideas?
Ok, got it.
I changed the item to String and it works!
ok i have the openhab log showing the right uptime in days hours and min in a blue line , but how can i show i on the sitemap ,can anyone help please
Hey Robert,
thank you for this solution. Works perfectly here at my Raspi
I tried the script from @rtvb (thanks), that works for basicUI, but not for PaparUI. Is there anything changed in the past two years regarding transformation within an item label?
upper part = paperui, lower part is basicui:
PaperUI is still an administrators interface for system setting up and testing etc., and is not intended to have nice presentation goodies.
Thanks, too bad. Administrator interface or not, i think it should have the transformation in place, for testing purpose etc. Anyway, with the all new UI that is being developed for 3.0 i don’t think anyone should waste their time implementing it for paperui
it works for me as well but I do not like so many logs to be pushed.
In your example @pbogocz you run the rule every 5min and 32sec. do you observe so many logs as rule launch?
2020-04-15 14:15:44.036 [INFO ] [.smarthome.model.script.system.rules] - Uptime updated to 27 Min.
2020-04-15 14:15:45.036 [INFO ] [.smarthome.model.script.system.rules] - Uptime updated to 27 Min.
2020-04-15 14:15:46.034 [INFO ] [.smarthome.model.script.system.rules] - Uptime updated to 27 Min.
2020-04-15 14:15:47.038 [INFO ] [.smarthome.model.script.system.rules] - Uptime updated to 27 Min.
2020-04-15 14:15:48.033 [INFO ] [.smarthome.model.script.system.rules] - Uptime updated to 27 Min.
2020-04-15 14:15:49.037 [INFO ] [.smarthome.model.script.system.rules] - Uptime updated to 27 Min.
2020-04-15 14:15:50.019 [INFO ] [.smarthome.model.script.system.rules] - Uptime updated to 27 Min.
2020-04-15 14:15:51.037 [INFO ] [.smarthome.model.script.system.rules] - Uptime updated to 27 Min.
2020-04-15 14:15:52.015 [INFO ] [.smarthome.model.script.system.rules] - Uptime updated to 27 Min.
2020-04-15 14:15:53.017 [INFO ] [.smarthome.model.script.system.rules] - Uptime updated to 27 Min.
2020-04-15 14:15:54.016 [INFO ] [.smarthome.model.script.system.rules] - Uptime updated to 27 Min.
2020-04-15 14:15:54.202 [vent.ItemStateChangedEvent] - RaspberryPiUptime changed from 27.3 to 28.3
2020-04-15 14:15:55.036 [INFO ] [.smarthome.model.script.system.rules] - Uptime updated to 28 Min.
2020-04-15 14:15:55.037 [vent.ItemStateChangedEvent] - RaspberryPiUptimeFormatted changed from 27 Min. to 28 Min.
2020-04-15 14:15:56.015 [INFO ] [.smarthome.model.script.system.rules] - Uptime updated to 28 Min.
2020-04-15 14:15:57.037 [INFO ] [.smarthome.model.script.system.rules] - Uptime updated to 28 Min.
2020-04-15 14:15:58.015 [INFO ] [.smarthome.model.script.system.rules] - Uptime updated to 28 Min.
2020-04-15 14:15:59.018 [INFO ] [.smarthome.model.script.system.rules] - Uptime updated to 28 Min..
It seems info log are being pushed until 1min overclock. every sec. or less.
Does it mean the script and the maths are being done with the same frequency ot it is being done one. It is strange and not efficient, unless I am wrong.