#Reusable script to increase volume 5%?

16 messages · Page 1 of 1 (latest)

warm torrent
#

I have several media_player devices. I also have several 5-button scene controllers in the various rooms these media_player devices are in. My goal is to with have two of the 5-button scene controller buttons trigger 5% volume increases or decreases on the media_player in that room. I could go writing a lot of automations and copying and pasting, but this will get out of hand and will be hard to maintain.

One idea is if there's an existing service I could call just specify the name of the media_player and the percentage, e.g. 5% and tie that event to the button press event. I believe there's already one for 10% (just volume_up or volume_down)? Is there one for 10%? What if 5% is important? I often find the UI volume up and down (10%) is not granular enough for my preferences.

If I had to create this myself I'd probably write a script. I've never written one before, but I'd like it to take a variable for the media_player name and the percentage change and it would do it. Is there any examples I could build off of for something like this? Also which docs are the ones I want to use to understand how to pass in the variable name and value when I call the script from my automation triggered on button-press?

marsh crest
#

That section of the docs needs a re-write in my opinion. You can ignore fields entirely; they simply give you a spot in the UI to enter those variables. Which is useful if you call your script from the UI, or perhaps mildly useful in the automation and script editor when you call that script (because it will then prompt you for those variables).

#

I would scroll down to the 3rd example which shows how to call the script and pass in variables.

When you write the script, you just have to assume those variables already exist and you can reference them in any template.

sullen yacht
#

You can use one automation for this. Just get area of trigger.entity_id, and get media_player in that area. Piece of cake.

warm torrent
#

@sullen yacht oh that's a good idea I see what you're saying. So the action portion of my automation could be equivalent to what I had in the script and since it's only being called by exactly 1 automation I can put it in there directly (no reuse needed).

sullen yacht
#

Yeah 🙂

#

I actually have things like this in scripts (like "stop music in area"), just because I can use that scripts in dashboard, automated, or via voice.

#

But the main thing is that with good organizing you don't need to duplicate, and it will adjust automatically to changes in your entities. 🙂

warm torrent
#

yup this is a strong pattern. Would you be willing to share an example of your script? no worries if not

sullen yacht
#

Let me dig something out 🙂

#
alias: Stop music in areas
fields:
  f_areas:
    selector:
      area:
        multiple: true
    name: Areas
    required: true
sequence:
  - variables:
      v_players: |-
        {{ f_areas|map('area_entities')|map('select', 'in', integration_entities('mass'))|map('list')|sum(start = [])|select('is_state', 'playing')|list }}
  - variables:
      result:
        message: |-
          {% if v_players|length > 0 %}
            Stopping music.
          {% else %}
            Nothing is playing.
          {% endif %}
  - if:
      - condition: template
        value_template: "{{ v_players|length > 0 }}"
        alias: There's something to stop
    then:
      - action: media_player.media_stop
        target:
          entity_id: "{{ v_players }}"
  - stop: Result
    response_variable: result
#

That's simple one with stopping music on Music Assistant players in given areas

#

This one is for adjusting humidifiers for the room:

alias: Adjust humidifiers for area
fields:
  f_area:
    selector:
      area:
        entity:
          domain: humidifier
    name: Area
    required: true
sequence:
  - action: script.get_humidifiers_for_area
    data:
      f_area: "{{ f_area }}"
    response_variable: humidifiers
  - alias: Stop, if no humidifiers in area
    if:
      - condition: template
        value_template: "{{ humidifiers.all|count == 0 }}"
    then:
      - stop: No humidifiers in area
        response_variable: ""
  - data:
      f_area: "{{ f_area }}"
    response_variable: ventilation
    action: script.get_ventilation_sources_for_area
  - if:
      - condition: template
        value_template: "{{ ventilation.open|count == 0 }}"
        alias: No open windows in area
      - condition: state
        entity_id: input_select.home_state
        state:
          - Home
          - Night
    then:
      - target:
          entity_id: "{{ humidifiers.all }}"
        action: humidifier.turn_on
    else:
      - target:
          entity_id: "{{ humidifiers.all }}"
        action: humidifier.turn_off
#

It's using this to get doors/windows for room:

alias: Get ventilation sources for area
fields:
  f_area:
    selector:
      area: null
    name: Area
    required: true
sequence:
  - variables:
      result:
        all: >-
          {{ label_entities("Vent source")|select("in",
          area_entities(f_area))|list }}
        open: >-
          {{ label_entities("Vent source")|select("in",
          area_entities(f_area))|select("is_state","on")|list }}
  - stop: Result
    response_variable: result
#

I'm using labels for defining the functionality, BTW.