How-to use thing state to disable a rule?

  • Platform information:
  • openHAB version: 3.4.0
  • Issue of the topic: is it possible to run a rule only if a thing is online

Hello,

I have rule triggered with cron event. Is it possible to add a thing state as online in addition to the cron ?

This is an example:

rule "AWtrix app temp update"
when
	Time cron "0 0/10 9-20 ? * * *"
then
	logInfo("Awtrix app temp", " Updating...")
end

I’m looking for a way to it like this:

when
	Time cron "0 0/10 9-20 ? * * *" 
       AND thingXXX online
then

there is docs to explain how to deal with a state change, but not with “just the state”

Thanks for your help

Yes you can do this but:

  1. Not the way you are trying to
  2. Not in Rules DSL

I don’t think there is an easy way to get the Thing’s status inside a rule in Rules DSL like you can in other languages (including Blockly) and there definitely isn’t a way to disable a rule from Rules DSL like other languages.

If you stick with Rules DSL, you’ll have to create another rule that triggers when the Thing changes status and update an Item. Then I’m this rule have an if statement that checks that Item.

In any of the other languages you can add an if statement that checks the status of the Thing directly.

In the UI, if statement would go in the “but only if” part of the rule.

With the other languages you also have the option and actually disabling the rule, but like with the Item approach, you’ll need another rule that triggers on state changes of the Thing and enables/disables the rule accordingly. But with this approach in the UI, you wouldn’t even need any code at all.

I have so many things remaining in my openhab todo list…
I was going to switch to something better when I have upgraded to OH3.3, but issues, memory leaks and so on made me postpone everything.

For OH4.x what would be the best language for rules ?
(I know this question can be an endless debate but…)

Thanks

  • Blockly
  • JS Scripting - this is the most popular and most used. Plenty of examples and plenty of people to help you
  • JRuby - IMO, this is the easiest and nicest language + helper library to work with, and also possibly the most powerful. It is much nicer to work with Ruby compared to JS. It has a comprehensive documentation too. But not as many people use it and there aren’t as many posts about it either. Currently the latest version of the helper library supports OH versions from 3.4 all the way to 4.2-snapshot. So you can actually use JRuby now before upgrading to OH4 and you’d get the latest version of the helper library with it. Upgrading to the latest helper library is easy, just restart openhab or restart the automation addon. You can also install third party libraries easily, much easier than in JS.
1 Like

argh, you 've added me another one in my list :slight_smile:

Ok, I’ll have some reading tonight on the Jruby docs.
I want to make things more dynamic in my rules and not code item per item behaviours…

Thanks

Generally to do this, you’d use Groups and act on group members

If you ever wondered, post the question and hopefully someone can offer ideas

This is easy, and can be done in jsscripting or jruby. I don’t know the syntax in js, but in jruby:

rule "AWtrix app temp update" do
  cron "0 0/10 9-20 ? * * *"
  only_if { things["mqtt:topic:backporch-pir"].online? }
  run do
    logger.info("Updating awtrix app temp...")
  end
end

The only_if is merely a syntactic sugar. You could do the same in this manner:

rule "AWtrix app temp update" do
  cron "0 0/10 9-20 ? * * *"
  run do
    next unless things["mqtt:topic:backporch-pir"].online?

    logger.info("Updating awtrix app temp...")
  end
end

JRuby offers ways to make cron triggers easier to write

rule "xxx" do
  cron minute: "0/10", hour: "9-20"
  ...
end

You could also do this, although not quite the same in implementation (it sets a cron job that triggers every 10 minutes around the clock, and each time it triggers, it checks whether the time is between 9am and 8pm)

rule "xxx" do
  every 10.minutes
  between "9am".."8pm"
  ...
end

Note that to achieve this is all about the approach. You can achieve this goal in any of the languages, even Rules DSL.

no way (no offense :wink:): Blockly (precisely not a language) is definitely the easiest among them… and yet powerful.
And you can include any code from JavaScript-examples too.

Whaou Thanks for all those info, don’t want to start a is Mac or PC are best :slight_smile:
(as everybody knows it is xxxx )

I’ve built a new VM with OH4.1.1 and I’ll make a try with JRuby as it looks like we can still do Java embedded inside.

As I no longer use Raspberry, it removes all the memory constrains, so I can eat as many RAM I need

Thanks again for all your feedback

1 Like

You can do Java embedded in rules for all the rules languages. Even Blockly, though you’ll have to use the inline script Block.