HOW TO: Use Alexa/Echo to control media volume "Alexa, turn up amplifier!"

I’ve been using Home Assistant(HASS) for about a month on a Raspberry Pi and am totally hooked. I wish the learning curve wasn’t so steep, but with better documentation that will certainly get better I’m sure! None the less, it’s a really flexible and powerful platform and I’m very appreciative to all the people who have worked so hard on it!

With that said, I recently built a Raspberry Pi based IR transmitter to control my old Kenwood amplifier, but thus far I have only been able to use the Emulated Hue component to increase/decrease the volume by saying, “Alexa, turn on MusicUp”/“Alexa, turn on MusicDown” via the following configuration:

switch:
  - platform: command_line
    switches:
      volup:
        command_on: '/usr/bin/curl -X POST http://192.168.1.67:3000/macros/Vol%20Up'
        friendly_name: Music Up
  - platform: command_line
    switches:
      voldown:
        command_on: '/usr/bin/curl -X POST http://192.168.1.67:3000/macros/Vol%20Down'
        friendly_name: Music Down  

This wasn’t very intuitive, so instead I wanted to say, “Alexa, raise amplifier!” or “Alexa, lower amplifier”. I struggled for a while with the Emulated Hue, but wasn’t able to get it to work. I finally succeeded and this is how I did it:

INSTALL HA-BRIDGE FOR BETTER HUE CONTROL:

Make sure your Raspberry Pi Jesse image is up to date: sudo apt-get update; sudo apt-get dist-upgrade

  1. cd /home/pi/; mkdir habridge; cd habridge; mkdir data; wget https://github.com/bwssytems/ha-bridge/releases/download/v4.2.0/ha-bridge-4.2.0.jar;
  2. nano habridge.service => copy and paste the following (make sure the jar version shown below matches the version you’ve downloaded):
    [Unit]
    Description=HA Bridge
    Wants=network.target
    After=network.target
    [Service]
    Type=simple
    ExecStart=/usr/bin/java -jar -Dconfig.file=/home/pi/habridge/data/habridge.config /home/pi/habridge/ha-bridge-4.2.0.jar
    [Install]
    WantedBy=multi-user.target
  3. sudo apt-get update && sudo apt-get install oracle-java8-jdk; sudo update-alternatives --config java
    (select the JDK 8 version (/usr/lib/jvm/jdk-8-oracle-arm32-vfp-hflt/jre/bin/java))
    java -version
    sudo systemctl enable /home/pi/habridge/habridge.service; sudo systemctl start habridge
    now visit http:// YOUR_HASS_IP (default port 80)

On the Bridge Control tab toward the bottom add HomeAssistant details (Home, YOUR_HASS_IP, port 8123 and password). Once added, a new tab should appear on the main menu ‘Home Assistant Devices’. Every single HA device will be shown, just select the ‘Build Item’ button for the device you want to add. In this case we’re going to manually add a new Hue volume device…

CREATE A NEW HUE VOLUME DEVICE:

Click Bridge Devices → Edit/Copy device and use the below info to set it up:

Name: Amp (Whatever you put for this is what Alexa will call it, make sure it’s a unique name)
Target: Encapsulated
Device Type: Custom

On Items:

Target: http://YOUR_HASS_IP:8123/api/services/input_slider/select_value
HTTP Verb: POST
HTTP Body: {“entity_id”:“input_slider.my_vol”,“value”:“255”}
HTTP Headers: [{“name”:“X-HA-Access”, “value”:“PASSWORD”}]
Content Type: application/json
Click (Add)

Dim Items:

	Target: http://YOUR_HASS_IP:8123/api/services/input_slider/select_value
	HTTP Verb: POST
	HTTP Body: {"entity_id":"input_slider.my_vol","value":${intensity.byte}}
	HTTP Headers: [{"name":"X-HA-Access", "value":"PASSWORD"}]
	Content Type: application/json
	Click (Add)

Off Items:

Target: http://YOUR_HASS_IP:8123/api/services/input_slider/select_value
HTTP Verb: POST
HTTP Body: {“entity_id”:“input_slider.my_vol”,“value”:“0”}
HTTP Headers: [{“name”:“X-HA-Access”, “value”:“PASSWORD”}]
Content Type: application/json
Click (Add)
To save your changes:
Click (Add Bridge Device)
Click (Update Bridge Device) when you are all done!

