Samsung AC NewGen experience

Hello,
my goal is to manage my Samsung air conditioner.
Thanks to the help of the forum, I wrote the following code and I can send the commands “power on” and “power off”. I also wrote a rule to activate a timer that checks the condition of the air conditioner in case it is controlled by the remote control. The latter, however, causing the switch to change if there is a difference between the detected state and that of the switch, creates a problem if I act on the switch and at the same time the timer intervenes. In practice, for example, if I press ON and the timer detects that the air conditioner is off, on its own initiative cancels my command and puts the switch back to the OFF state.
Do you have any ideas to help me?
Thank you

// *** SAMSUNG AC ITEMS ***

// Monolocale
Switch      SamsungAC_Power_Monolocale "Monolocale" <heating>
Number  	SamsungAC_Temp_Monolocale "[Temperatura %d]"
Switch      SamsungAC_Monolocale {channel="exec:command:samsungac_monolocale:run"}
String      SamsungAC_Args_Monolocale {channel="exec:command:samsungac_monolocale:input"}
String      SamsungAC_Out_Monolocale {channel="exec:command:samsungac_monolocale:output"}
// *** SAMSUNG AC RULES ***

// Monolocale
rule "Accensione/spegnimento Monolocale"
 when
     Item SamsungAC_Power_Monolocale changed
  then
                if(SamsungAC_Power_Monolocale.state == ON){
                        SamsungAC_Args_Monolocale.sendCommand("power on monolocale KBFz5iq6Wa 192.168.1.110")
                        val Number tempStatus = executeCommandLine("/etc/openhab2/samsungac/samsungac.sh temperature status monolocale KBFz5iq6Wa 192.168.1.110", 15000)
                        logDebug("SamsungAC","Lettura effettuata")
                        SamsungAC_Temp_Monolocale.postUpdate(tempStatus)
                }else{
                        SamsungAC_Args_Monolocale.sendCommand("power off monolocale KBFz5iq6Wa 192.168.1.110")
                        SamsungAC_Temp_Monolocale.sendCommand(OFF)
                }
                while(SamsungAC_Monolocale.state != ON) Thread::sleep(100)
end
rule "Power Status check (5 sec) Monolocale"
        when 
                Time cron "0/5 * * * * ?"
        then
                val String powerStatus = executeCommandLine("/etc/openhab2/samsungac/samsungac.sh power status monolocale KBFz5iq6Wa 192.168.1.110", 15000)
                while(powerStatus != "\"On\"" && powerStatus != "\"Off\""){
                        powerStatus = executeCommandLine("/etc/openhab2/samsungac/samsungac.sh power status monolocale KBFz5iq6Wa 192.168.1.110", 15000)
                        Thread::sleep(500) 
                }
                if(powerStatus == "\"On\"") {
                        if(SamsungAC_Power_Monolocale.state == OFF || SamsungAC_Power_Monolocale.state == NULL) {
                                powerStatus = executeCommandLine("/etc/openhab2/samsungac/samsungac.sh power status monolocale KBFz5iq6Wa 192.168.1.110", 15000)
                                if(powerStatus == "\"On\"") {
                                                SamsungAC_Power_Monolocale.postUpdate(ON)
                                }
                                
                        }
                }else{
                        if(SamsungAC_Power_Monolocale.state == ON || SamsungAC_Power_Monolocale.state == NULL) {
                                powerStatus = executeCommandLine("/etc/openhab2/samsungac/samsungac.sh power status monolocale KBFz5iq6Wa 192.168.1.110", 15000)
                                if(powerStatus == "\"Off\"") {
                                                SamsungAC_Power_Monolocale.postUpdate(OFF)
                                }
                        }
                }
end

Hello,
Could you edit your post with code fences, please
It will be much easier to read and understand

Thanks

Done.
Thanks

What is the content of: SamsungAC_Out_Monolocale? and how often do the binding pulls it?
Your while statements in the rules are holding the threads. Openhab has only 5 to play with.
I wonder if there is a better way to update your values directly from the binding without using the while statement.
What it the output of channel=“exec:command:samsungac_monolocale:output”?
Thanks

Thing exec:command:samsungac_monolocale [ command="/bin/sh /etc/openhab2/samsungac/samsungac.sh %2$s", interval=0, autorun=true ]
#!/bin/sh

arg="$1"
arg2="$2"
arg3="$3"
arg4="$4"
arg5="$5"

