Rules & Calculations

Hi - I have a fairly simple rule as my first attempt to calculate monthly electricity cost against consumption reported by a xiaomi sensor.

I have Dishwasher_MonthlyCost defined as an item (type number) and the rule as follows;

rule "Startup"
when
    System started
then
    Dishwasher_MonthlyCost = 0
end

rule "Dishwasher - Update Monthly Cost"
when
Item mihome_sensor_plug_158d0001ad42eb_powerConsumed received update
then
Dishwasher_MonthlyCost.postUpdate(mihome_sensor_plug_158d0001ad42eb_powerConsumed)x(0.212040)
end

However I recieve the following erorr in the openHAB logs;

2017-12-18 08:08:49.300 [INFO ] [el.core.internal.ModelRepositoryImpl] - Refreshing model 'calc.rules'
2017-12-18 08:08:54.382 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Error during the execution of startup rule 'Startup': Cannot assign a value in null context.
2017-12-18 08:10:47.274 [ERROR] [.script.engine.ScriptExecutionThread] - Rule 'Dishwasher - Update Monthly Cost': An error occurred during the script execution: Could not invoke method: org.eclipse.smarthome.model.script.actions.BusEvent.postUpdate(org.eclipse.smarthome.core.items.Item,org.eclipse.smarthome.core.types.State) on instance: null

Been trying to play around with a few different combinations but have run out of ideas. I think I just must be missing something small, as the rule is not tryingto do a complex function.

If I can get a good example working I can use the logic to extend to other use cases

Thanks

You cannot assign a value to an item in this way. You need to do like this:

Dishwasher_MonthlyCost.postUpdate(0)

The multiplication operator in openHAB (as in most other programming languages) is ‘*’ and not ‘x’. Furthermore you need to multiply the state of the item and not the item itself, and finally this needs to happen inside the parentheses so that the result of the operation becomes the input to the postUpdate() method.

Try the following and see if it works better:

Dishwasher_MonthlyCost.postUpdate(mihome_sensor_plug_158d0001ad42eb_powerConsumed.state * 0.212040)
1 Like

Thanks KjetilA,

Great help, apologises about the ‘x’ that was me messing about.

I made the changes but am now receiving a new error;

2017-12-18 11:46:53.033 [ERROR] [.script.engine.ScriptExecutionThread] - Rule 'Dishwasher - Update Monthly Cost': An error occurred during the script execution: The name '<XMemberFeatureCallImplCustom> * <XNumberLiteralImpl>' cannot be resolved to an item or type

This sounds like it cant access/resolve the item file?

calc.items

Number Dishwasher_MonthlyCost

Cheers

Also need to cast the state to a Number.

Dishwasher_MonthlyCost.postUpdate(mihome_sensor_plug_158d0001ad42eb_powerConsumed.state as Number * 0.212040)

Thanks Rich - worked like a charm and I can now see the data flowing in. Is there a simple way within the rule file to limit the number of decimal places that are returned? Currently it returns up to 6 i.e. 1.123456 which looks a tad messy.
Cheers

Dishwasher_MonthlyCost.postUpdate(String::format("%1$.2f",mihome_sensor_plug_158d0001ad42eb_powerConsumed.state as Number * 0.212040))

Typically the way to handle this is to keep the many decimal places and only round the value when logging or showing the value to the user. See Scott’s example using String::format for how you would do that for logging. See http://docs.openhab.org/configuration/items.html#state-presentation for how to round the value in your sitemap.