[SOLVED] Request for rule: Virtual switch and Astro Status switching on staircase light

This is my first request in the OH community due to a problem with a maybe? simple rule.

Task is: the light in our staircase shall be switched on

  • when a virtual is switched on before and
  • civil dusk is reached (and not before)

Reason: when we are out off the house, the staircase light shall be switched after civil dusk

  • Platform information:
    • Hardware: Raspi 3
    • OS: openhabian
    • openHAB version: 2.3.0-1

Can anybody support in this case, code and logs are attached. Thanks in advance

items

Group gTreppenhaus

// Virtual Rule switch staircase
Switch dg_l_treppe_virtual "Treppenhaus Licht" <light>  (gTreppenhaus)

// Real Switch (Test with office ceiling light)
Switch dg_az_l_deckenlampe "AZ Decke Licht" <light>     (gLicht)    {channel="zwave:device:2a8c8c23:node4:switch_binary"}

//Regel 03.07.2018
rule "TreppenhausLicht ein"
when
Channel "astro:sun:falkensee:civilDusk#event" triggered START  
then
var CivilDusk_Time = (astro_sun_civil_dusk_start.state as DateTimeType).getZonedDateTime.toInstant.toEpochMilli
    if(dg_l_treppe_virtual.state == "ON" && now.isBefore(CivilDusk_Time)) {
    dg_az_l_deckenlampe.sendCommand(OFF)
    }
    logInfo("Treppenhaus Licht", "wurde remote eingeschaltet!")
    if(dg_l_treppe_virtual.state == "ON" && now.isAfter(CivilDusk_Time)) {
    dg_az_l_deckenlampe.sendCommand(ON)
    }   
end

sitemap treppenhaus label="Treppenhaus Licht"
{
Switch item=dg_l_treppe_virtual 
Switch item=astro_sun_civil_dusk_start_status label="Abenddämmerung erreicht"   icon="switch"
Switch item=dg_az_l_deckenlampe label="Treppenhaus Licht" icon="light" 
}

Log2018-07-04 21:34:00.056 [vent.ItemStateChangedEvent] - Day_Phase changed from SUN_SET to CIVIL_DUSK
2018-07-04 21:34:01.092 [vent.ItemStateChangedEvent] - iPhone_Sybille_ResponseTime changed from 89.0 to 100.0
2018-07-04 21:34:01.106 [vent.ItemStateChangedEvent] - iPhone_Sybille_zuletzt_gemeldet changed from 2018-07-04T21:33:00.953+0200 to 2018-07-04T21:34:01.080+0200
==> /var/log/openhab2/openhab.log <==
2018-07-04 21:34:01.256 [INFO ] [home.model.script.Wallplug Astro Ein] - Wall Plug eingeschaltet!
2018-07-04 21:34:01.265 [INFO ] [thome.model.script.Treppenhaus Licht] - wurde remote eingeschaltet!
==> /var/log/openhab2/events.log <==
2018-07-04 21:34:01.273 [ome.event.ItemCommandEvent] - Item 'dg_az_s_wallplug' received command ON
2018-07-04 21:34:01.288 [vent.ItemStateChangedEvent] - dg_az_s_wallplug changed from OFF to ON

What is not working for you? The only thing that looks odd to me is that I don’t see logic for triggering the light to turn off, but this could be handled by adding another Astro event to the rule trigger, like (did some cleanup too)…

 rule "TreppenhausLicht ein"
when
    Channel "astro:sun:falkensee:civilDusk#event" triggered START 
    or
    Channel "astro:sun:falkensee:civilDawn#event" triggered START
then
    if (dg_l_treppe_virtual.state == "ON") {
        var CivilDusk_Time = (astro_sun_civil_dusk_start.state as DateTimeType).getZonedDateTime.toInstant.toEpochMilli
        if (now.isBefore(CivilDusk_Time)) {
            dg_az_l_deckenlampe.sendCommand(OFF)
        }
        else if (now.isAfter(CivilDusk_Time)) {
            dg_az_l_deckenlampe.sendCommand(ON)
        }
    }
end

I wasn’t sure what you were trying to log, since it was logging every time the rule ran.

Why do you test in the time is after the civil dusk start time if the rule trigger is civil dusk start? The time will ALWAYS be after.
You can simplify your rule considerably knowing this.

//Regel 03.07.2018
rule "TreppenhausLicht ein"
when
Channel "astro:sun:falkensee:civilDusk#event" triggered START  
then
    if (dg_l_treppe_virtual.state == ON) dg_az_l_deckenlampe.sendCommand(ON)
end

Regards

Dear both,
thank you very much for the very replies, I am very impressed.

The smart proposal for a shortened rule from @vzorglub fired right on time, thx.

@5iver: I had implemented the log just for testing case to get information into the log-file.

Best regards