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

Changelog

Version 0.1

  • initial release

Resources

uid: prosenb:mute_for
label: Mute speakers temporarily
description: Mute speakers for a configured time
configDescriptions:
  - name: SPEAKER_MUTE_SWITCH_ITEMS
    label: Speaker Mute Switches
    description: The items to switch the speakers to mute
    type: TEXT
    context: item
    multiple: true
    required: true
  - name: MUTE_FOR_SWITCH_ITEM
    label: Mute For Switch
    description: The item to trigger the mute period
    type: TEXT
    context: item
    required: true
  - name: MUTE_DURATION
    type: INTEGER
    label: Mute Duration
    description: The duration to mute the speakers for, in seconds
    required: false
    defaultValue: 120
  - name: MUTE_DURATION_ITEM
    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: ""
triggers:
    - id: "1"
      configuration:
        itemName: "{{MUTE_FOR_SWITCH_ITEM}}"
      type: core.ItemStateUpdateTrigger
conditions: []
actions:
  - inputs: {}
    id: "1"
    configuration:
      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)
            .split(",")
            .map(item => item.trim());
          const muteSwitchItems = muteSwitchItemNamesList.map(name => 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
            muteForSwitchItem.sendCommand("OFF");
          }

          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