Anti burglary presence simulation

I’m trying to implement similar thing. When I’m gone, turn on light based on its state from a week ago. It worked once, but then it always returns the rror “could not invoke… on instance null”

GLV_PENDEL_HAL.sendCommand (GLV_PENDEL_HAL.historicState(now.minusDays(7)).state )

or

GLV_PENDEL_HAL.sendCommand (GLV_PENDEL_HAL.historicState(now.minusDays(7)).state.toString )

I’ve been reading on other threads similar behavior but haven’t succeeded, it worked a few times. but i’m not sure why. Any ideas?

thanks
philippe

Start with debugging and see what

GLV_PENDEL_HAL.historicState(now.minusDays(7)).state

returns

I included the first two points of the ideas of @Oggerschummer to the initial solution:

  • Store the date when the auto-lighting has been activated.

  • Get the historic value from the stored date -7 + (days since then mod 7)

Further, I added:

  • A distinction by means of two groups between lights, which already have an automatic switch off and lamps which do not have such automatic switch off.

  • A check if the lamp state is equal to the historic state to avoid unnecessary commands.

Currently, I use Influxdb as backend i.e. val historicItem = lamp.historicState(JavaTimestamp_toGetHistStateOf,"influxdb")

burglerlights.rules

import java.time.temporal.ChronoUnit;

rule "Periodically check auto lights"
when
    Time cron "0/30 * * ? * * *"
then

	// If lights away mode is on
	if (SwitchLightAutoMode_state.state == ON){

        val JavaTimestamp_AutoLightModeSwitchedON = ZonedDateTime.parse(Timestamp_AutoLightModeSwitchedON.state.toString).withZoneSameInstant(ZoneId.systemDefault())
        val diffDays = ChronoUnit.DAYS.between(JavaTimestamp_AutoLightModeSwitchedON.truncatedTo(ChronoUnit.DAYS), now.truncatedTo(ChronoUnit.DAYS))
        val sevenMinusModDiffDays = 7 - diffDays % 7 + diffDays
        val JavaTimestamp_toGetHistStateOf = now.minusDays(sevenMinusModDiffDays)

		// for lights with manual switch off, forward the historic state
		gLightManualOff.members.forEach[lamp|
			val historicItem = lamp.historicState(JavaTimestamp_toGetHistStateOf)
                        // if there is no historic item in case e.g. the item is added in the last 7 days 
			if (historicItem !== null){
                                val historicState = historicItem.state as OnOffType

				// only send command with historic state if different from current state
				if (historicState != lamp.state) {
					lamp.sendCommand(historicState)
					logInfo("Burgler Light","Burgler light mode switched " + historicState.toString + " manual-off-light: " + lamp.name)
				}
			}
		]

		// for lights with automatic switch off, only forward the ON state
		gLightAutoOff.members.forEach[lamp|
			val historicItem = lamp.historicState(JavaTimestamp_toGetHistStateOf)
			// if there is no historic item in case e.g. the item is added in the last 7 days
			if (historicItem !== null){
				val historicState = historicItem.state as OnOffType

				// only send ON command and only send it if current state is OFF
				if (lamp.state == OFF && historicState == ON) {
					lamp.sendCommand(ON)
					logInfo("Burgler Light","Burgler light mode switched ON auto-off-light: " + lamp.name)
				}
			}
		]
	}
end

rule "SwitchLightAutoMode_state switched on"
when
	Item SwitchLightAutoMode_state changed from OFF to ON
then
	Timestamp_AutoLightModeSwitchedON.sendCommand(now.toString())
end

burglerlight.items

Group     gLightManualOff
Group     gLightAutoOff
Switch    SwitchLightAutoMode_state            "Burgler Light: [%s]"    <wallswitch>
String    Timestamp_AutoLightModeSwitchedON

mapdb.persist

Strategies {
  default = everyUpdate
}

Items {
        Timestamp_AutoLightModeSwitchedON : strategy = everyChange, restoreOnStartup
}