Thanks @pacive. Rookie error! It shows I haven’t really touched OH for quite a while and just recently started upgrading/migrating.
Using the InfluxDB persistence made the error go away. So, Ruby-wise it is ok now, Although the error is a bit misleading as it refers the a method called state
.
@broconne From my tests it seems previous_state
is just the current state. I tested it with InfluxDB persistence. There are a few persisted states in the database:
$ influx query 'from(bucket:"openhab") |> range(start:-1d)' | grep -i light_scene_office
2023-01-03T15:42:35.220953448Z 2023-01-04T15:42:35.220953448Z value Light_Scene_office Light_Scene_office 2023-01-04T15:04:34.964000000Z OFF
2023-01-03T15:42:35.220953448Z 2023-01-04T15:42:35.220953448Z value Light_Scene_office Light_Scene_office 2023-01-04T15:04:37.881000000Z EVENING
2023-01-03T15:42:35.220953448Z 2023-01-04T15:42:35.220953448Z value Light_Scene_office Light_Scene_office 2023-01-04T15:04:43.948000000Z OFF
2023-01-03T15:42:35.220953448Z 2023-01-04T15:42:35.220953448Z value Light_Scene_office Light_Scene_office 2023-01-04T15:09:30.533000000Z EVENING
2023-01-03T15:42:35.220953448Z 2023-01-04T15:42:35.220953448Z value Light_Scene_office Light_Scene_office 2023-01-04T15:10:25.995000000Z OFF
2023-01-03T15:42:35.220953448Z 2023-01-04T15:42:35.220953448Z value Light_Scene_office Light_Scene_office 2023-01-04T15:21:12.067000000Z EVENING
2023-01-03T15:42:35.220953448Z 2023-01-04T15:42:35.220953448Z value Light_Scene_office Light_Scene_office 2023-01-04T15:21:18.004000000Z OFF
2023-01-03T15:42:35.220953448Z 2023-01-04T15:42:35.220953448Z value Light_Scene_office Light_Scene_office 2023-01-04T15:21:30.059000000Z EVENING
2023-01-03T15:42:35.220953448Z 2023-01-04T15:42:35.220953448Z value Light_Scene_office Light_Scene_office 2023-01-04T15:42:26.109000000Z OFF
2023-01-03T15:42:35.220953448Z 2023-01-04T15:42:35.220953448Z value Light_Scene_office Light_Scene_office 2023-01-04T15:42:29.999000000Z EVENING
The item:
String Light_Scene_office "Lights Office" <light> (gOffice,gHist0)
The test rule:
rule 'Hue Dimmer Switch Key triggered' do
channel [
'dim_office:dimmer_switch_event',
# ... etc ...
],
thing: 'hue:0820:0017582dc5b3',
triggered: ['1000.0', '1002.0']
run do |event|
lightScene = items['Light_Scene_' + event.channel.to_s.split(':')[3].split('_')[1]]
logger.info "Hue #{lightScene.name} state is #{lightScene} with trigger #{event.get_event}"
logger.info "Previous state: #{lightScene.previous_state}"
end
end
Triggering event 1000.0 logs this:
[INFO ] [ort.loader.AbstractScriptFileWatcher] - Loading script '/openhab/conf/automation/jsr223/ruby/personal/hue_sw_office.rb'
[INFO ] [fice.hue_dimmer_switch_key_triggered] - Hue Light_Scene_office state is EVENING with trigger 1000.0
[INFO ] [fice.hue_dimmer_switch_key_triggered] - Previous state: EVENING
Both .state
and .previous_state
are the current state, so it seems. Is InfluxDB supported as persistence service for this use case? Or should I use another persistence service?
UPDATE: I removed all persistence from the Light_Scene_office
item and get a correct value for .state
and .previousState
after changing the state a few times between ‘OFF’ and ‘EVENING’ from the Karaf shell.
[INFO ] [fice.hue_dimmer_switch_key_triggered] - Hue Light_Scene_office state is EVENING with trigger 1000.0
[INFO ] [fice.hue_dimmer_switch_key_triggered] - Previous state: OFF
String Light_Scene_office "Lights Office" <light> (gOffice)
Looks like .previous_state
comes from the OpenHAB Item object in memory in this case.
However, after toggling the status with:
lightScene << lightScene.previous_state
it changes to the previous state once and stays that way on subsequent Hue channel triggers:
[INFO ] [fice.hue_dimmer_switch_key_triggered] - Hue Light_Scene_office state is EVENING with trigger 1002.0
[INFO ] [fice.hue_dimmer_switch_key_triggered] - Previous state: OFF
[INFO ] [fice.hue_dimmer_switch_key_triggered] - Hue Light_Scene_office state is OFF with trigger 1002.0
[INFO ] [fice.hue_dimmer_switch_key_triggered] - Previous state: OFF
[INFO ] [fice.hue_dimmer_switch_key_triggered] - Hue Light_Scene_office state is OFF with trigger 1000.0
[INFO ] [fice.hue_dimmer_switch_key_triggered] - Previous state: OFF