Now that you’ve setup a HA Bridge HUE device that sends Alexa requests to HASS, it’s time to setup your HASS configuration.yaml file.

configuration.yaml (Add the following lines to your configuration)
input_slider:
  my_vol:
    name: Brightness
    initial: 100
    min: 0
    max: 255
    step: 1


volume.yaml: (Add the following lines to a new **automation**, I made a separate file called volume.yaml 
- alias: VOLUME UP - emulated hue slider automation
  trigger:
    platform: state
    entity_id: input_slider.my_vol
  condition:
    condition: template
    value_template: '{{ states("input_slider.my_vol") | int > 100 }}'  # If slider value > 100 then turn on volume and reset slider back to 100  
  action:
    - service: shell_command.volume_up				       
    - service: input_slider.select_value
      data:
        entity_id: input_slider.my_vol
        value: 100

- alias: VOLUME DOWN - emulated hue slider automation
  trigger:
    platform: state
    entity_id: input_slider.my_vol
  condition:
    condition: template
    value_template: '{{ states("input_slider.my_vol") | int < 100 }}'  # If slider value < 100 then turn off volume and reset slider back to 100
  action:
    - service: shell_command.volume_down
    - service: input_slider.select_value
      data:
        entity_id: input_slider.my_vol
        value: 100

Don’t forget to change shell_command.volume_up / shell_command.volume_down to whatever IR commands you want to transmit. Also, don;t forget to enable separate automations to your configuration.yaml file, if you want to keep your automations separate from the rest of your configuration.yaml file. i.e. add the following line to configuration.yaml:
automation: !volume.yaml

Now that HA Bridge is installed you can say ‘Alexa, Discover’, and all your Hue devices should be found automatically. FYI, In the Alexa app HA Bridge devices are shown as LUX LIGHT, while emulated_hue devices are shown as DIMMABLE LIGHTS.

From here you should be able to say, “Alexa, raise amp!” or “Alexa, lower amp!”. The words “increase” and “decrease” also seem to work as well.

Determining States and Commands:

The below steps aren’t really necessary for use, but I thought I might include them if you want to peek under the hood to get a better idea of how messages are passed to and from HASS, or if you need to do some debugging, then you can follow along:

  1. Determine JSON calls by going to your HASS web page, click the Hamburger menu on the top left to see Developer Tools, then click “< >” and search for a switch type entity name and double click it.
  2. In a Chrome Browser click View → Developer → Developer Tools → Click on Network Tab → click Record Network Log button if it’s not already red
  3. Scroll to the top of the screen to see the Entity ID, State and State Attributes then click ‘Set State’ to record the Network Events.
  4. View the recorded event and look at the Headers tab and scroll to the bottom where it says ‘Request Payload’ and this will show the states and attributes to use
    when communicating with HASS.
  5. Use a Google Chrome browser add in called POSTMAN tool to validate your findings:
    type: POST
    url: http:// YOUR_HASS_IP:8123/api/services/switch/turn_off?api_password=YOUR_PASSWORD (NOTE if setup as a light then it won’t work if called with switch/Turn_off)
    alternative url: http:// YOUR_HASS_IP:8123/api/services/homeassistant/turn_on?api_password=YOUR_PASSWORD ((NOTE the homeassistant word works for both a light or a switch))
    headers: name: X-HA-Access
    headers: Content-Type: application/json
    body: raw JSON(application/json) {“entity_id”:“switch.tunes”} (NOTE {“entity_id”:“switch.tunes”,“brightness”:“144”} brightness only works with a light or slider and not switches)

Once POSTMAN fires the command you should see ‘OK’ returned, if it was a good transaction, and then hopefully see the state of your entity change. If not, it’s easy to get tripped up by even one wrong character.

4 Likes

Thanks for posting this, i’ve been meaning to figure out if this was possible to control my gas fireplace.

I only have on/off working at the moment (sending 433mhz signals, not IR), but “Alexa, turn up/down the fire” would be a great addition :slight_smile:

Hi R_J,

Sure thing. Good luck with the fireplace!

Nice write up thanks!

I have a bunch of volume stuff I’m using with emulated hue, gave up and put them into custom skills in the end.

Could this also be achieved with Haaska?

I haven’t played with Haaska to validate the functionality, but from reading the Haaska documentation it looks like it would work.