Turn off receiver after 2 h when nothing is played

I would like to turn off my receiver (Onkyo controlled with Logitech remote) with “chrome audio” built in if it hasn’t played music for two hours. I’ve started with the following automation but can’t figure out to get the time part in as some kind of condition since I don’t have the last played time. Maybe I can start a time a time somehow when the state is turn to off. At the same time I don’t want to turn off if it starts to play music again or if the receiver source is changed to let’s say TV.

- alias: 'Turn off receiver when not play music for 2 hours'
  initial_state: true
  trigger:
    platform: state
    entity_id: media_player.hemma
    to: 'off'
  action:
    service: remote.turn_on
    data:
      entity_id: remote.remote
      activity: 'PowerOff'
- alias: 'Turn off receiver when not play music for 2 hours'
  initial_state: true
  trigger:
    platform: state
    entity_id: media_player.hemma
    to: 'off'
    for: "2:00:00"
  action:
    service: remote.turn_on
    data:
      entity_id: remote.remote
      activity: 'PowerOff'

You can add a time component into your trigger…

- alias: 'Turn off receiver when not play music for 2 hours'
  initial_state: true
  trigger:
    platform: state
    entity_id: media_player.hemma
    to: 'off'
    for:
      minutes: 30

You’ll need to add a condition to test for the receiver input ( assuming the state is available to Home Assistant )

I realised that the state is more likely to be idle, so

- alias: 'Turn off receiver when not play music for 2 hours'
  initial_state: true
  trigger:
    platform: state
    entity_id: media_player.hemma
    to: 'idle'
    for: "2:00:00"

Worked perfectly, and added condition as mentioned below as well. Thank for all!