Samsung SmartThings Washer → MQTT → Domoticz Integration Guide

All kinds of 'OS' scripts

Moderator: leecollings

Post Reply
mojso
Posts: 95
Joined: Friday 08 November 2019 23:07
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Samsung SmartThings Washer → MQTT → Domoticz Integration Guide

Post by mojso »

# Samsung SmartThings Washer → MQTT → Domoticz Integration Guide

This guide shows how to integrate a Samsung SmartThings washing machine with Domoticz using MQTT and SmartThings CLI on Raspberry Pi/Linux.

The setup provides:

* Washer status
* Wash program detection
* Remaining time
* Progress percentage
* Power consumption
* Total energy usage
* MQTT integration
* Automatic background service
* Auto start after reboot

Tested with Samsung WW80CGC0EDA/ELE and Raspberry Pi OS.

---

# Requirements

* Raspberry Pi / Linux server
* Domoticz installed
* Internet connection
* Samsung washer connected to SmartThings
* MQTT enabled in Domoticz
* SmartThings account

---

# Credits

Integration created using:

* SmartThings CLI
* Mosquitto MQTT
* Bash scripting
* Domoticz MQTT JSON API

This guide is intended for Domoticz forum users and home automation enthusiasts.

---

This guide explains how to integrate a Samsung washing machine from SmartThings into Domoticz using MQTT on Raspberry Pi/Linux.

Features:

* Washer status
* Program name
* Remaining time
* Progress %
* Power consumption
* Energy monitoring
* Automatic updates
* Auto-start service

---

# 1. Install Node.js and npm

Check if already installed:

Code: Select all

node -v
npm -v
If not installed:

Code: Select all

sudo apt update
sudo apt install nodejs npm -y
---

# 2. Install SmartThings CLI

Install SmartThings CLI globally:

Code: Select all

sudo npm install -g @smartthings/cli
Verify installation:

Code: Select all

smartthings --version
---

# 3. Login to SmartThings

Run:

Code: Select all

smartthings login
A browser window will open for authentication.

---

# 4. Find Your Washer Device ID

List SmartThings devices:

Code: Select all

smartthings devices
Example output:

Code: Select all

Name                 Device ID
Samsung Washer       xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
Copy the washer Device ID.

---

# 5. Install jq

Code: Select all

sudo apt install jq -y
---

# 6. Install MQTT Broker

Install Mosquitto MQTT broker and clients:

Code: Select all

sudo apt install mosquitto mosquitto-clients -y
Enable and start Mosquitto:

Code: Select all

sudo systemctl enable mosquitto
sudo systemctl start mosquitto
---

# 7. Create the Washer Script

Create the script file:

Code: Select all

nano /home/pi/washer.sh
Paste the following script:

Code: Select all

#!/bin/bash

DEVICE_ID="YOUR DEVICE ID"
BROKER="localhost"

STATUS_IDX=1992
JOB_IDX=1993
PROGRESS_IDX=1994
SWITCH_IDX=1995
PRG_IDX=1998
RMTIME_IDX=2014
ELECTRIC_IDX=2018
#ENERGY_FILE="/home/pi/scripts/washer_energy.txt"
LAST=""

