Dear Sirs,
how do you manage Mqttt Thing that got items that show last value even if the thing is offline?
I’d like to get a zero value instead of the last value.
There is no easy way I know of that lets you map the Items to things from a rule. The LinkRegistry is not available as far as I can tell. Consequently you have to do the mapping yourself, be it through a separate rule per Thing or by using Groups.
But I’ll second @binderth’s question, wouldn’t it make much more sense to use NULL or UNDEF for Items where you cannot know the state of the sensor because the device if OFFLINE? Setting it to a number like that poses a bunch of problems.
I’ll also second his recommended approach.
If you don’t want to create a bunch of separate rules, one for each Thing, you can use Thing Status Reporting [4.0.0.0;4.9.9.9] to call a rule you write when any Thing goes OFFLINE. But you’ll still need to map the Thing to the Items yourself.
I believe using expiry is probably the simplest way to achieve this if applicable (i.e. predictable timeout). However, setting the value to zero seems unwise. It is better to set it to UNDEF.
In any case, here is how you can do what you wanted in JRuby. In this example, I am filtering it to only “mqtt” things, and only to items whose name contain temperature, but you can adjust the logic to whatever you want to achieve accordingly.
I’m just trying to demonstrate how easy it is to do in JRuby.
rule "Update item state when any mqtt things go offline" do
changed things, to: :offline
run do |event|
next unless event.thing.uid.binding_id == "mqtt"
event.thing.channels.each do |channel|
channel.items.each do |item|
item.update(0) if item.name.include?("temperature")
end
end
end
end