Mute speakers for

Switches the configured speakers to mute for a configured amount of seconds and then back on. That’s particularly useful when ads start to play.

Speaker things usually have a channel mute of type switch. It mutes the speaker when it’s switched on. To use it, you’ll need to link it to an item and and control the mute state with it.

This rule template controls the mute item of one or multiple speakers to switch them to mute for a configured time and then back on. You can configure these items with the rule template parameter Speaker Mute Switches.

When you want to switch the speakers to mute, you need a second switch. Create an item of type switch and configure it with the rule template parameter Mute For Switch.

There are two options to configure the mute duration. Either you can set the duration in seconds with the template parameter Mute Duration but then it can only be changed with deleting and recreating the rule.
The second option is to use an item the mute duration is read from. Create a new item of type Number and configure it with the template parameter Mute duration item. Best configure it as a slider item to easily adjust the duration if needed.

After creating the rule, give it a try. Play some music and then toggle the item configured as Mute For Switch to on. The speaker should be muted and continue to play after the configured time.

Language: ECMAScript (ECMAScript 262 Edition 11)
Dependencies: JavaScript Scripting


Version 0.1

  • initial release


uid: prosenb:mute_for
label: Mute speakers temporarily
description: Mute speakers for a configured time
    label: Speaker Mute Switches
    description: The items to switch the speakers to mute
    type: TEXT
    context: item
    multiple: true
    required: true
    label: Mute For Switch
    description: The item to trigger the mute period
    type: TEXT
    context: item
    required: true
    type: INTEGER
    label: Mute Duration
    description: The duration to mute the speakers for, in seconds
    required: false
    defaultValue: 120
    label: Mute duration item
    description: The item to read the mute duration in seconds from. Takes precedence over Mute Duration.
    type: TEXT
    context: item
    required: false
    defaultValue: ""
    - id: "1"
        itemName: "{{MUTE_FOR_SWITCH_ITEM}}"
      type: core.ItemStateUpdateTrigger
conditions: []
  - inputs: {}
    id: "1"
      type: application/javascript;version=ECMAScript-2021
      script: >-
        /* global Java, event, items, actions */
        (function () {
          const muteSwitchItemNames = '{{SPEAKER_MUTE_SWITCH_ITEMS}}';
          // Multiple values are passed in like an array but without quotes.
          // So we need to parse them.
          const muteSwitchItemNamesList = muteSwitchItemNames
            // remove []
            .substr(1, muteSwitchItemNames.length - 2)
            .map(item => item.trim());
          const muteSwitchItems = => items.getItem(name));

          const muteForSwitchItemName = '{{MUTE_FOR_SWITCH_ITEM}}';
          const muteForSwitchItem = items.getItem(muteForSwitchItemName);

          const muteDurationFromArgument = '{{MUTE_DURATION}}';
          const muteDurationItemName = '{{MUTE_DURATION_ITEM}}';
          const muteDuration = muteDurationItemName !== ""
            ? Number.parseInt(items.getItem(muteDurationItemName).state)
            : muteDurationFromArgument;

          function unmute() {
            // unmute speaker
            muteSwitchItems.forEach(item => item.sendCommand("OFF"));
            // set Mute For switch OFF as well

          if (muteForSwitchItem.state == "ON") {
            // mute speaker
            muteSwitchItems.forEach(item => item.sendCommand("ON"));

            // schedule unmute
            var durationSeconds = Number.parseInt(muteDuration);
            setTimeout(unmute, durationSeconds * 1000);
          } else {
            // muteForSwitchItem is actively switched OFF so we end mute prematurely
            muteSwitchItems.forEach(item => item.sendCommand("OFF"));

    type: script.ScriptAction