Apply rule to all items?

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.

I’ve defined this rule and it works like a charm:

configuration: {}
triggers:
  - id: "1"
    configuration:
      thingUID: mqtt:topic:mosquittotb:HeT
      status: OFFLINE
    type: core.ThingStatusUpdateTrigger
conditions: []
actions:
  - inputs: {}
    id: "2"
    configuration:
      itemName: shelly_temperatura_e_umidita_temperatura
      state: "0"
    type: core.ItemStateUpdateAction
  - inputs: {}
    id: "3"
    configuration:
      itemName: shelly_temperatura_e_umidita_umidita
      state: "0"
    type: core.ItemStateUpdateAction

Now my question is how can I extend this to all things without manually add each item to each thing?

It’s a bit easier, if you use the “expire”- function on a item itself. you can define a timespan, after that the value is changed to a defined state:

That is of course, if normally your temperatures are sent in a defined interval via MQTT and if that interval is e.g. 5minutes.

My question though is the use case behind it. Why do you want to “reduce” the temperature to “0”, if the MQTT is offline?

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