Virtual Item - What am I doing wrong?! [FIXED]

Okay - I’ll list everything I’ve done - but I don’t see why it’s not working (I’m getting a NULL Value when viewing under classic UI with the sitemap in question) Very simple stuff so far!

{
  "link": "http://******:8080/rest/items/zwave_device_dcac0cb6_node2_meter_kwh",
  "state": "497.147",
  "stateDescription": {
    "pattern": "%.1f",
    "readOnly": true,
    "options": []
  },
  "type": "NumberItem",
  "name": "zwave_device_dcac0cb6_node2_meter_kwh",
  "label": "Electric meter (kWh)",
  "tags": [],
  "groupNames": []
}

I’m testing here - so the Cron is very often - I’ll change it to when the zwave sensor gets updated

I’m also just trying to insert a value as a last ditch effort to get something ANYTHING other than NULL.

import org.openhab.core.library.types.DecimalType

rule "Calculate Monthly Charge"
when
//Item zwave_device_dcac0cb6_node2_meter_kwh.state  recieved updated
time cron "5 * * * * ?"
then
//total_cost.postUpdate((zwave_device_dcac0cb6_node2_meter_kwh.state as DecimalType) * .047)
totalCost.postUpdate(500)
end

I’ve bounced back and forth defining the label in sitemap vs Item - I switched at one point, but didn’t make a difference.

sitemap default label="The old Strickland House"{
    Frame label="Energy" {
        Text item=zwave_device_dcac0cb6_node2_meter_kwh label="KWH"
        Text item=zwave_device_dcac0cb6_node2_meter_watts label="Watts" valuecolor=[>5000="red",>2500="orange",<=1000="blue"]
        Text item=totalCost label="Total Cost [%f]"
        }
    }

