Denon integration volume in dB

I am trying to show denon receiver volume in dBs as a gauge card in lovelace and mostly have it working, except it displays an error when the receiver is powered off (entity is not numeric: sensor.compensation) and is slow to update the number when I turn up the volume with the remote. I am using “compensation” integration to get “volume_level” from “media_player.denon_receiver”. I also have a template sensor to get a volume level when the receiver is ON but don’t know how to combine the two. Any suggestions?

A few bits of my config file:

- sensor:
      - name: "denon_volume"
        state: >
          {% if is_state('media_player.denon_receiver', 'on')  %}
            {{ (state_attr('media_player.denon_receiver', 'volume_level') * 103) | int }}
          {% endif %}
  - sensor:                                                                                     
        - name: "denon_vol_zone2"
          state: >
          {% if is_state('media_player.denon_receiver_2', 'on')  %}
            {{ (state_attr('media_player.denon_receiver_2', 'volume_level') * 103) | int >
          {% endif %}

# Compensation
compensation:
   db_volume_zone1:
    source: media_player.denon_receiver
    attribute: volume_level                                                                   
    unit_of_measurement: dB
    data_points:
      - [0.0, -80.0]
      - [1.0, 20.0]                                                                       
  db_volume_zone2:
    source: media_player.denon_receiver_2
    attribute: volume_level                                                                   
    unit_of_measurement: dB
    data_points:
      - [0.0, -80.0]
      - [1.0, 20.0]

Can you please edit your post and format your pasted configuration correctly for the forum, see: How to help us help you - or How to ask a good question

But basically you need an {% else %} case for when the receiver is off. You can use {{ this.state }} if you want it to remember the last volume setting before the receiver was turned off. Otherwise set it to 0, or whatever numeric value you want.

Thank you, edited to make it look better. Is there a way to convert volume to db in a template sensor or use “state” within compensation? As I have it right now the template works, but shows volume in percentage and compensation also works, but throws an error in GUI when the receiver is off. Just trying to find one method to use in a gauge card

There is no value template for the compensation sensor so you have to use a template sensor. You can do the conversion from 0 → 1 to dB in the template sensor, so no need to use the compensation integration:

- sensor:
    - name: "Denon Volume"
      unit_of_measurement: "dB"
      state: >
        {% if is_state('media_player.denon_receiver', 'on')  %}
          {{ 100 * state_attr('media_player.denon_receiver', 'volume_level')|int(0) - 80 }}
        {% else %}
          {{ this.state }}
        {% endif %}
      availability: "{{ state_attr('media_player.denon_receiver', 'volume_level')|is_number }}"
- sensor:                                                                                     
    - name: "Denon Zone 2 Volume"
      unit_of_measurement: "dB"
      state: >
        {% if is_state('media_player.denon_receiver_2', 'on')  %}
          {{ 100 * state_attr('media_player.denon_receiver_2', 'volume_level')|int(0) - 80 }}
        {% else %}
          {{ this.state }}
        {% endif %}
      availability: "{{ state_attr('media_player.denon_receiver_2', 'volume_level')|is_number }}"

These sensors will keep the last known volume setting when the receiver is turned off. If you want them set to zero when the receiver is turned off instead use this:

- sensor:
    - name: "Denon Volume"
      unit_of_measurement: "dB"
      state: >
        {% if is_state('media_player.denon_receiver', 'on')  %}
          {{ 100 * state_attr('media_player.denon_receiver', 'volume_level')|int(0) - 80 }}
        {% else %}
          0
        {% endif %}
      availability: "{{ state_attr('media_player.denon_receiver', 'volume_level')|is_number }}"
- sensor:                                                                                     
    - name: "Denon Zone 2 Volume"
      unit_of_measurement: "dB"
      state: >
        {% if is_state('media_player.denon_receiver_2', 'on')  %}
          {{ 100 * state_attr('media_player.denon_receiver_2', 'volume_level')|int(0) - 80 }}
        {% else %}
          0
        {% endif %}
      availability: "{{ state_attr('media_player.denon_receiver_2', 'volume_level')|is_number }}"

Thanks for your help with this, I have changed one of the sensors, but now when the receiver is off I get “entity is non-numeric value error”. The code looks like below and state for “sensor.denon_volume” is unknown when receiver is off. If I set else to 0, it works, but I would like the gauge to either go away or show some other value than 0 as in dB that means reference level

  - sensor:
      - name: "denon_volume"
        unit_of_measurement: "dB"
        state: >
          {% if is_state('media_player.denon_receiver', 'on')  %}
            {{ (state_attr('media_player.denon_receiver', 'volume_level')*100) | int(0) -80 }}
          {% else %}
            {{ this.state }}
          {% endif %}
        availability: "{{ state_attr('media_player.denon.receiver', 'volume_level') | is_number }}"

Try just removing the availability template.

Tried that as well, but no luck. Decided to just create a conditional card for lovelace, working that way. Now to figure out why it takes a few seconds to update the sensor when the volume is lowered/raised

I played around with volume sensor a bit more, but cannot get to update quicker than 5 sec. Does anybody know how to change poling interval for template.sensor or maybe I can try a different integration? Basically I would like to see the input, volume and sound mode on the tablet as my receiver is in the closet. Any suggestions?

Template sensors do not have a polling interval. They update when the source entities change.

The solution for me was to upgrade to the newer version of HA which includes denon integration with telnet option, now all sensors are updating almost instantly. I was running HA in venv and older version of python, so I ended up converting to docker and portainer install. Thanks for your help with template sensors.