while true
do
  DATA=$(smartthings devices:status $DEVICE_ID --json)

  STATUS=$(echo "$DATA" | jq -r '.components.main["samsungce.washerOperatingState"].operatingState.value')
  JOB=$(echo "$DATA" | jq -r '.components.main["samsungce.washerOperatingState"].washerJobState.value')
  PROGRESS=$(echo "$DATA" | jq -r '.components.main["samsungce.washerOperatingState"].progress.value // 0')
  RMTIME=$(echo "$DATA" | jq -r '.components.main["samsungce.washerOperatingState"].remainingTimeStr.value // 0')
  SW=$(echo "$DATA" | jq -r '.components.main["samsungce.switch"].switch.value')
  PRG=$(echo "$DATA" |jq -r '.components.main["custom.supportedOptions"].course.value // "unknown"')
  ENERGY=$(echo "$DATA" | jq -r '.components.main.powerConsumptionReport.powerConsumption.value.energy // 0')
  POWER=$(echo "$DATA" | jq -r '.components.main.powerConsumptionReport.powerConsumption.value.power // 0')
 # DELTAENERGY=$(echo "$DATA" | jq -r '.components.main.powerConsumptionReport.powerConsumption.value.deltaEnergy')
   
   [ -z "$POWER" ] && POWER=0
   [ -z "$ENERGY" ] && ENERGY=0
   
  CURRENT="$STATUS|$JOB|$PROGRESS|$SW|$PRG|$RMTIME"
  CURRENT2="$ENERGY|$POWER"

  if [ "$CURRENT" != "$LAST" ]; then
    echo "Status: $CURRENT|$CURRENT2"

    # -----------------------------
    # 📝 STATUS (TEXT)
    # -----------------------------
    mosquitto_pub -h $BROKER -t domoticz/in -m "{
      \"idx\": $STATUS_IDX,
      \"nvalue\": 0,
      \"svalue\": \"$STATUS\"
    }"

    # -----------------------------
    # 🧺 JOB STATE (TEXT)
    # -----------------------------
    mosquitto_pub -h $BROKER -t domoticz/in -m "{
      \"idx\": $JOB_IDX,
      \"nvalue\": 0,
      \"svalue\": \"$JOB\"
    }"

    # -----------------------------
    # 🧺 REMAINING TIME (TEXT)
    # -----------------------------
    
    # Convert HH:MM to minutes
    if [[ "$RMTIME" =~ ^([0-9]{2}):([0-9]{2})$ ]]; then
    HOURS=${BASH_REMATCH[1]}
    MINS=${BASH_REMATCH[2]}
    TOTAL_MIN=$((10#$HOURS * 60 + 10#$MINS))
    else
    TOTAL_MIN=0
    fi
    
    if [ "$STATUS" = "running" ] || [ "$STATUS" = "paused" ]; then
    RT=$TOTAL_MIN
      else
      RT=0
    fi
    
    mosquitto_pub -h $BROKER -t domoticz/in -m "{
      \"idx\": $RMTIME_IDX,
      \"nvalue\": 0,
      \"svalue\": \"$RT\"
    }"

    # -----------------------------
    # 📊 PROGRESS (%)
    # -----------------------------
    mosquitto_pub -h $BROKER -t domoticz/in -m "{
      \"idx\": $PROGRESS_IDX,
      \"nvalue\": 0,
      \"svalue\": \"$PROGRESS\"
    }"

    # -----------------------------
    # ⚡ PROGRAM
    # -----------------------------
    Pname="UNKNOWN ($PRG)"

    case "$PRG" in
      "1C") Pname="ECO 40-60" ;;
      "1B") Pname="COTTON" ;;
      "25") Pname="SYNTHETICS" ;;
      "20") Pname="HYGIENE STEAM" ;;
      "08") Pname="RINSE + SPIN" ;;
      "74") Pname="DRUM CLEAN" ;;
      "87") Pname="DOWNLOADED" ;;
      "06") Pname="BEDDING" ;;
      "7F") Pname="WOOL/DELICATES" ;;
      "65") Pname="COLOURS" ;;
      "8F") Pname="INTENSE COLD" ;;
      "96") Pname="LESS MICROFIBER" ;;
      "34") Pname="MIXED LOAD" ;;
      "A0") Pname="QUICK WASH 15min" ;;
     esac

    mosquitto_pub -h $BROKER -t domoticz/in -m "{
      \"idx\": $PRG_IDX,
      \"nvalue\": 0,
      \"svalue\": \"$Pname\"
    }"

    # -----------------------------
    # ⚡ SWITCH (ON/OFF)
    # -----------------------------
    if [ "$SW" = "on" ]; then
      N=1
    else
      N=0
    fi

    mosquitto_pub -h $BROKER -t domoticz/in -m "{
      \"idx\": $SWITCH_IDX,
      \"nvalue\": $N,
      \"svalue\": \"\"
    }"

# -----------------------------
    # ⚡ CONSUMPTION 
    # -----------------------------
    #PREV_ENERGY=$(cat $ENERGY_FILE 2>/dev/null || echo "0")
     #DELTA_ENERGY=$(echo "scale=3; $ENERGY - $PREV_ENERGY" | bc)
    # [ -z "$DELTA_ENERGY" ] && DELTA_ENERGY=0
   
    # ако е негативно (reset), стави 0
    #if (( $(echo "$DELTA_ENERGY < 0" | bc -l) )); then
    #DELTA_ENERGY=0
   #fi
   # echo "$ENERGY" > $ENERGY_FILE 
 
 
  mosquitto_pub -h $BROKER -t domoticz/in -m "{
  
  \"idx\": $ELECTRIC_IDX,
  \"nvalue\": 0,
  \"svalue\": \"$POWER;$ENERGY\"
}"



    LAST="$CURRENT"
  fi
    

if [ "$SW" = "off" ]; then
  SLEEP_TIME=900   # 20 минути 1200
elif [ "$SW" = "on" ]; then
  SLEEP_TIME=60     # 1 минута
else
  SLEEP_TIME=120    # fallback
fi
echo "Next check in $SLEEP_TIME sec"
#echo "$POWER | $ENERGY"
  sleep $SLEEP_TIME
  
done

Save and exit.

---

# 8. Make Script Executable

Code: Select all

chmod +x /home/pi/washer.sh
---

# 9. Test the Script

Run manually:

Code: Select all

/home/pi/washer.sh
Example output:

Code: Select all

Status: running|spin|45|on
Next check in 60 sec
---

# 10. Create Virtual Devices in Domoticz

Open:

Code: Select all

Setup → Hardware
Add:

* Dummy Hardware

---

# 11. Create Virtual Sensors

## STATUS

Type:

* Text

---

## JOB STATE

Type:

* Text

---

## PROGRAM

Type:

* Text

---

## REMAINING TIME

Type:

* Custom Sensor

Unit:

Code: Select all

min
---

## PROGRESS

Type:

* Percentage

---

## SWITCH

Type:

* Switch

---

## POWER + ENERGY

Type:

* Electric (Instant + Counter)

IMPORTANT:

* Data source must be:

Code: Select all

From Device
---

# 12. Get Device IDX Values

Open:

Code: Select all

Setup → Devices
Find IDX values for each virtual device.

Example:

Code: Select all

1992
1993
1994
Update your script variables:

Code: Select all

STATUS_IDX=1992
JOB_IDX=1993
---

# 13. MQTT Test

Test Domoticz MQTT input:

Code: Select all

mosquitto_pub -h localhost -t domoticz/in -m '{"idx":1992,"nvalue":0,"svalue":"running"}'
If configured correctly, the Domoticz device should update instantly.

---

# 14. Create systemd Service

Create service file:

Code: Select all

sudo nano /etc/systemd/system/washer.service
Paste:

Code: Select all

[Unit]
Description=Samsung Washer Monitor
After=network.target

[Service]
ExecStart=/home/pi/washer.sh
Restart=always
User=pi

[Install]
WantedBy=multi-user.target
Save and exit.

---

# 15. Enable and Start the Service

Reload systemd:

Code: Select all

sudo systemctl daemon-reload
Enable auto-start:

Code: Select all

sudo systemctl enable washer.service
Start service:

Code: Select all

sudo systemctl start washer.service
---

# 16. Check Service Status

Check status:

Code: Select all

sudo systemctl status washer.service
View live logs:

Code: Select all

journalctl -u washer.service -f
---

# 17. Restart the Service

Code: Select all

sudo systemctl restart washer.service
---

# 18. Stop the Service

Code: Select all

sudo systemctl stop washer.service
---

# 19. Verify Auto Start

Check if enabled:

Code: Select all

systemctl is-enabled washer.service
---

# 20. Check SmartThings JSON Data

Code: Select all

smartthings devices:status DEVICE_ID --json
---

# 21. Useful MQTT Debugging Commands

Monitor MQTT messages:

Code: Select all

mosquitto_sub -v -t domoticz/in
Check if service is running:

Code: Select all

systemctl is-active washer.service
---

# 22. Final Result

After completing the setup, Domoticz will display:

* Washer ON/OFF state
* Current washer status
* Current wash cycle
* Remaining time in minutes
* Progress percentage
* Instant power usage
* Total energy usage
* Historical energy graphs
* Automatic updates via MQTT
* Automatic startup after reboot

The washer integration runs fully automatically in the background using systemd.

---

# Troubleshooting

## MQTT messages not updating

Check Mosquitto service:

Code: Select all

sudo systemctl status mosquitto
Monitor MQTT traffic:

Code: Select all

mosquitto_sub -v -t domoticz/in
---

## SmartThings CLI not working

Re-login:

Code: Select all

smartthings login
Check available devices:

Code: Select all

smartthings devices
---

## Washer service not starting

Check logs:

Code: Select all

journalctl -u washer.service -f
Restart service:

Code: Select all

sudo systemctl restart washer.service
---

## Domoticz energy graph incorrect

Use:

Code: Select all

Electric (Instant + Counter)
and select:

Code: Select all

From Device
Do NOT use:

* Computed
* Incremental Counter
* RFXMeter

---

# Notes

* ENERGY value must be total cumulative energy from SmartThings.
* POWER value must be current live power in Watts.
* Domoticz expects:

Code: Select all

POWER;TOTAL_ENERGY
Example:

Code: Select all

250;1.523
---

# Forum Notes

This setup uses polling through SmartThings CLI.

Recommended polling intervals:

| Washer State | Polling Time |
| ------------ | ------------ |
| OFF | 900 sec |
| ON / Running | 60 sec |
| Fallback | 120 sec |

This minimizes API calls while still providing responsive updates.

---

# End of Guide

You now have:

* Live washer status
* Active wash program
* Remaining time
* Progress percentage
* Power consumption monitoring
* Energy history graphs
* Automatic MQTT updates
* Auto-start after reboot

fully integrated into Domoticz.
Post Reply