Since the existing Item is updated over MQTT, you have no idea in the rule if it has the old value or the new value when the rule runs, so you can’t really rely on that Item in your rule anyway.
I’m trying to do some things… I’ve modified the LastUpdate to being updated from the rule. It’s working but I cannot transform back to datetime when reading the item:
var now = time.ZonedDateTime.now().withFixedOffsetZone();
var item_lu = items.getItem('InverterPV_PVPower_LastUpdate');
var lastupdate = item_lu.state.toString();
var lastupdatee = time.ZonedDateTime.parse(lastupdate);
...other code...
item_lu.postUpdate(now.toString());
I receive this error:
org.graalvm.polyglot.PolyglotException: JsJodaException: Text '2022-06-07T21:10:07.518+0200' could not be parsed at index 23: 2022-06-07T21:10:07.518+0200, at index: 23
at <js>.parse2(webpack://openhab/./node_modules/@js-joda/core/dist/js-joda.esm.js?:7706) ~[?:?]
at <js>.parse(webpack://openhab/./node_modules/@js-joda/core/dist/js-joda.esm.js?:7676) ~[?:?]
var lastupdate = item_lu.rawState.getZonedDateTime();
The post problem is one I’ve seen reported on the forum before. Sometimes ZDT will put the timezone in the string in a way that Joda can’t parse. But LocalDateTime doesn’t have that problem.
I being crazy trying to understand difference from js-joda and java time… and how to make to work together…
I’m sorry if I use many comments but still trying to understand what happens…
var {alerting} = require('personal');
//var {timeUtils} = require('openhab_rules_tools');
var now = time.LocalDateTime.now(); //.withFixedOffsetZone(); //.toEpochMilli();
var logger = log('Watt to Wh');
//java.lang.Thread.sleep(500);
//var item = items.getItem(event.itemName);
var item = items.getItem('InverterPV_PVPower');
//var item_lu = items.getItem(event.itemName + '_LastUpdate');
var item_lu = items.getItem('InverterPV_PVPower_LastUpdate');
var pastupdate = item_lu.rawState.getZonedDateTime();
var lastvalue = item.state;
var previousvalue = item.history.historicState(pastupdate); //var previousvalue = item.history.previousState(true);
var delta = '';
//var delta = time.Duration.between(pastupdatee, now);
//var delta = time.now.minus(pastupdatee);
logger.info(now);
logger.info('Handling update for: ' + item.name + ' = ' + item.state);
logger.info(item.name + ' Past Update string: ' + item_lu.state.toString() + ' Now is: ' + now.toString() ); //logger.debug
logger.info(item.name + ' Past Update formatted ' + pastupdate );
logger.info(item.name + ' Last value is: -> ' + lastvalue + ' Past value is: -> ' + previousvalue );
logger.info(item.name + ' Delta: -> ' + delta.toString() );
item_lu.postUpdate(time.LocalDateTime.now().toString()); //update with new time
In general, endeavor to always convert to and only use ZonedDateTime and you will usually be fine (with that one exception with post update which is hopefully temporary). time.toZDT() will convert what ever is passed to it, of it can, to a ZonedDateTime.
You can choose which units you want to see regardless of the units the Item holds. For example, if you skip the divide by 1000 and update the Item using totalpower + "Wh" and set the Item’s State Description pattern to %.0f kWh it will show kWh on your UI even though the value stored by the Item is Wh.
Only one instance of a given rule can run at a time. If the rule is already running when triggered again, the second trigger will wait for the rule to exit before running the rule again.
I’m sorry if I’m opening this thread again but… The rule after update of Openhab to version 4 is not working anymore. As I understood, it is because I cannot read an item as Number:Power (instant power), calculate and then send to an item as Number: Energy.
And I I have to admit that I didn’t understand the quantity and modification of the target measurements.