OH3 - state.toString?

  • Platform information: Pi3
  • openHAB version: openhabian

Hi guys, I am just trying to move over some DSL rules to scripts.

I am trying to send a notification of a battery level. The items is reading fine.

Is state.toString the way to send an item value to a broadcast notification? I can’t seem to get it to work.

var logger = Java.type('org.slf4j.LoggerFactory').getLogger('org.openhab.rule.' + ctx.ruleUID);  
var NotificationAction = org.openhab.io.openhabcloud.NotificationAction;  
var ScriptExecution = Java.type("org.openhab.core.model.script.actions.ScriptExecution");  

if (itemRegistry.getItem('Test_Sw').getState() == 'ON')   
{ 
 NotificationAction.sendBroadcastNotification("GW battery is {}" + GPSTrackerGW_BatteryLevel.state.toString );  
}

see the following information for that.
easiest for ECMA is events.sendCommand("ITEMNAME", VALUE); or events.postUpdate("ITEMNAME", VALUE);
to read the value it’s just items["ITEMNAME"]

your line should read:

... 
 NotificationAction.sendBroadcastNotification("GW battery is " + items["GPSTrackerGW_BatteryLevel"] );  
... 

https://openhab-scripters.github.io/openhab-helper-libraries/Guides/Event%20Object%20Attributes.html

Thanks Thomas. That really helped.

I am still getting the hang of the script type solutions.

Thanks again.

1 Like