Return value from shell script controlling NAD preamp?

Is it possible to read a return value from a shell script? I have a number of working shell scripts in configuration.yaml that control my NAD preamp, which are all in this general form:

nad_on: echo -e "\rMain.Power=On\r" | nc 192.168.4.41 23

These are all “send only” though. According to the NAD manual, I should be able to query the state of some items (such as power, source etc.) as well as just set them. This involved just replacing the “=value” in the command with “?”, like this:

nad_power: echo -e "\rMain.Power?\r" | nc 192.168.4.41 23

This command runs without any errors, but I can’t see anywhere in the logs or events that the return value is captured or displayed. Am I just looking in the wrong place or is this just not possible?

Instead of a shell command, try a command line sensor:

1 Like

Thanks Tom - that works perfectly!

For the benefit of anyone else with the same issue, here is the code I ended up with in configuration.yaml:

sensors:
- platform: command_line
    name: NAD Power
    command: 'echo -e "\rMain.Power?\r" | nc 192.168.4.41 23'
    
  - platform: command_line
    name: NAD Source
    command: 'echo -e "\rMain.Source?\r" | nc 192.168.4.41 23'

This give me two sensore that return values like this:

Main.Model=C658Main.Power=On and Main.Model=C658Main.Source=6

Now I just need to template the output to extract the value I need from that string.

1 Like

For the power:

value_template: '{{ value.split("=")[2] }}'

For the source (example sources, you have to work out the number to name mapping, and maximum number of sources) :

value_template: >
  {% set source_numb = value.split("=")[2] %}
  {% if source_numb == 0 %}
    TV
  {% elif source_numb == 1 %}
    HDMI2
...
  {% elif source_numb == 6 %}
    DVD
  {% else %}
    unknown
  {% endif %}

Which can also be simplified to:

value_template: >
  {% set source_numb = value.split("=")[2]|int %}
  {% if 6 >= source_numb >= 0 %}
    {{ ["TV", "HDMI2", ... "DVD"][source_numb] }}
  {% else %}
    unknown
  {% endif %}
1 Like

Excellent - thanks. Saves me some work!