case "$arg" in

        power|Power|POWER)
                if [ "$arg2" = "on" ] || [ "$arg2" = "On" ] || [ "$arg2" = "ON" ] ; then
                curl -k -H "Content-Type: application/json" -H "Authorization: Bearer "$arg4"" --cert /etc/openhab2/samsungac/samsungac_"$arg3".pem --insecure -X PUT -d '{"Operation" : {"power" : "On"}}' https://"$arg5":8888/devices/0 
                fi
                if [ $arg2 = "off" ] || [ $arg2 = "Off" ] || [ $arg2 = "OFF" ] ; then
                curl -k -H "Content-Type: application/json" -H "Authorization: Bearer "$arg4"" --cert /etc/openhab2/samsungac/samsungac_"$arg3".pem --insecure -X PUT -d '{"Operation" : {"power" : "Off"}}' https://"$arg5":8888/devices/0
                fi
                if [ "$arg2" = "status" ] || [ "$arg2" = "Status" ] || [ "$arg2" = "STATUS" ] ; then
                power=`curl -s -k -H "Content-Type: application/json" -H "Authorization: Bearer "$arg4"" --cert /etc/openhab2/samsungac/samsungac_"$arg3".pem --insecure -X GET https://"$arg5":8888/devices|jq '.Devices[0].Operation.power'`
                echo "$power"
                fi
                ;;
        temperature|Temperature|TEMPERATURE)
                if [ "$arg2" != "status" ] && [ "$arg2" != "Status" ] && [ "$arg2" != "STATUS" ] ; then
                curl -k -H "Content-Type: application/json" -H "Authorization: Bearer "$arg4"" --cert /etc/openhab2/samsungac/samsungac_"$arg3".pem --insecure -X PUT -d '{"desired": ""$arg2"""}' https://"$arg5":8888/devices/0 
                else
                temp=`curl -s -k -H "Content-Type: application/json" -H "Authorization: Bearer "$arg4"" --cert /etc/openhab2/samsungac/samsungac_"$arg3".pem --insecure -X GET https://"$arg5":8888/devices|jq '.Devices[0].Temperatures[0].current'`
                echo $temp
                fi
                ;;
        mode|Mode|MODE)
                if [ $arg2 = "cool" ] || [ $arg2 = "Cool" ] || [ $arg2 = "COOL" ] ; then
                curl -s -X PUT -d '{"modes": ["Cool"]}' -k -H "Content-Type: application/json" -H "Authorization: Bearer KBFz5iq6Wa" --cert /etc/openhab2/services/cert.pem --insecure https://192.168.1.110:8888/devices/0/mode
                fi
                if [ $arg2 = "dry" ] || [ $arg2 = "Dry" ] || [ $arg2 = "DRY" ] ; then
                curl -s -X PUT -d '{"modes": ["Dry"]}' -k -H "Content-Type: application/json" -H "Authorization: Bearer KBFz5iq6Wa" --cert /etc/openhab2/services/cert.pem --insecure https://192.168.1.110:8888/devices/0/mode
                fi
                if [ $arg2 = "wind" ] || [ $arg2 = "Wind" ] || [ $arg2 = "WIND" ] ; then
                curl -s -X PUT -d '{"modes": ["Wind"]}' -k -H "Content-Type: application/json" -H "Authorization: Bearer KBFz5iq6Wa" --cert /etc/openhab2/services/cert.pem --insecure https://192.168.1.110:8888/devices/0/mode
                fi
                if [ $arg2 = "auto" ] || [ $arg2 = "Auto" ] || [ $arg2 = "AUTO" ] ; then
                curl -s -X PUT -d '{"modes": ["Auto"]}' -k -H "Content-Type: application/json" -H "Authorization: Bearer KBFz5iq6Wa" --cert /etc/openhab2/services/cert.pem --insecure https://192.168.1.110:8888/devices/0/mode
                fi
                if [ $arg2 = "heat" ] || [ $arg2 = "Heat" ] || [ $arg2 = "HEAT" ] ; then
                curl -s -X PUT -d '{"modes": ["Heat"]}' -k -H "Content-Type: application/json" -H "Authorization: Bearer KBFz5iq6Wa" --cert /etc/openhab2/services/cert.pem --insecure https://192.168.1.110:8888/devices/0/mode
                fi
                ;;
        purify|Purify|Purify)
                if [ $arg2 = "on" ] || [ $arg2 = "On" ] || [ $arg2 = "ON" ] ; then
                curl -s -X PUT -d '{"options": ["Spi_On"]}' -k -H "Content-Type: application/json" -H "Authorization: Bearer KBFz5iq6Wa" --cert /etc/openhab2/services/cert.pem --insecure https://192.168.1.110:8888/devices/0/mode
                fi
                if [ $arg2 = "off" ] || [ $arg2 = "Off" ] || [ $arg2 = "OFF" ] ; then
                curl -s -X PUT -d '{"options": ["Spi_Off"]}' -k -H "Content-Type: application/json" -H "Authorization: Bearer KBFz5iq6Wa" --cert /etc/openhab2/services/cert.pem --insecure https://192.168.1.110:8888/devices/0/mode
                fi
                ;;
        goodsleep|Goodsleep|GOODSLEEP)
                if [ $arg2 = "1" ] ; then
                curl -s -X PUT -d '{"options": ["Sleep_2"]}' -k -H "Content-Type: application/json" -H "Authorization: Bearer KBFz5iq6Wa" --cert /etc/openhab2/services/cert.pem --insecure https://192.168.1.110:8888/devices/0/mode
                fi
                if [ $arg2 = "2" ] ; then
                curl -s -X PUT -d '{"options": ["Sleep_4"]}' -k -H "Content-Type: application/json" -H "Authorization: Bearer KBFz5iq6Wa" --cert /etc/openhab2/services/cert.pem --insecure https://192.168.1.110:8888/devices/0/mode
                fi
                if [ $arg2 = "3" ] ; then
                curl -s -X PUT -d '{"options": ["Sleep_6"]}' -k -H "Content-Type: application/json" -H "Authorization: Bearer KBFz5iq6Wa" --cert /etc/openhab2/services/cert.pem --insecure https://192.168.1.110:8888/devices/0/mode
                fi
                if [ $arg2 = "4" ] ; then
                curl -s -X PUT -d '{"options": ["Sleep_8"]}' -k -H "Content-Type: application/json" -H "Authorization: Bearer KBFz5iq6Wa" --cert /etc/openhab2/services/cert.pem --insecure https://192.168.1.110:8888/devices/0/mode
                fi
                if [ $arg2 = "5" ] ; then
                curl -s -X PUT -d '{"options": ["Sleep_10"]}' -k -H "Content-Type: application/json" -H "Authorization: Bearer KBFz5iq6Wa" --cert /etc/openhab2/services/cert.pem --insecure https://192.168.1.110:8888/devices/0/mode
                fi
                if [ $arg2 = "6" ] ; then
                curl -s -X PUT -d '{"options": ["Sleep_12"]}' -k -H "Content-Type: application/json" -H "Authorization: Bearer KBFz5iq6Wa" --cert /etc/openhab2/services/cert.pem --insecure https://192.168.1.110:8888/devices/0/mode
                fi
                if [ $arg2 = "7" ] ; then
                curl -s -X PUT -d '{"options": ["Sleep_14"]}' -k -H "Content-Type: application/json" -H "Authorization: Bearer KBFz5iq6Wa" --cert /etc/openhab2/services/cert.pem --insecure https://192.168.1.110:8888/devices/0/mode
                fi
                if [ $arg2 = "8" ] ; then
                curl -s -X PUT -d '{"options": ["Sleep_16"]}' -k -H "Content-Type: application/json" -H "Authorization: Bearer KBFz5iq6Wa" --cert /etc/openhab2/services/cert.pem --insecure https://192.168.1.110:8888/devices/0/mode
                fi
                if [ $arg2 = "9" ] ; then
                curl -s -X PUT -d '{"options": ["Sleep_18"]}' -k -H "Content-Type: application/json" -H "Authorization: Bearer KBFz5iq6Wa" --cert /etc/openhab2/services/cert.pem --insecure https://192.168.1.110:8888/devices/0/mode
                fi
                if [ $arg2 = "10" ] ; then
                curl -s -X PUT -d '{"options": ["Sleep_20"]}' -k -H "Content-Type: application/json" -H "Authorization: Bearer KBFz5iq6Wa" --cert /etc/openhab2/services/cert.pem --insecure https://192.168.1.110:8888/devices/0/mode
                fi
                ;;
esac

The binding is only an sh…

You could definne a binding for

val Number tempStatus = executeCommandLine("/etc/openhab2/samsungac/samsungac.sh temperature status monolocale KBFz5iq6Wa 192.168.1.110", 15000)
instead of pulling it in an action and updating the value

Same for powerStatus = executeCommandLine("/etc/openhab2/samsungac/samsungac.sh power status monolocale KBFz5iq6Wa 192.168.1.110", 15000)

These values can be updated automatically by the binding and your rules will be much simpler without holding on the threads

I’ll try!!!
Thanks

I don’t have experience with the version2 of the exec binding so I couldn’t guide you any further down that path. But when you get your values updated directly from the binding at intervals that suit you (5s, 1min…) then come back, and we’ll work on the rules. I bet you that they’ll be far simpler…
Good luck

Hi here’s my two cents hope it helps.

I think could be useful to change your rule and pay attention on the difference between sendCommand and postUpdate.

So in your first rule try to change the when clause from when “changed” to “received command” , this way the command will fire only when you manually trigger the switch, so when the “Power Status check …” rule fires, the “postUpdate” will only change the value of the switch without triggering again the command.