Platform: history_stats . Is it possible that it only counts if a plug is turned on?

Hello everyone, I currently have some sensors like “history_stats” these I use to have a summary of the viewing time of content such as youtube, Netflix…
My question is, would it be possible that the time will only count if apart from what I indicate on the sensor so that it begins to count, the condition is that a plug is turned on? How could I do it?
Thank you for your help!

## Youtube      
  - platform: history_stats
    name: Fire Tv - Time - Youtube
    entity_id: sensor.fire_tv_content_type
    state: "YouTube (FireTV)"
    type: time
    start: "{{ now().replace(hour=0).replace(minute=0).replace(second=0) }}"
    end: "{{ now() }}"

Make a Template Binary Sensor that reports on only when the switch is on and the sensor reports YouTube (FireTV).

template:
  - binary_sensor:
      - name: "Fire TV Netflix"
        state: >
          {{ is_state('sensor.fire_tv_content_type', 'YouTube (FireTV)') and 
            is_state('switch.your_plug', 'on') }}

Reference the Template Binary Sensor in the History Stats sensor’s configuration:

## Youtube      
  - platform: history_stats
    name: Fire Tv - Time - Youtube
    entity_id: binary_sensor.fire_tv_netflix
    state: "on"
    type: time
    start: "{{ today_at() }}"
    end: "{{ now() }}"
1 Like

Hi, sorry for the delay in replying. It worked perfectly for me. Thank you very much for your help!

1 Like