Volume control via telnet

Hello,

I want to control the volume of an audio device via home assistant. The device can be controlled via telnet. This requires me to send a numeric value, namely the volume the user selects via telnet. I am struggling to find a way to do that. The telnet switch component seems to only allow to send one command for on and off and the command_on and command_off parts do not allow me to use value templates. I have tried something like
living_room_volume:
resource: AAA.BBB.CCC.DDD
port: 12345
command_on: “^VPZ @1,{{states.input_number.living_room_volume.state|int}}$”
command_off: “^VPZ @1,{{states.input_number.living_room_volume.state|int}}$”

But that did not work. It looks like a command arrives at the audio devices, but it has the wrong syntax. I dont know how to see the actual telnet command that is sent to the device (putting the telnet component in debug mode doesnt show anything), but I can see on the device that a command with wrong syntax has been received, so I suppose the templating failed.

I then tried to go for a shell_command via
shell_command:
set_volume_living_room: echo ^VPZ @{{zone}},{{volume}}$ | curl --raw --max-time 1 telnet://AAA.BBB.CCC.DDD:12345

But again this failed. It does not even look like anything arrived at the device. If I enter the curl command as above in the shell it works fine.

I am at a loss. Are there any other ways to do this?

Many thanks!

Hi and welcome to the forum.

The telnet integration is a switch and the commands do not support templating. You are on the right track using the shell command integration. Unfortunately other than offering a bit of advice about the template I can’t help much with the actual command.

If using a template on a single line it must be quoted.

shell_command:
  set_volume_living_room: "echo ^VPZ @{{zone}},{{volume}}$ | curl --raw --max-time 1 telnet://AAA.BBB.CCC.DDD:12345"

You can use a multiline template format that does not require quotes:

shell_command:
  set_volume_living_room: >
    echo ^VPZ @{{zone}},{{volume}}$ | curl --raw --max-time 1 telnet://AAA.BBB.CCC.DDD:12345

You have not defined the variables zone or volume are you going to pass these to the shell command using data:?

Or do you want to use the input number you had in your attempt at the telnet switch?

If the latter:

shell_command:
  set_volume_living_room: >
    {% set volume == states('input_number.living_room_volume')|int(0) %}
    {% set zone == states('input_number.living_room_zone)|int(0) %} 
    echo ^VPZ @{{zone}},{{volume}}$ | curl --raw --max-time 1 telnet://AAA.BBB.CCC.DDD:12345

Though passing the zone and volume when calling the command is more flexible.

Note the use of states(). This is due to this warning in the templating documentation.

https://www.home-assistant.io/docs/configuration/templating/#states

Also please have a read of point 11 here regarding formattting your post.

Sorry I could not be more help with the actual telnet command. Hopefully someone else can help you out with this.

Hi @hunte

Did you find out a working solution to control the Zektor audio matrix ? Killing myself too trying to get something working with shell_command but no success so far while it works perfect in terminal with same command line :frowning:

Hi, I have a similar situation:
The command is *Z2VOLUME60 (set the volume at 60%)

If I try:

shell_command:
  imposta_volume: "echo *Z2VOLUME60 | curl --raw --max-time 1 telnet://192.168.0.127:23"

it works…

But with this code doesn’t work:

input_number:
  volume:
    name: volume_hifi
    initial: 30
    min: 0
    max: 100
    step: 10

shell_command:
  imposta_volume: "echo *Z2VOLUME{{states('input_number.volume')}} | curl --raw --max-time 1 telnet://192.168.0.127:23"

The input number works, and I have no errors

any suggestion?

Hi, same exact situation… Were you able to solve it?

not solved

finally solved!!!

Could you explain how you solved?

Hi Ben, the other option, which I eventually got to is pyscript.

I have a bunch of audio, HDMI and home theater devices that all use telnet for the command interface (along with other gadgets such as TVs which have all their own connections and integrations). I’ve been using HA since about March 2023 - 3.5 months.

After struggling with complex HA Automations, shell scripts using nc (netcat) and various methods including json to update HA afterwards, I ended up with a relatively slow, un-maintainable and un-reliable solution.

I have some scripting experience but knew nothing about python. But in desperation, knowing that telnetlib for python existed (thank you ! ), and given that HA appears to be largely a python creation, I started (a) learning basic python (supported by Python tester - Test code online), and (b) working through the python alternatives.

I looked at HA’s python_script but it was going to be complex to get relatively simple things done. There were two other options whose names I can’t recall but were also going to be complex.

I eventually found pyscript, read the concise but very good documentation, and eventually migrated. It is well integrated into HA, provides global and persistent variables, has replaced all of my shell scripts (bash) and HA scripts, and almost all of my Automations. Total number of lines has been reduced by about 70%. Pyscript turned out to be easy learn - level is about VB script rather than C# .

I now have an HA for my environment that is much more maintainable and reliable, and is quicker.

David