Blinking LED - how to implement it correctly?

In my installation when I receive signal from flood sensor I will activate blinging led.
Because I have many sensors I have several similar sections:

rule "FloodAlarm_BathroomLower_Activated"
    when
    	Item FloodSensorLazienkaDolna changed from OFF to ON or
        Item FloodSensorLazienkaDolna changed from NULL to ON
	then
		<activate blinking led>
		floodAlarmIsActivated = true 	 
		...
end


// FloodAlarm activation BathroomUpper (edge activated)
rule "FloodAlarm_BathroomUpper_Activated"
    when
    	Item FloodSensorLazienkaGorna changed from OFF to ON or
        Item FloodSensorLazienkaGorna changed from NULL to ON
	then
		<activate blinking led>
		floodAlarmIsActivated = true 	 
		...
end




I’ve found script that implement blinking behaviour

but I do not add the same code for each sensor - I would have one place where I put ‘blinking logic’ script, I do not want to put such part in each rule:

if(lightsBlinkingTimer != null) {
        lightsBlinkingTimer.cancel
        lightsBlinkingTimer = null
    }

    lightsBlinkingTimer = createTimer(now.plusSeconds(1), [|
        Light.sendCommand(if(Light.state == ON) OFF else ON)
        lightBlinkingTimer.resechedule(now.plusSeconds(1))
    ])

I wanted to make to rule on changes floodAlarmIsActivated variable but such rule cannot be accepted.
What is best way to do it?

Or

Thank you.
I also found out that I can use time based trigger that start every second and inside that triger check if variable floodAlarmIsActivated is set to true and then on or off LED. I know that cron is limited to second but in this example it is not a problem. I think it should work.

this code works well for me

var Timer TimerH = null

rule "Alarma automĂĄtica"
when
Item Detector_Gas changed to ON
then
if(TimerH != null) {
TimerH.cancel
TimerH = null
}

TimerH = createTimer(now.plusSeconds(1)) [|
    if(Alarma.state==ON){
            Alarma.sendCommand(OFF)
    }
    else{
            Alarma.sendCommand(ON)
    }
    TimerH.reschedule(now.plusSeconds(1))
]

end

rule "Apagado de alarma automĂĄtica"
when
Item Detector_Gas changed to OFF
then
Alarma.sendCommand(OFF)
if(TimerH != null) {
TimerH.cancel
TimerH = null
}
end

1 Like

I know this is an old thread but rather than opening up a new one
I think my question does relate to that topic.

I just created a rule that toggles a virtual item every other second, so I can use it as a blinker wherever I want

triggers:
  - id: "1"
    configuration:
      cronExpression: "* * * * * ? *"
    type: timer.GenericCronTrigger
conditions: []
actions:
  - inputs: {}
    id: "2"
    configuration:
      type: application/javascript
      script: |
        if (itemRegistry.getItem('Blinker').getState() == 'ON') {
          events.sendCommand('Blinker', 'OFF');
        } else {
          events.sendCommand('Blinker', 'ON');
        }
    type: script.ScriptAction

which brings me to my questions:

  • is it really a “good” idea to run a rule every other second?
  • By toggling this virtual item it basically spams my logs which also doesn’t really look healthy
2021-01-22 18:26:18.989 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'Blinker' changed from ON to OFF
2021-01-22 18:26:19.001 [INFO ] [openhab.event.ItemCommandEvent      ] - Item 'Blinker' received command ON
2021-01-22 18:26:19.006 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'Blinker' changed from OFF to ON
2021-01-22 18:26:19.500 [INFO ] [openhab.event.ItemCommandEvent      ] - Item 'Blinker' received command OFF
2021-01-22 18:26:19.505 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'Blinker' changed from ON to OFF#
````

Any ideas?

Well, in general it’s not really “bad”. But it’s not great. We should endeavor to avoid writing rules that do nothing most of the time. This is also sometimes called polling. It’s much better to instead react to events and only do work when necessary instead of always doing something even when you don’t need to. The code tends to be simpler to understand and maintain and you avoid being wasteful with resources.

However, is it really bad to do this? No, not really. You’ll be spamming the logs, sure and that can add up to wearing out an SD card a little faster. And you’ll be using probably a barely measurable more amount of energy per day. But those are not really that bad of a problem.

So what is someone to do? You have a number of options actually. A couple are:

1 Like

Thanks Rich,

the intent was to have a blinker item that I can use with a logic and to make icons blink on the UI (for example an alarm) without using much logic - instead I would have just used a logical and with the blink to let my icon toggle visibility.

but yes I could somehow write a rule that makes sure the “blinker” will only start when any alarm item goes on and then using the approach you have provided.

Thanks for sharing your thoughts - always appreciated!