What is the command to re-send the actual state of an item?
My goal is to resend via MQTT the state of some items (relays) when the Arduino board is reconnected to the network (detected by PING via Network Binding).
Now I want to include the re-sending command into this code:
rule "ping ArduinoSwitchInverter"
when Item ping_192_168_1_10_online changed
then
if (ping_192_168_1_10_online.state == ON) {
logInfo("Alert:", "ArduinoSwitchInverter changed to ON")
//here I want to re-send to Arduino the actual state of item "Relay4"
}
else logInfo("Alert: ", "ArduinoSwitchInverter changed to OFF")
end
I’m focus on using postUpdate(Relay4… ) but I don’t know how to add here the current status of the item involved.
I haven’t tested this but you can try it and see what the logs say.
rule "ping ArduinoSwitchInverter"
when Item ping_192_168_1_10_online changed
then
var relay_state = Relay4.state
var new_state = "UNKOWN"
if (ping_192_168_1_10_online.state == ON)
logInfo("Alert:", "ArduinoSwitchInverter changed to ON")
switch true {
case relay_state == ON : new_state = "ON"
case relay_state == OFF : new_state = "OFF"
}
if(relay_state.state != new_state){
Relay4.sendCommand(new_state)
}
//here I want to re-send to Arduino the actual state of item "Relay4"
else logInfo("Alert: ", "ArduinoSwitchInverter changed to OFF")
end
rule "ping ArduinoSwitchInverter"
when Item ping_192_168_1_10_online changed // test: receive update [<state>] OR receive command [<command>]
then
var relay3_state = Relay3.state
var relay4_state = Relay4.state
var relay3_new_state = "UNKNOWN"
var relay4_new_state = "UNKNOWN"
if (ping_192_168_1_10_online.state == ON) {
logInfo("Alert:", "ArduinoSwitchInverter changed to ON")
switch true {
case relay3_state == ON : relay3_new_state = "ON"
case relay3_state == OFF: relay3_new_state = "OFF"
case relay4_state == ON : relay4_new_state = "ON"
case relay4_state == ON : relay4_new_state = "OFF"
}
}
else if (relay3_state.state != relay3_new_state){
Relay3.sendCommand(relay3_new_state)
}
else if (relay4_state.state != relay4_new_state){
Relay4.sendCommand(relay4_new_state)
}
else logInfo("Alert: ", "ArduinoSwitchInverter changed to OFF")
end
Got this log error:
==> /var/log/openhab2/openhab.log <==
2019-01-18 15:52:59.401 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'ping ArduinoSwitchInverter': 'state' is not a member of 'org.eclipse.smarthome.core.library.types.OnOffType'; line 22, column 14, length 18
The easiest way would be to persist the topic when the command is sent. Then the arduino would automatically receive the topic and last command when reconnecting.
Relay3_state is already a state, so you shouldn’t do Relay3_state.state in your else if part (same for Relay4), just remove the .state.
Furthermore the else if statements make no sense, they should be just if statements.
Furthermore you should set Relay3_new_state to ON or OFF, not to "ON" or "OFF".
[WARN ] [rthome.model.script.actions.BusEvent] - Cannot convert '' to a state type which item 'Relay4' accepts: [OnOffType, UnDefType].
The corrrected code:
rule "ping ArduinoSwitchInverter"
when Item ping_192_168_1_10_online changed // test: receive update [<state>] OR receive command [<command>]
then
var Relay3_state = Relay3.state
var Relay4_state = Relay4.state
var Relay3_new_state = ""
var Relay4_new_state = ""
if (ping_192_168_1_10_online.state == ON) {
logInfo("Alert:", "ArduinoSwitchInverter changed to ON")
switch true {
case Relay3_state == ON : Relay3_new_state = ON
case Relay3_state == OFF: Relay3_new_state = OFF
case Relay4_state == ON : Relay4_new_state = ON
case Relay4_state == ON : Relay4_new_state = OFF
}
}
if (Relay3_state != Relay3_new_state){
Relay3.postUpdate(Relay3_new_state)
}
if (Relay4_state != Relay4_new_state){
Relay4.postUpdate(Relay4_new_state)
}
else logInfo("Alert: ", "ArduinoSwitchInverter changed to OFF")
end
As Vincent mentioned above, you have two identical case statements for Relay4
switch true {
case Relay3_state == ON : Relay3_new_state = ON
case Relay3_state == OFF: Relay3_new_state = OFF
case Relay4_state == ON : Relay4_new_state = ON
case Relay4_state == ON : Relay4_new_state = OFF
2019-01-18 16:18:54.847 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'ping ArduinoSwitchInverter': An error occurred during the script execution: Could not invoke method: org.eclipse.smarthome.model.script.actions.BusEvent.sendCommand(org.eclipse.smarthome.core.items.Item,org.eclipse.smarthome.core.types.Command) on instance: null
The updated code is:
rule "ping ArduinoSwitchInverter"
when Item ping_192_168_1_10_online changed // test: receive update [<state>] OR receive command [<command>]
then
var Relay3_state = Relay3.state
var Relay4_state = Relay4.state
var OnOffType Relay3_new_state = NULL
var OnOffType Relay4_new_state = NULL
if (ping_192_168_1_10_online.state == ON) {
logInfo("Alert:", "ArduinoSwitchInverter changed to ON")
switch true {
case Relay3_state == ON : Relay3_new_state = ON
case Relay3_state == OFF: Relay3_new_state = OFF
case Relay4_state == ON : Relay4_new_state = ON
case Relay4_state == OFF : Relay4_new_state = OFF
}
}
if (Relay3_state != Relay3_new_state){
Relay3.sendCommand(Relay3_new_state)
}
if (Relay4_state != Relay4_new_state){
Relay4.sendCommand(Relay4_new_state)
}
else logInfo("Alert: ", "ArduinoSwitchInverter changed to OFF")
end
switch true {
case Relay3_state == ON : Relay3_new_state = ON
case Relay3_state == OFF: Relay3_new_state = OFF
case Relay4_state == ON : Relay4_new_state = ON
case Relay4_state == ON : Relay4_new_state = OFF
}
}
That won’t work, it will only do one of the cases, the first one that watches on the list
replace with: