Page 1 of 1

Selector switch action mqtt

Posted: Saturday 13 January 2024 21:24
by PeJeWe
Hi,

I have created a virtual switch with a selector type
is it possible to send a mqtt message with a selector action?
the action i want to send is:

Code: Select all

mosquitto_pub -t "domoticz/climate/airco/fan_mode/command" -m "auto"
Only domoticz requires to use only "Should start with http://, https:// or script://"

How can i send commands like these .

Re: Selector switch action mqtt

Posted: Saturday 13 January 2024 21:55
by waltervl
Put it into a bash script that can accept variables. https://www.domoticz.com/wiki/Scripting ... tch_action

Re: Selector switch action mqtt

Posted: Saturday 13 January 2024 22:06
by PeJeWe
I'am not that good in scripting.

this is the message from mqtt regarding the airco

Code: Select all

{
  "curr_temp_t": "domoticz//climate/airco/current_temperature/state",
  "mode_cmd_t": "domoticz//climate/airco/mode/command",
  "mode_stat_t": "domoticz//climate/airco/mode/state",
  "modes": [
    "off",
    "cool",
    "heat",
    "fan_only",
    "dry",
    "heat_cool"
  ],
  "temp_cmd_t": "domoticz//climate/airco/target_temperature/command",
  "temp_stat_t": "domoticz//climate/airco/target_temperature/state",
  "min_temp": 16,
  "max_temp": 32,
  "temp_step": 1,
  "temp_unit": "C",
  "min_hum": 30,
  "max_hum": 99,
  "pr_mode_cmd_t": "domoticz//climate/airco/preset/command",
  "pr_mode_stat_t": "domoticz//climate/airco/preset/state",
  "preset_modes": [
    "sleep",
    "Antifungus",
    "Clean",
    "Health"
  ],
  "act_t": "domoticz//climate/airco/action/state",
  "fan_mode_cmd_t": "domoticz//climate/airco/fan_mode/command",
  "fan_mode_stat_t": "domoticz//climate/airco/fan_mode/state",
  "fan_modes": [
    "auto",
    "low",
    "medium",
    "high",
    "mute",
    "turbo"
  ],
  "swing_mode_cmd_t": "domoticz//climate/airco/swing_mode/command",
  "swing_mode_stat_t": "domoticz//climate/airco/swing_mode/state",
  "swing_modes": [
    "off",
    "both",
    "vertical",
    "horizontal"
  ],
Can you give me a start-up?

Re: Selector switch action mqtt

Posted: Saturday 13 January 2024 22:17
by waltervl
See for example viewtopic.php?t=25275

Re: Selector switch action mqtt

Posted: Saturday 20 January 2024 11:27
by PeJeWe
I have been busy to build this:

Code: Select all

#!/bin/bash

# MQTT Broker instellingen
BROKER="IPadresBBROKER"
PORT="1883"
USERNAME="USERNAME_BROKER"
PASSWORD="PASSWORD_BROKER"

# Topics for different commands
MODE_TOPIC="domoticz//climate/airco/mode/command"
FAN_MODE_TOPIC="domoticz//climate/airco/fan_mode/command"
SWING_MODE_TOPIC="domoticz//climate/airco/swing_mode/command"
PR_MODE_TOPIC="domoticz//climate/airco/preset/command"
TEMP_TOPIC="domoticz//climate/airco/target_temperature/command"

# Array of valid modes
VALID_MODES=(off cool heat fan_only dry heat_cool auto low medium high mute turbo)

# Array of valid preset modes
VALID_PR_MODES=(sleep Antifungus Clean Health)

# Array of valid swing modes
VALID_SWING_MODES=(off both vertical horizontal)

# Function to publish an MQTT message
publish_mqtt() {
  local topic=$1
  local message=$2
  mosquitto_pub -h $BROKER -p $PORT -u $USERNAME -P $PASSWORD -t $topic -m "$message"
}

# Validate the number of arguments
case $# in
  0)
    echo "Usage: $0 <mode1> [<mode2> <mode3> ...]"
    exit 1
    ;;
esac

# Loop through all provided modes
for MODE in "$@"; do
  # Check if the mode is valid
  if [[ " ${VALID_MODES[@]} " =~ " $MODE " ]]; then
    # Send the appropriate command
    case $MODE in
      off)
        # Send mode command for turning off the entire device
        publish_mqtt $MODE_TOPIC $MODE
        ;;
      cool|heat|fan_only|dry|heat_cool)
        # Send mode command for specific modes
        publish_mqtt $MODE_TOPIC $MODE
        ;;
      auto|low|medium|high|mute|turbo)
        # Send fan mode command
        publish_mqtt $FAN_MODE_TOPIC $MODE
        ;;
      *)
        echo "Invalid mode. Allowed values: ${VALID_MODES[@]}"
        exit 1
        ;;
    esac
  elif [[ " ${VALID_PR_MODES[@]} " =~ " $MODE " ]]; then
    # Send the preset mode command
    publish_mqtt $PR_MODE_TOPIC $MODE
  elif [[ " ${VALID_SWING_MODES[@]} " =~ " $MODE " ]]; then
    # Send the swing mode command
    publish_mqtt $SWING_MODE_TOPIC $MODE
  elif [[ $MODE == "off_swing" ]]; then
    # Stop the swing mode
    publish_mqtt $SWING_MODE_TOPIC "off"
  elif [[ $MODE == "temp" ]]; then
    # Set the temperature command with a 5-second delay
    TEMP=$2
    sleep 5
    publish_mqtt $TEMP_TOPIC $TEMP
    shift
  else
    # Print an error message and exit
    echo "Invalid mode. Allowed values: ${VALID_MODES[@]} ${VALID_PR_MODES[@]} ${VALID_SWING_MODES[@]} off off_swing temp"
    exit 1
  fi
done
i have made virtual switches and set names.
you can give commands like

Code: Select all

airco.sh off`: Turn off the air conditioner.
airco.sh cool`: Set the air conditioner to cooling mode.
airco.sh heat`: Set the air conditioner to heating mode.
airco.sh fan_only`: Set the air conditioner to fan mode.
airco.sh dry`: Set the air conditioner to dehumidification mode.
airco.sh heat_cool`: Set the air conditioner to heating and cooling mode.
airco.sh auto`: Set the fan speed of the air conditioner to automatic.
airco.sh low`: Set the fan speed of the air conditioner to low.
airco.sh medium`: Set the fan speed of the air conditioner to medium.
airco.sh high`: Set the fan speed of the air conditioner to high.
airco.sh mute`: Set the fan speed of the air conditioner to silent.
airco.sh turbo`: Set the fan speed of the air conditioner to turbo.
airco.sh sleep`: Set the air conditioner to sleep mode.
airco.sh antifungus`: Set the air conditioner to anti-fungal mode.
airco.sh clean`: Set the air conditioner to cleaning mode.
airco.sh health`: Set the air conditioner to health mode.
airco.sh swing off`: Turn off the swing mode.
airco.sh swing both`: Set the swing mode for both vertical and horizontal louvers.
airco.sh swing vertical`: Set the swing mode for the vertical louvers.
airco.sh swing horizontal`: Set the swing mode for the horizontal louvers.
and airco.sh cool medium temp 16
and airco.sh cool medium temp 16
the temp setting has a delay of 5 seconds because with my airco you can set the temp not directly
This is working now for me, maybe it can be of help for others.