Automation to enable sensor

I’m trying to create an automation that triggers when my Marantz receiver is turned on and sets up a sensor that shows the source.

I’ve got a sensors.yaml config that has below sensor in there:

- platform: template                                                                        
  sensors:                                                                                    
    marantz_sr_source:                                                                          
      value_template: '{% if states.media_player.sr6014_2.state %}{{ states.media_player.sr6014_2.attributes["source"] | default }}{% endif %}'

If the receiver is available when Hassio starts the sensor shows up fine, but doesn’t when I turn on the receiver afterwards (I’ve disabled network control on the receiver to limit power usage btw).

Can anyone help me out with the action format for this sensor in the automation?

Try this:

- platform: template                                                                        
  sensors:                                                                                    
    marantz_sr_source:                                                                          
      value_template: >
        {% if states('media_player.sr6014_2') not in ['unkonwn', 'unavailable'] %}
          {{ state_attr('media_player.sr6014_2', 'source') }}
        {% else %}
          'off'
        {% endif %}

Most likely the sensor is not loading at start up when the player s off because of this:

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

Also you need to supply a state when the player is off.

Hi Tom,

Thanks for the quick response! I noticed a typo in your code ‘unkonwn’ instead of ‘unknown’. After changing that the state is being recognised as ‘off’ when hassio restarts and the receiver is off and the correct audio source when the receiver is on. But if I turn on the receiver when the sensor shows off it doesn’t change to the audio source.

Is there a way to create an automation that change the sensor state when the receiver gets turned on?

Edit: Scratch that. It seems to be working, but it takes a lot longer before it updates the sensor after turning on the receiver. Longer than it used to take when the receiver was still accesible in standby mode over the network.

1 Like

What does this show in the template editor when you change the source?

{{ state_attr('media_player.sr6014_2', 'source') }}

At the moment it’s showing ‘END’, but the whole night it had been showing ‘TV audio’ even though I turned it off yesterday evening via remote.

I just noticed that it behaves strangely when I turn it off via the remote. If I turn the receiver off via Hassio it seems to detect the state change properly.

Btw: the state of the reveiver itself shows up as ‘unknown’ via:

{{ states('media_player.sr6014_2')}}

Then (if that is the correct entity_id and the player is on) there is something wrong with your media player integration.

It only shows the wrong value sometimes when it’s turned off via remote (so it seems).

If it’s turned on it switches to the correct source after a certain amount of time.

I’ve got the Marantz setup with below config:

media_player:                                                                                                                                                                                             
  - platform: denon                                                                           
    name: Marantz SR6014                                                                      
    host: 192.168.1.143

I’ve disabled the Denon HEOS integration. Maybe I should start using that and remove the config of the Marantz receiver from the configuration.yaml?