Gas measurement unit in a rule

i measure my natural gas consuption using a pulse sensor attached to my meter.1 pulse is giving me a 0.01 m³.So i made that rule to set a counter for my gas

Number:Volume Total_gas "Total_gas" 
rule "Gas_sensor"
    Item Gassensorpulse changed
    var  gas_unit = Total_gas.state as Number + 0.01

that way it works but i get a plain number without a unit (m³).What do i have to change to convert the number to a number with a unit?

You could change your Total_gas Item to a suitable quantity type Item - a Number:Volume perhaps.

Then you’d need to handle it differently in your rule, something like
... + 0.01 | m³

it is already Number:Volume
i added the | m³

rule "Gas_sensor"
    Item Gassensorpulse changed to CLOSED
	Item test changed
    var  Number gas_unit = Total_gas.state as Number + 0.01 | m³

but not changed

2022-02-15 15:00:11.800 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'Total_gas' changed from 5718.62 to 5718.63


is not a smart thing to do with it, destroying its “quantity flavour”.
var Number gas_unit = ...
and here you’re asking for the result as non-quantity too. In general, just let the system sort that part out.

This should be fairly bombproof
var gas_unit = Total_gas.state as QuantityType<Volume> + 0.01 | m³

but you might have a problem with your starting value

which seems to have no units to begin with.
I suppose that’s a leftover from previous attempts.
I suspect this might get you out of that hole -
var gas_unit = 0.01 | m³ + Total_gas.state as QuantityType<Volume>
as it usually gives the result in the units of the first operand

1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.