Hi, I’m trying to figure out how to write an effective and non-repetitive rules. This is what I want to accomplish:
To have one HABPanel with several sitemaps, all functioning at the same time, with the “same” buttons, e. g. the buttons all will have the same function, but will be different objects (items). Values for them will be different, but in fact will all be doing the same thing.
For example I will have Feeder 1, Feeder 2 and Feeder 3 for different rooms (that’s just an “stupid” example, I don’t really want to write details of my project). Feeder1 sitemaps’ buttons will trigger some rules, scripts and so on. Feeder2 and 3 buttons will do the same thing, only change will be the number in the buttons’ names, like for Feeder1 i will have FeedTheCat1, FeedTheDog1, for Feeder2 FeedTheCat2… You get the idea.
I know I have to actually create all the buttons by myself. I have rules working for sitemap1 and I could easily rewrite it for all of them, changing the numbers, but that seems just stupid and I don’t want to write nasty codes.
rule “Feed the cat 1”
when
Item FeedTheCat1 was changed to ON
then
val command = "python3 /etc/openhab2/feeder/feedthecat.py 1 "
Maybe some version of
rule “Feed the cats”
when Group’s FeedTheCats Item changed
then
val number = parse the triggered buttons name and get the last character in it
val command = "python3 /etc/openhab2/feeder/feedthecat.py " + number
I have read through the Design Pattern: Working with Groups in Rules article, but the thing is, all of the buttons can be triggered at any time and don’t depend on each other trhough the sitemaps, but are related in one - e.g. pushing one button will set values to others, execute scripts and so on.
Please ignore the syntax, I just want the correct idea. In another programming language that would be a piece of cake for me (simple function with arguments), but I’m not really familiar with OpenHAB concept yet.
I know you can trigger a rule when any of the group’s button was changed, but how to determine which one of it was it without writing milions of if’s? What if they get triggered at the same time?
I don’t want you to write the code for me, I’m just thinking of the best solution and would appreciate any push (or a kick, really :D), if you are willing to share your ideas with me.