[SOLVED] Resend current item's state

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).

Actually, I’m using this code:

if (Relay4.state == OFF) && (HabpanelBattery.state == "ECO")  {
                            Relay3.sendCommand(ON) }

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

I’ve updated my code following your instructions:



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

What does the item file look like.

Updated with Relay:


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

No more log error… but nothing happened (Relay… no changes & no related log report).

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.

1 Like

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".

I didn’t notice the else if that was added by the OP.:hushed: Thanks for pointing that out.:+1:

@TomaxMe here’s a link to help with if, else and else if statements.
https://www.w3schools.com/js/js_if_else.asp
I keep this bookmarked.:wink:

You have two identical case statements…

    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"
        }
    }

I’ve got this error after correcting:

[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

    var OnOffType Relay3_new_state = NULL
    var OnOffType Relay4_new_state = NULL

Because you first instantiate Relay3_new_state as a string, which you really shouldn’t do, change that into:

var OnOffType Relay3_new_state 
var OnOffType Relay4_new_state 

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

H102… I have 4 cases: ON-ON, ON-OFF for each relay.
Update: uhhh… now I’ve saw that. I’ll correct it right now.,

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:

Relay3_new_state = Relay3_state
Relay4_new_state = Relay4_state

And change:

    var OnOffType Relay3_state = Relay3.state
    var OnOffTypre Relay4_state = Relay4.state

Why so complicated…

rule "ping ArduinoSwitchInverter"
    when Item ping_192_168_1_10_online changed  // test: receive update [<state>] OR receive command [<command>]
then
    if (ping_192_168_1_10_online.state == ON) {
        logInfo("Alert:", "ArduinoSwitchInverter changed to ON")
        Relay3.sendCommand(Relay3.state.toString)
        Relay4.sendCommand(Relay4.state.toString)
    }
    else logInfo("Alert: ", "ArduinoSwitchInverter changed to OFF")
end

My thoughts exactly when reviewing how this topic has been developing.
However, without .toString :slight_smile:

:slight_smile:
Yeap, it’s working well (changing the relay via MQTT).

Also, I’ve got that error

[WARN ] [rthome.model.script.actions.BusEvent] - Cannot convert 'NULL' to a command type which item 'Relay4' accepts: [OnOffType, RefreshType].

Which one?