Sending Jinja2 templates in an HTTP Request?

Hi all,

I’m configuring a Loxone System to raise volume from a physical button to a Chromecast. Thanks to HAOS I have a nice HTTP REST interface I can easily use.

However, the basic media player “volume up” command raises the volume by 10%, and I don’t want it. I want it to go up in steps.

So I did manage to create a script that does that:

data:
  volume_level: "{{ state_attr('media_player.chromecast_dining_room', 'volume_level') + 0.01  }}"
target:
  entity_id: media_player.chromecast_dining_room
action: media_player.volume_set

However, I don’t want to create too many scripts and then call them, as I have several Chromecasts at home. I’d like to send the content of “data” as part of the HTTP command, so that I can easily communicate with multiple devices without the use of scripts.

Is that possible?

I tried to send an HTTP Request with a payload like this:

{
  "entity_id": "media_player.chromecast_dining_room",
  "volume_level": "{{ state_attr('media_player.chromecast_dining_room', 'volume_level') | float + 0.01 }}"
}

But it replied as “bad request”.

Is there a way to do that? thanks!

Make 1 script to handle them all.

volume:
  alias: Increase volume
  mode: parallel
  descrption: >
    increase the volume of any media player.
  fields:
    player: 
      description: The media player(s)
      required: True
      selector:
        entity:
          mulitple: True
          domain: media_player
    direction:
      description: Volume Up or Volume Down
      required: True
      selector:
        select:
          options:
          - label: Volume Up
            value: up
          - label: Volume Down
            value: down
    level:
      description: Increment
      default: 0.1
      selector:
        number:
          min: 0.0
          max: 0.0
          step: 0.01
  variables:
    media_player: "{{ player if player is list else [player] }}"
    current: "{{ media_player | map('state_attr', 'volume_level') | select('is_number') | list | average }}"
    step: "{{ level | default(0.1) }}"
    volume_level: "{{ current + step if direction == 'up' else current - step }}"
  sequence:
  - action: media_player.volume_set
    target:
      entity_id: "{{ media_player }}"
    data:
      volume_level: "{{ volume_level }}"

Then just call that script with the data

{
  "player": "media_player.chromecast_dining_room",
  "direction": "up"
}
{
  "player": "media_player.chromecast_dining_room",
  "direction": "down"
}

Wow this is absolutely awesome!!! Thank you petro!!!

I have no idea if it works. Try it and let me know (Post any errors from the logs)

I copy/pasted it in the YAML for the entire script and I got this:

Message malformed: extra keys not allowed @ data['volume']

Show me where you’re pasting it please

Settings → Automations and Scenes → Create Script → Create New Script → Edit in YAML (from 3 dots top right)

Yah, you need to delete the first line

volume:

and remove 2 leading spaces from Every line.

alias: Increase volume
mode: parallel
descrption: >
  increase the volume of any media player.
fields:
  player: 
    description: The media player(s)
    required: True
    selector:
      entity:
        mulitple: True
        domain: media_player
  direction:
    description: Volume Up or Volume Down
    required: True
    selector:
      select:
        options:
        - label: Volume Up
          value: up
        - label: Volume Down
          value: down
  level:
    description: Increment
    default: 0.1
    selector:
      number:
        min: 0.0
        max: 0.0
        step: 0.01
variables:
  media_player: "{{ player if player is list else [player] }}"
  current: "{{ media_player | map('state_attr', 'volume_level') | select('is_number') | list | average }}"
  step: "{{ level | default(0.1) }}"
  volume_level: "{{ current + step if direction == 'up' else current - step }}"
sequence:
- action: media_player.volume_set
  target:
    entity_id: "{{ media_player }}"
  data:
    volume_level: "{{ volume_level }}"

It worked! I just contained two typos (description and multiple). Here is the version without typos for future reference.

Thank you petro!

alias: Increase volume
mode: parallel
description: |
  increase the volume of any media player.
fields:
  player:
    description: The media player(s)
    required: true
    selector:
      entity:
        multiple: true
        domain: media_player
  direction:
    description: Volume Up or Volume Down
    required: true
    selector:
      select:
        options:
          - label: Volume Up
            value: up
          - label: Volume Down
            value: down
  level:
    description: Increment
    default: 0.1
    selector:
      number:
        min: 0
        max: 0
        step: 0.01
variables:
  media_player: "{{ player if player is list else [player] }}"
  current: >-
    {{ media_player | map('state_attr', 'volume_level') | select('is_number') |
    list | average }}
  step: "{{ level | default(0.1) }}"
  volume_level: "{{ current + step if direction == 'up' else current - step }}"
sequence:
  - action: media_player.volume_set
    target:
      entity_id: "{{ media_player }}"
    data:
      volume_level: "{{ volume_level }}"
1 Like