And last but not least - the one liner of a items list (I’m not sure where OH2 put my nodes for Zwave, I think somewhere in userdata? which is weird, but both other items in the sitemap work.

Number totalCost

Any help is appreciated!

EDIT: - log file contains this.

2016-08-27 10:33:15.326 [ERROR] [.script.engine.ScriptExecutionThread] - Rule 'Calculate Monthly Charge': Script interpreter couldn't be obtain

I’m going to loose my mind! I can’t get this thing to update! It’s just NULL… Stinking Null! It’s so simple now I’ve got

rule "Calculate Monthly Charge"
when
	time cron "5 * * * * ?" 
then
	totalCost.postUpdate(500)
end

Hey Brandon,
this is a bit confusing. Are you sure your item is defined in the system (check smarthome::items)?

Also, are you sure your rule is executed at all? Add logging (as good practice) and check your logs:

rule "Calculate Monthly Charge"
when
    Time cron "5 * * * * ?"
then {
    logInfo("RULE", "Calculate Monthly Charge")
    totalCost.postUpdate(500)
}
end
 tail -f /var/log/openhab2/openhab.log -f /var/log/openhab2/events.log

Oh my gosh someone to help!

I’m happy to log it - but I don’t see (in new OH2 structure) where to put the logbook.xml and the wiki isn’t super clear how it should be formatted.

I would HOPE the rule is being executed. I don’t need something extra for “time cron” to run do I? Should just be standard Cron formatting (so that should be every 5 seconds) But to your point - this is sort of where I was zoning in as well. I felt it wasn’t being fired.

How do I check my items? I have the virtual.items file JUST for virtual Items (i.e. things I want displayed (typically calculated values) in my sitemap but that don’t “actually” exist with a binding. Can I check “smarthome::items” from the terminal? Is this what you’re intending?

Everytime I change the virtual.items file I see in events “totalCost item removed” (paraphrasing) but never see it actually added?

Thanks for replying - I think once I get my head wrapped around this simple concept I’ll be able to build quickly on it.

I Also - I’m daft - I know how to get into the terminal for OH2 if I start it manually (because it takes me there) but I’m not sure how to get to it when starting it as a service. I can poke around - probably something I should know being I love CLI.

I didn’t check there but under habmin2 I can see it under items as type NumberItem. If that counts?

Still not confident the cron is actually firing this rule every 5 sec

(side node: if you do not reply to my answer or mention me like @pr0f I will not be notified of your messages)

Okay, first things first: A lot changed since openHAB 1.x and a lot of confusion comes out of that. There is nothing wrong with OH2, may too many users just try to apply what was right with OH1.x. That said, there is no logback.xml file anymore. Also be aware, that the wiki is written for OH1.x. While a lot of information there is still valuable, the main source for knowledge should be the openHAB 2 documentation: http://docs.openhab.org

“Logging” just means putting the rule I posted as is into the file /etc/openhab2/rules/test.rules. No extra steps are needed to see “Calculate Monthly Charge” every five minutes with:

tail -f /var/log/openhab2/openhab.log

As I took these lines from my own configuration, I know that they should indeed work.
Additionally I expect your item to be changed visible in the events log, that’s why it’s a good idea to look at the combined log:

tail -f /var/log/openhab2/openhab.log -f /var/log/openhab2/events.log

One small remark, as I am not at home right now, I am not absolutely sure if updating to the same value 500 will create a log entry every time.

Please refer to the chapters below http://docs.openhab.org/administration/index.html

Now, the last thing I can think of is the item not being defined in your system. The file /etc/openhab2/items/virtual.items with the content Number totalCost is totally okay (I personally would define label and such here as well).

In the karaf console. Please check the link above.

Defining an item in an item file should just do… Did you try changing the name?

Are you using a manual (possibly outdated) installation of openHAB?

@ThomDietrich - I totally use a manual installation. But I’m super anal about where things get installed, and I prefer everything in a special “app” directory for all my installs, so if I have a chance, I always manually install, or compile myself.

It’s only about a week or 2 old, openhab-offline-2.0.0-SNAPSHOT to be exact.

Glad to see logging is totally built in and you don’t need to add extra to XML files. Seems like a good direction.

When you say OH1 and OH2 (Eclipse vs non) I totally get their quite a bit different under the sheets. This is okay - since I started on OH2 (3 weeks ago) and have never used OH1 (so I’m just learning OH2 as I go - even if it means referencing OH1 Docs (or the newer OH2 documentation - even though some of it is obviously in it’s infancy. I don’t mind that either - I like helping (even if it’s just through learning and helping others later) with newer software - especially if it’s as cool as this stuff!

I’m going to go through all of what you suggested and report back in a bit.

Thanks again!

@ThomDietrich

W…T…F… I paste your stuff in and it works.

Is this because I’m editing in VIM and not in OH2 (eclipse) designer? How strict is it with formatting - your actual text was basically the same.

I JUST got Eclipse HA Designer working - so I’m going to get it attached to the SMB shares but honestly - I can’t see what made the difference?

Thanks for all the help though - I’m going to try to track down exactly what did the trick.

FYI I’m assuming I can’t use OH1 designer on OH2 right? I’m to use Eclipse HA Designer?

Looking at it “time” vs “Time” sticks out. “time” is not correct. The Eclipse SmartHome Designer (that’s what you should use) just confirmed that. So your lesson here is to use this tool! :wink: Besides that, there are no strict formatting rules I know of, you may still use vim from time to time.

Well okay then :slight_smile: If you know what you are doing, that is of course your option :wink:

I just learned a new word :slight_smile:
It is no secret, that the documentation still lacks some critical chapters and there are a lot of smaller task open for volunteers. If you are interested, let me know!

Give me a few more weeks to get comfortable and I’ll be happy to help! I hate trying to help when I myself don’t know my way around (I don’t expect to be an expert but at least getting consistent returns will be a start!)

@ThomDietrich

Okay - Got Eclipse HA designer working with SMB mount - and I like it! I have a question though. My sitemap is below and works fine for the 2 Zwave items (first 2 of the three). But they’re not in my .items file - they’re added as node1.xml (for my zwave controller) and node2.xml (with my power sensor). in {oh2_HOME}/userdata/zwave

sitemap default label="The old Strickland House"{
    Frame label="Energy" {
        Text item=zwave_device_dcac0cb6_node2_meter_kwh label="KWH"
        Text item=zwave_device_dcac0cb6_node2_meter_watts label="Watts" valuecolor=[>5000="red",>2500="orange",<=1000="blue"] 
	Text item=totalCost
        }
    }

Wy is it then when I reference this in the rules

(fyi price is a variable set to my kwh pricing so it should be fine and total is also a declared var above the rule as well. )

total = zwave_device_dcac0cb6_node2_meter_kwh.state * price

It says…

The method or field zwave_device_dcac0cb6_node2_meter_kwh is
   undefined

Thanks for any info! Maybe I need Item=?

Wierd - doesn’t seem to like onevar * twovar (the * part)

17:48:05.030 [ERROR] [ntime.internal.engine.ExecuteRuleJob] - Error during the execution of rule Calculate Monthly Charge: An error occured during the script execution: The name '<XInstanceOfExpressionImpl> * <XFeatureCallImplCustom>' cannot be resolved to an item or type.

And yet…

Works completely fine… Why is the zwave device state being such a jerk?

    //total = zwave_device_dcac0cb6_node2_meter_kwh.state + price
    total = 500 * price
    //total = 500
    totalCost.postUpdate(total)

@ThomDietrich - WORKING

WHy - I don’t know - I casted as a Number Var.

import org.openhab.core.types.*
import java.lang.Math

var Number kwh = zwave_device_dcac0cb6_node2_meter_kwh.state
var Number total = 0
var Number price = 0.045
rule "Calculate Monthly Charge"
when
    Time cron "5 * * * * ?" or
    Item zwave_device_dcac0cb6_node2_meter_kwh received update
then {
    logInfo("RULE", "Calculate Monthly Charge")
    total = kwh * price
    //total = 500 * price
    //total = 500
    logInfo("RULE", "there - " + zwave_device_dcac0cb6_node2_meter_kwh.state)
    totalCost.postUpdate(total)
}
end